1 | package desmoj.core.simulator; |
2 | |
3 | import java.util.EnumMap; |
4 | import java.util.Map; |
5 | import java.util.concurrent.TimeUnit; |
6 | |
7 | /** |
8 | * This is the default Time Formatter. Use this class to format TimeSpan and |
9 | * TimeInstant objects like in the following examples: 5.1 , 5.1 SECONDS, |
10 | * 12.035, 12.035 MINUTES. |
11 | * |
12 | * @version DESMO-J, Ver. 2.3.3 copyright (c) 2011 |
13 | * @author Felix Klueckmann |
14 | * |
15 | * Licensed under the Apache License, Version 2.0 (the "License"); you |
16 | * may not use this file except in compliance with the License. You may |
17 | * obtain a copy of the License at |
18 | * http://www.apache.org/licenses/LICENSE-2.0 |
19 | * |
20 | * Unless required by applicable law or agreed to in writing, software |
21 | * distributed under the License is distributed on an "AS IS" BASIS, |
22 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or |
23 | * implied. See the License for the specific language governing |
24 | * permissions and limitations under the License. |
25 | * |
26 | */ |
27 | public class SingleUnitTimeFormatter implements TimeFormatter { |
28 | private static Map<TimeUnit, Map<TimeUnit, Long>> timeConstants = new EnumMap<TimeUnit, Map<TimeUnit, Long>>( |
29 | TimeUnit.class); |
30 | |
31 | static { |
32 | |
33 | Map<TimeUnit, Long> dayConstants = new EnumMap<TimeUnit, Long>( |
34 | TimeUnit.class); |
35 | dayConstants.put(TimeUnit.HOURS, 24l); |
36 | dayConstants.put(TimeUnit.MINUTES, 1440l); |
37 | dayConstants.put(TimeUnit.SECONDS, 86400l); |
38 | dayConstants.put(TimeUnit.MILLISECONDS, 86400000l); |
39 | dayConstants.put(TimeUnit.MICROSECONDS, 86400000000l); |
40 | dayConstants.put(TimeUnit.NANOSECONDS, 86400000000000l); |
41 | Map<TimeUnit, Long> hourConstants = new EnumMap<TimeUnit, Long>( |
42 | TimeUnit.class); |
43 | hourConstants.put(TimeUnit.MINUTES, 60l); |
44 | hourConstants.put(TimeUnit.SECONDS, 3600l); |
45 | hourConstants.put(TimeUnit.MILLISECONDS, 3600000l); |
46 | hourConstants.put(TimeUnit.MICROSECONDS, 3600000000l); |
47 | hourConstants.put(TimeUnit.NANOSECONDS, 3600000000000l); |
48 | Map<TimeUnit, Long> minuteConstants = new EnumMap<TimeUnit, Long>( |
49 | TimeUnit.class); |
50 | minuteConstants.put(TimeUnit.SECONDS, 60l); |
51 | minuteConstants.put(TimeUnit.MILLISECONDS, 60000l); |
52 | minuteConstants.put(TimeUnit.MICROSECONDS, 60000000l); |
53 | minuteConstants.put(TimeUnit.NANOSECONDS, 60000000000l); |
54 | Map<TimeUnit, Long> secondConstants = new EnumMap<TimeUnit, Long>( |
55 | TimeUnit.class); |
56 | secondConstants.put(TimeUnit.MILLISECONDS, 1000l); |
57 | secondConstants.put(TimeUnit.MICROSECONDS, 1000000l); |
58 | secondConstants.put(TimeUnit.NANOSECONDS, 1000000000l); |
59 | Map<TimeUnit, Long> milliConstants = new EnumMap<TimeUnit, Long>( |
60 | TimeUnit.class); |
61 | milliConstants.put(TimeUnit.MICROSECONDS, 1000l); |
62 | milliConstants.put(TimeUnit.NANOSECONDS, 1000000l); |
63 | Map<TimeUnit, Long> microConstants = new EnumMap<TimeUnit, Long>( |
64 | TimeUnit.class); |
65 | microConstants.put(TimeUnit.NANOSECONDS, 1000l); |
66 | |
67 | timeConstants.put(TimeUnit.DAYS, dayConstants); |
68 | timeConstants.put(TimeUnit.HOURS, hourConstants); |
69 | timeConstants.put(TimeUnit.MINUTES, minuteConstants); |
70 | timeConstants.put(TimeUnit.SECONDS, secondConstants); |
71 | timeConstants.put(TimeUnit.MILLISECONDS, milliConstants); |
72 | timeConstants.put(TimeUnit.MICROSECONDS, microConstants); |
73 | |
74 | } |
75 | /** |
76 | * The TimeUnit that is used in this TimeFormatter |
77 | */ |
78 | private final TimeUnit _myTimeUnit; |
79 | /** |
80 | * the value of epsilon used in this TimeFormatter |
81 | */ |
82 | private final TimeUnit _epsilon; |
83 | /** |
84 | * A Factor used for unit conversion |
85 | */ |
86 | private final long _myFactor; |
87 | /** |
88 | * The number of digits after the decimal point which will be displayed for |
89 | * the time values. |
90 | */ |
91 | protected final long _floats; |
92 | |
93 | /** |
94 | * |
95 | */ |
96 | private final long _precisionFactor; |
97 | |
98 | /** |
99 | * Flag that indicates if the time unit will be included in the time String. |
100 | * |
101 | */ |
102 | private final boolean _writeUnit; |
103 | |
104 | /** |
105 | * Constructs a DecimalTimeFormatter. |
106 | * |
107 | * @param unit |
108 | * java.util.concurrent.TimeUnit : The time values that will be |
109 | * used |
110 | * @param floats |
111 | * int : The number of floating point digits to print |
112 | * @param writeUnit |
113 | * boolean : Indicates if the time unit will be included in the |
114 | * time String. |
115 | */ |
116 | public SingleUnitTimeFormatter(TimeUnit unit, TimeUnit epsilon, int floats, boolean writeUnit) { |
117 | this._myTimeUnit = unit; |
118 | this._epsilon=epsilon; |
119 | this._floats = floats; |
120 | this._precisionFactor=(long)java.lang.Math.pow(10, floats); |
121 | this._writeUnit = writeUnit; |
122 | this._myFactor=epsilon.convert(1,_myTimeUnit); |
123 | } |
124 | |
125 | /**Returns the String-Representation of the given TimeInstant |
126 | * |
127 | */ |
128 | public String buildTimeString(TimeInstant instant) { |
129 | return buildSingleUnitTimeString(instant.getTimeInEpsilon()); |
130 | } |
131 | |
132 | /**Returns the String-Representation of the given TimeSpan |
133 | * |
134 | */ |
135 | public String buildTimeString(TimeSpan span) { |
136 | return buildSingleUnitTimeString(span.getTimeInEpsilon()); |
137 | } |
138 | |
139 | /**Returns the String-Representation of the given time object. |
140 | * |
141 | */ |
142 | private String buildSingleUnitTimeString(long timeValue){ |
143 | StringBuffer timeStringBuffer = new StringBuffer(); |
144 | |
145 | if (_myTimeUnit.compareTo(_epsilon) > 0) { |
146 | // unit is a coarser granularity than epsilon |
147 | timeStringBuffer.append('.'); |
148 | //append seperator |
149 | long fractionTime = _myTimeUnit.convert((timeValue |
150 | % _myFactor)*_precisionFactor, |
151 | _epsilon); |
152 | |
153 | String fractionTimeString= Long.toString(fractionTime); |
154 | char zero = '0'; |
155 | //append as many zeros as needed |
156 | for (int i = fractionTimeString.length(); i <_floats; i++) { |
157 | timeStringBuffer.append(zero); |
158 | } |
159 | |
160 | timeStringBuffer.append(fractionTimeString); |
161 | |
162 | } |
163 | timeStringBuffer.insert(0,Long.toString(_myTimeUnit.convert(timeValue,_epsilon))); |
164 | if (_writeUnit) { |
165 | //append the name of the time unit |
166 | timeStringBuffer.append(' '); |
167 | timeStringBuffer.append(_myTimeUnit.name()); |
168 | } |
169 | return timeStringBuffer.toString(); |
170 | } |
171 | } |