1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
4 * All rights reserved. This program and the accompanying materials
5 * are made available under the terms of the Eclipse Public License v1.0
6 * which accompanies this distribution, and is available at
7 * http://www.eclipse.org/legal/epl-v10.html
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.strings.format;
14 import java.util.ArrayList;
15 import java.util.List;
18 * List of metrics formats
20 * @author Toni Kalajainen
22 public class MetricsFormatList {
24 public static MetricsFormat METRICS_GENERIC = new MetricsFormat("%s", 1.0, "Generic");
26 public static MetricsFormat METRICS_EXPONENT2 = new MetricsFormat("%e", 1.0, "Exponent");
28 public static MetricsFormat METRICS_EXPONENT = new MetricsFormat("D0.##E0",1.0, "Exponent");
30 public static MetricsFormat METRICS_DECIMAL = new MetricsFormat("D0.0###", 1.0, "Decimal");
32 public static MetricsFormat METRICS_DECIMAL2 = new MetricsFormat("D0.####", 1.0, "Decimal");
34 public static MetricsFormat METRICS_DECIMAL3 = new MetricsFormat("%,g", 1.0, "Decimal");
36 public static MetricsFormat METRICS_DATE_FI = new MetricsFormat("%td.%tm.%ty", 1.0, "Date dd.mm.yyyy");
38 public static MetricsFormat METRICS_DATE_IMPERIAL = new MetricsFormat("%tm/%td/%ty", 1.0, "Date mm/dd/yyyy");
40 public static MetricsFormat METRICS_TIME_SECONDS = new MetricsFormat("%tS.%tL", 1.0, "Time, ss.ms");
42 public static MetricsFormat METRICS_CLOCK_TIME = new MetricsFormat("%tH:%tM:%tS.%tL", 1.0, "Time, hh:mm:ss.ms");
44 public static MetricsFormat METRICS_CLOCK_TIME2 = new MetricsFormat("%tH:%tM:%tS", 1.0, "Time, hh:mm:ss");
47 private final List<MetricsFormat> formats = new ArrayList<MetricsFormat>();
49 private final List<MetricsFormatListListener> listeners = new ArrayList<MetricsFormatListListener>();
51 public MetricsFormat[] getFormats() {
52 synchronized (formats) {
53 return formats.toArray(new MetricsFormat[0]);
57 public MetricsFormatList() {
60 private void addDefaults() {
62 addFormat(METRICS_DECIMAL);
63 addFormat(METRICS_DECIMAL2);
64 addFormat(METRICS_DECIMAL3);
65 addFormat(METRICS_EXPONENT);
66 addFormat(METRICS_EXPONENT2);
67 addFormat(METRICS_GENERIC);
68 addFormat(METRICS_DATE_FI);
69 addFormat(METRICS_DATE_IMPERIAL);
70 addFormat(METRICS_TIME_SECONDS);
71 addFormat(METRICS_CLOCK_TIME);
72 addFormat(METRICS_CLOCK_TIME2);
73 } catch (Exception e) {
77 public void addListener(MetricsFormatListListener listener) {
79 listeners.add(listener);
83 public void removeListener(MetricsFormatListListener listener) {
85 listeners.remove(listener);
89 private void fireListChanged() {
91 for (MetricsFormatListListener listener : listeners)
92 listener.onListChanged(this);
96 public MetricsFormat addFormat(MetricsFormat format) throws Exception {
98 MetricsFormat old = findEqual(format);
100 throw new Exception("Format already exists");
107 public void removeFormat(MetricsFormat format) throws Exception {
108 synchronized (this) {
109 int index = formats.indexOf(format);
111 throw new Exception("Format not found");
112 formats.remove(format);
117 public MetricsFormat replaceFormatWith(MetricsFormat oldFormat, MetricsFormat newFormat) throws Exception {
118 synchronized (this) {
119 if (formats.contains(newFormat))
120 throw new Exception("New format already exists");
121 if (oldFormat == newFormat)
122 throw new Exception("No changes");
123 int index = formats.indexOf(oldFormat);
125 throw new Exception("Format not found");
126 formats.remove(index);
127 formats.add(index, newFormat);
133 public MetricsFormat findEqual(MetricsFormat format) {
134 synchronized (this) {
135 int i = formats.indexOf(format);
138 return formats.get(i);
142 public MetricsFormat findByIndex(int index) {
143 return formats.get(index);
146 public int findIndex(MetricsFormat format) {
147 for (int i=0; i<formats.size(); i++)
148 if (formats.get(i).equals(format))
153 private static MetricsFormatList list;
155 public static MetricsFormatList getList() {
160 list = new MetricsFormatList();