net.sourceforge.plantuml.stats.FormatCounter Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of plantuml-mit Show documentation
Show all versions of plantuml-mit Show documentation
PlantUML is a component that allows to quickly write diagrams from text.
// THIS FILE HAS BEEN GENERATED BY A PREPROCESSOR.
package net.sourceforge.plantuml.stats;
import java.util.EnumMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.prefs.BackingStoreException;
import java.util.prefs.Preferences;
import net.sourceforge.plantuml.FileFormat;
import net.sourceforge.plantuml.api.NumberAnalyzed;
import net.sourceforge.plantuml.log.Logme;
import net.sourceforge.plantuml.stats.api.StatsColumn;
import net.sourceforge.plantuml.stats.api.StatsLine;
import net.sourceforge.plantuml.stats.api.StatsTable;
public class FormatCounter {
private ConcurrentMap data = new ConcurrentHashMap();
public FormatCounter(String prefix) {
for (FileFormat format : FileFormat.values()) {
final String key = prefix + format.name();
data.put(format, new NumberAnalyzed(key));
}
}
public void plusOne(FileFormat fileFormat, long duration) {
final NumberAnalyzed n = data.get(fileFormat);
n.addValue(duration);
}
private StatsLine createLine(String name, NumberAnalyzed n) {
final Map result = new EnumMap(StatsColumn.class);
result.put(StatsColumn.FORMAT, name);
result.put(StatsColumn.GENERATED_COUNT, n.getNb());
result.put(StatsColumn.GENERATED_MEAN_TIME, n.getMean());
result.put(StatsColumn.GENERATED_STANDARD_DEVIATION, n.getStandardDeviation());
result.put(StatsColumn.GENERATED_MAX_TIME, n.getMax());
return new StatsLineImpl(result);
}
public StatsTable getStatsTable(String name) {
final StatsTableImpl result = new StatsTableImpl(name);
final NumberAnalyzed total = new NumberAnalyzed();
for (Map.Entry ent : data.entrySet()) {
final NumberAnalyzed n = ent.getValue();
if (n.getNb() > 0) {
result.addLine(createLine(ent.getKey().name(), n));
total.add(n);
}
}
result.addLine(createLine("Total", total));
return result;
}
public void reload(String prefix, Preferences prefs) throws BackingStoreException {
for (String key : prefs.keys()) {
if (key.startsWith(prefix)) {
try {
final String name = removeDotSaved(key);
final NumberAnalyzed value = NumberAnalyzed.load(name, prefs);
if (value != null) {
final FileFormat format = FileFormat.valueOf(name.substring(prefix.length()));
data.put(format, value);
}
} catch (Exception e) {
Logme.error(e);
}
}
}
}
static String removeDotSaved(String key) {
return key.substring(0, key.length() - ".saved".length());
}
public void save(Preferences prefs, FileFormat fileFormat) {
data.get(fileFormat).save(prefs);
}
}