1
0
mirror of https://github.com/octoleo/plantuml.git synced 2024-06-11 12:42:26 +00:00
plantuml/src/net/sourceforge/plantuml/api/NumberAnalyzed.java

245 lines
6.0 KiB
Java
Raw Normal View History

2015-04-07 18:26:58 +00:00
/* ========================================================================
* PlantUML : a free UML diagram generator
* ========================================================================
*
2016-03-06 16:47:34 +00:00
* Project Info: http://plantuml.com
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* If you like this project or if you find it useful, you can support us at:
2022-08-17 17:34:24 +00:00
*
2017-03-15 19:13:31 +00:00
* http://plantuml.com/patreon (only 1$ per month!)
* http://plantuml.com/paypal
2022-08-17 17:34:24 +00:00
*
2015-04-07 18:26:58 +00:00
* This file is part of PlantUML.
*
* PlantUML is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* PlantUML distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU General Public
* License aint with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
* USA.
*/
package net.sourceforge.plantuml.api;
2016-12-01 20:29:25 +00:00
import java.util.StringTokenizer;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
2016-11-18 21:12:09 +00:00
import java.util.prefs.Preferences;
2022-08-17 17:34:24 +00:00
import net.sourceforge.plantuml.log.Logme;
2022-12-17 11:01:10 +00:00
import net.sourceforge.plantuml.utils.Log;
2023-02-02 17:59:43 +00:00
2015-04-07 18:26:58 +00:00
public class NumberAnalyzed implements INumberAnalyzed {
2018-06-12 20:50:45 +00:00
private static final int SLIDING_WINDOW = 1024;
2016-12-01 20:29:25 +00:00
2016-11-18 21:12:09 +00:00
private long nb;
private long sum;
private long min;
private long max;
2016-12-01 20:29:25 +00:00
private long sumOfSquare;
// See https://fossies.org/linux/haproxy/include/proto/freq_ctr.h
private long sliddingSum;
2016-11-18 21:12:09 +00:00
private final String name;
2016-12-01 20:29:25 +00:00
private final Lock saveLock = new ReentrantLock();
2016-11-18 21:12:09 +00:00
public NumberAnalyzed(String name) {
this.name = name;
}
2015-04-07 18:26:58 +00:00
2016-12-01 20:29:25 +00:00
public synchronized void reset() {
this.nb = 0;
this.sum = 0;
this.min = 0;
this.max = 0;
this.sumOfSquare = 0;
this.sliddingSum = 0;
}
2015-04-07 18:26:58 +00:00
public NumberAnalyzed() {
2016-11-18 21:12:09 +00:00
this("");
}
2015-04-07 18:26:58 +00:00
2016-12-01 20:29:25 +00:00
public final void save(Preferences prefs) {
2016-11-18 21:12:09 +00:00
if (name.length() == 0) {
throw new UnsupportedOperationException();
}
2016-12-01 20:29:25 +00:00
if (saveLock.tryLock())
try {
prefs.put(name + ".saved", getSavedString());
} finally {
saveLock.unlock();
}
2015-04-07 18:26:58 +00:00
}
2016-12-01 20:29:25 +00:00
private String getSavedString() {
final long nb1;
final long sum1;
final long min1;
final long max1;
final long sumOfSquare1;
final long sliddingSum1;
final String supp1;
synchronized (this) {
nb1 = nb;
sum1 = sum;
min1 = min;
max1 = max;
sumOfSquare1 = sumOfSquare;
sliddingSum1 = sliddingSum;
2016-11-18 21:12:09 +00:00
}
2016-12-01 20:29:25 +00:00
supp1 = getSavedSupplementatyData();
return longToString(nb1) + ";" + longToString(sum1) + ";" + longToString(min1) + ";" + longToString(max1) + ";"
+ longToString(sumOfSquare1) + ";" + longToString(sliddingSum1) + ";" + supp1 + ";";
}
protected String getSavedSupplementatyData() {
return "";
}
protected final String longToString(long val) {
return Long.toString(val, 36);
}
public static NumberAnalyzed load(String name, Preferences prefs) {
final String value = prefs.get(name + ".saved", "");
if (value.length() == 0) {
Log.info("Cannot load " + name);
2016-11-18 21:12:09 +00:00
return null;
}
2016-12-01 20:29:25 +00:00
try {
final StringTokenizer st = new StringTokenizer(value, ";");
return new NumberAnalyzed(name, Long.parseLong(st.nextToken(), 36), Long.parseLong(st.nextToken(), 36),
Long.parseLong(st.nextToken(), 36), Long.parseLong(st.nextToken(), 36), Long.parseLong(
st.nextToken(), 36), Long.parseLong(st.nextToken(), 36));
} catch (Exception e) {
2022-08-17 17:34:24 +00:00
Logme.error(e);
2016-12-01 20:29:25 +00:00
Log.info("Error reading " + value);
2016-11-18 21:12:09 +00:00
return null;
}
}
@Override
public synchronized String toString() {
return "sum=" + sum + " nb=" + nb + " min=" + min + " max=" + max + " mean=" + getMean();
}
2016-12-01 20:29:25 +00:00
protected NumberAnalyzed(String name, long nb, long sum, long min, long max, long sumOfSquare, long sliddingSum) {
2016-11-18 21:12:09 +00:00
this(name);
2015-04-07 18:26:58 +00:00
this.nb = nb;
this.sum = sum;
this.min = min;
this.max = max;
2016-12-01 20:29:25 +00:00
this.sumOfSquare = sumOfSquare;
this.sliddingSum = sliddingSum;
2015-04-07 18:26:58 +00:00
}
public synchronized INumberAnalyzed getCopyImmutable() {
2016-12-01 20:29:25 +00:00
final NumberAnalyzed copy = new NumberAnalyzed(name, nb, sum, min, max, sumOfSquare, sliddingSum);
2015-04-07 18:26:58 +00:00
return copy;
}
2016-11-18 21:12:09 +00:00
public synchronized void addValue(long v) {
2015-04-07 18:26:58 +00:00
nb++;
if (nb == 1) {
min = v;
max = v;
2016-12-01 20:29:25 +00:00
} else if (v > max) {
2015-04-07 18:26:58 +00:00
max = v;
2016-12-01 20:29:25 +00:00
} else if (v < min) {
2015-04-07 18:26:58 +00:00
min = v;
}
2016-12-01 20:29:25 +00:00
sum += v;
sumOfSquare += v * v;
sliddingSum = sliddingSum * (SLIDING_WINDOW - 1) / SLIDING_WINDOW + v;
2015-04-07 18:26:58 +00:00
}
2016-12-01 20:29:25 +00:00
public void add(NumberAnalyzed other) {
final long nb1;
final long sum1;
final long min1;
final long max1;
final long sumOfSquare1;
final long sliddingSum1;
2016-11-18 21:12:09 +00:00
synchronized (other) {
2016-12-01 20:29:25 +00:00
nb1 = other.nb;
sum1 = other.sum;
min1 = other.min;
max1 = other.max;
sumOfSquare1 = other.sumOfSquare;
sliddingSum1 = other.sliddingSum;
}
synchronized (this) {
this.sum += sum1;
this.nb += nb1;
this.min = Math.min(this.min, min1);
this.max = Math.max(this.max, max1);
this.sumOfSquare += sumOfSquare1;
// Not good!
this.sliddingSum += sliddingSum1;
2016-11-18 21:12:09 +00:00
}
}
synchronized public final long getNb() {
2015-04-07 18:26:58 +00:00
return nb;
}
2016-11-18 21:12:09 +00:00
synchronized public final long getSum() {
2015-04-07 18:26:58 +00:00
return sum;
}
2016-11-18 21:12:09 +00:00
synchronized public final long getMin() {
2015-04-07 18:26:58 +00:00
return min;
}
2016-11-18 21:12:09 +00:00
synchronized public final long getMax() {
2015-04-07 18:26:58 +00:00
return max;
}
2016-11-18 21:12:09 +00:00
synchronized public final long getMean() {
2015-04-07 18:26:58 +00:00
if (nb == 0) {
return 0;
}
return sum / nb;
}
2018-06-12 20:50:45 +00:00
synchronized public final long getSliddingMean() {
if (nb == 0) {
return 0;
}
if (nb < SLIDING_WINDOW) {
return sum / nb;
}
return sliddingSum / nb;
}
2016-12-01 20:29:25 +00:00
public final long getStandardDeviation() {
final long sum1;
final long sumOfSquare1;
final long nb1;
synchronized (this) {
sum1 = this.sum;
sumOfSquare1 = this.sumOfSquare;
nb1 = this.nb;
}
if (nb1 == 0) {
return 0;
}
final long mean = sum1 / nb1;
return Math.round(Math.sqrt(sumOfSquare1 / nb1 - mean * mean));
}
final public String getName() {
2016-11-18 21:12:09 +00:00
return name;
}
2015-04-07 18:26:58 +00:00
}