Commit 0a36538f authored by patrick.lambert's avatar patrick.lambert
Browse files

add a method to calculate the number of values > a threshold

No related merge requests found
Showing with 59 additions and 40 deletions
+59 -40
package miscellaneous; package miscellaneous;
import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.ArrayBlockingQueue;
import java.lang.Number;
public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> { public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
private int memorySize; private int memorySize;
public QueueMemory(int memorySize) { public QueueMemory(int memorySize) {
super(memorySize); super(memorySize);
this.memorySize = memorySize; this.memorySize = memorySize;
} }
public void push(E item){
public void push(E item) {
try { try {
if (this.size() == this.memorySize){ if (this.size() == this.memorySize) {
this.poll(); this.poll();
} }
this.put(item); this.put(item);
...@@ -24,77 +24,97 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> { ...@@ -24,77 +24,97 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
} }
} }
public E getLastItem(){
public E getLastItem() {
Object[] items = this.toArray(); Object[] items = this.toArray();
if (items.length > 0) if (items.length > 0)
return (E) items[items.length-1]; return (E) items[items.length - 1];
else else
return null; return null;
} }
public E getItemFromLast(int i){
public E getItemFromLast(int i) {
Object[] items = this.toArray(); Object[] items = this.toArray();
if (i>=0 & i<items.length) if (i >= 0 & i < items.length)
return (E) items[items.length-i]; return (E) items[items.length - i];
else else
return null; return null;
} }
public double getSum(){
double sum= 0.; public double getSum() {
for (E item : this){ double sum = 0.;
for (E item : this) {
sum += this.doubleValue(item); sum += this.doubleValue(item);
} }
return (sum); return (sum);
} }
public double getMean(){
double sum= 0.; public int getNumberOfStriclyHigherValue(double threshold) {
for (E item : this){ int nb = 0;
sum += this.doubleValue(item); for (E item : this) {
double value = this.doubleValue(item);
if (value > threshold)
nb++;
} }
sum = sum / memorySize ; return nb;
}
public double getMean() {
double sum = 0.;
for (E item : this) {
sum += this.doubleValue(item);
}
sum = sum / memorySize;
return (sum); return (sum);
} }
public double getMeanWithoutZero(){
double sum= 0.; public double getMeanWithoutZero() {
double total= 0.; double sum = 0.;
for (E item : this){ double total = 0.;
if (this.doubleValue(item) > 0.){ for (E item : this) {
sum += this.doubleValue(item); if (this.doubleValue(item) > 0.) {
sum += this.doubleValue(item);
total++; total++;
} }
} }
if(total > 0){ if (total > 0) {
sum = sum / total; sum = sum / total;
} }
return (sum); return (sum);
} }
public double getGeometricMean(){
double sum= 0.; public double getGeometricMean() {
for (E item : this){ double sum = 0.;
for (E item : this) {
sum += Math.log(this.doubleValue(item)); sum += Math.log(this.doubleValue(item));
} }
return (Math.exp(sum / memorySize)); return (Math.exp(sum / memorySize));
} }
public double getStandartDeviation(){
double mean= this.getMean(); public double getStandartDeviation() {
double sse= 0; double mean = this.getMean();
for (E item : this){ double sse = 0;
sse += Math.pow(this.doubleValue(item) - mean, 2.); for (E item : this) {
sse += Math.pow(this.doubleValue(item) - mean, 2.);
} }
return (Math.sqrt(sse /(memorySize -1.))); return (Math.sqrt(sse / (memorySize - 1.)));
} }
public double getCoefficientVariation(){
public double getCoefficientVariation() {
return (this.getStandartDeviation() / this.getMean()); return (this.getStandartDeviation() / this.getMean());
} }
private double doubleValue(E item){
private double doubleValue(E item) {
if (item instanceof Double) if (item instanceof Double)
return ((Double) item); return ((Double) item);
else if (item instanceof Float) else if (item instanceof Float)
...@@ -107,4 +127,3 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> { ...@@ -107,4 +127,3 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
return Double.NaN; return Double.NaN;
} }
} }
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment