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

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

parent 98ed1ea1
package miscellaneous;
import java.util.concurrent.ArrayBlockingQueue;
import java.lang.Number;
public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
private int memorySize;
private int memorySize;
public QueueMemory(int memorySize) {
super(memorySize);
this.memorySize = memorySize;
}
public void push(E item){
public void push(E item) {
try {
if (this.size() == this.memorySize){
if (this.size() == this.memorySize) {
this.poll();
}
this.put(item);
......@@ -24,77 +24,97 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
}
}
public E getLastItem(){
public E getLastItem() {
Object[] items = this.toArray();
if (items.length > 0)
return (E) items[items.length-1];
return (E) items[items.length - 1];
else
return null;
}
public E getItemFromLast(int i){
public E getItemFromLast(int i) {
Object[] items = this.toArray();
if (i>=0 & i<items.length)
return (E) items[items.length-i];
if (i >= 0 & i < items.length)
return (E) items[items.length - i];
else
return null;
}
public double getSum(){
double sum= 0.;
for (E item : this){
public double getSum() {
double sum = 0.;
for (E item : this) {
sum += this.doubleValue(item);
}
return (sum);
}
public double getMean(){
double sum= 0.;
for (E item : this){
sum += this.doubleValue(item);
public int getNumberOfStriclyHigherValue(double threshold) {
int nb = 0;
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);
}
public double getMeanWithoutZero(){
double sum= 0.;
double total= 0.;
for (E item : this){
if (this.doubleValue(item) > 0.){
sum += this.doubleValue(item);
public double getMeanWithoutZero() {
double sum = 0.;
double total = 0.;
for (E item : this) {
if (this.doubleValue(item) > 0.) {
sum += this.doubleValue(item);
total++;
}
}
}
if(total > 0){
sum = sum / total;
if (total > 0) {
sum = sum / total;
}
return (sum);
}
public double getGeometricMean(){
double sum= 0.;
for (E item : this){
public double getGeometricMean() {
double sum = 0.;
for (E item : this) {
sum += Math.log(this.doubleValue(item));
}
return (Math.exp(sum / memorySize));
}
public double getStandartDeviation(){
double mean= this.getMean();
double sse= 0;
for (E item : this){
sse += Math.pow(this.doubleValue(item) - mean, 2.);
public double getStandartDeviation() {
double mean = this.getMean();
double sse = 0;
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());
}
private double doubleValue(E item){
private double doubleValue(E item) {
if (item instanceof Double)
return ((Double) item);
else if (item instanceof Float)
......@@ -107,4 +127,3 @@ public class QueueMemory<E extends Number> extends ArrayBlockingQueue<E> {
return Double.NaN;
}
}
Markdown is supported
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