From 0a36538f4e9cbb060d0cd8c43048456c46c91caf Mon Sep 17 00:00:00 2001 From: "patrick.lambert" <patrick.mh.lambert@inrae.fr> Date: Fri, 19 Mar 2021 17:20:11 +0100 Subject: [PATCH] add a method to calculate the number of values > a threshold --- src/main/java/miscellaneous/QueueMemory.java | 99 ++++++++++++-------- 1 file changed, 59 insertions(+), 40 deletions(-) diff --git a/src/main/java/miscellaneous/QueueMemory.java b/src/main/java/miscellaneous/QueueMemory.java index 3c93a4b..9acb461 100644 --- a/src/main/java/miscellaneous/QueueMemory.java +++ b/src/main/java/miscellaneous/QueueMemory.java @@ -1,20 +1,20 @@ 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; } } - -- GitLab