1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.monitoring;
19
20 import java.util.ArrayList;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.Map;
24 import java.util.Set;
25 import java.util.concurrent.ConcurrentHashMap;
26 import java.util.concurrent.CopyOnWriteArrayList;
27 import java.util.concurrent.CopyOnWriteArraySet;
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42 public class Unit implements Comparable<Unit>
43 {
44 private static final Map<String, Unit> UNITS = new ConcurrentHashMap<String,Unit>();
45
46
47 public static final Unit NANOS = new Unit( "ns" );
48 public static final Unit MICROS = new Unit( "µs", NANOS, 1000 );
49 public static final Unit MILLIS = new Unit( "ms", MICROS, 1000 );
50 public static final Unit SECOND = new Unit( "s", MILLIS, 1000 );
51 public static final Unit MINUTE = new Unit( "min", SECOND, 60 );
52 public static final Unit HOUR = new Unit( "h", MINUTE, 60 );
53 public static final Unit DAY = new Unit( "day", HOUR, 24 );
54
55
56 public static final Unit BYTE = new Unit( "b" );
57 public static final Unit KBYTE = new Unit( "Kb", BYTE, 1024 );
58 public static final Unit MBYTE = new Unit( "Mb", KBYTE, 1024 );
59 public static final Unit GBYTE = new Unit( "Gb", MBYTE, 1024 );
60
61
62
63
64
65
66 public static final Unit UNARY = new Unit( "" );
67 public static final Unit DECA = new Unit( "*10", UNARY, 10 );
68 public static final Unit HECTO = new Unit( "*100", DECA, 10 );
69 public static final Unit KILO = new Unit( "*1000", HECTO, 10 );
70 public static final Unit MEGA = new Unit( "*10^6", KILO, 1000 );
71 public static final Unit GIGA = new Unit( "*10^9", MEGA, 1000 );
72 public static final Unit TERA = new Unit( "*10^12", GIGA, 1000 );
73
74 private final String name;
75
76 private final long scale;
77
78 private Unit primary;
79
80 private List<Unit> derived;
81
82 public Set<Unit> primaryUnits = new CopyOnWriteArraySet<Unit>();
83
84
85 public static Unit get( String name )
86 {
87 return UNITS.get( name );
88 }
89
90
91
92
93
94 public Unit( String name )
95 {
96 this.name = name;
97 this.primary = this;
98 this.scale = 1;
99 this.derived = new ArrayList<Unit>();
100 this.derived.add( this );
101 primaryUnits.add( this );
102 UNITS.put( name, this );
103 }
104
105
106
107
108
109
110
111 public Unit( String name, Unit derived, long scale )
112 {
113 this.name = name;
114 this.primary = derived.isPrimary() ? derived : derived.getPrimary();
115 this.scale = scale * derived.getScale();
116 primary.derived.add( this );
117 Collections.sort( primary.derived );
118 UNITS.put( name, this );
119 }
120
121 public Unit getDerived( String name )
122 {
123 for ( Unit unit : derived )
124 {
125 if (unit.name.equals( name ))
126 {
127 return unit;
128 }
129 }
130 return null;
131 }
132
133 public List<Unit> getDerived()
134 {
135 return Collections.unmodifiableList( derived );
136 }
137
138
139 public String getName()
140 {
141 return name;
142 }
143
144 public long getScale()
145 {
146 return scale;
147 }
148
149 public boolean isPrimary()
150 {
151 return primary == this;
152 }
153
154 public boolean isCompatible( Unit unit )
155 {
156 return primary == unit.getPrimary();
157 }
158
159 public Unit getPrimary()
160 {
161 return this.primary;
162 }
163
164 public int compareTo( Unit o )
165 {
166 return scale < o.scale ? -1 : 1;
167 }
168
169 public String toString()
170 {
171 return name;
172 }
173
174
175
176
177 @Override
178 public int hashCode()
179 {
180 final int prime = 31;
181 int result = 1;
182 result = prime * result + ( ( name == null ) ? 0 : name.hashCode() );
183 return result;
184 }
185
186
187
188
189 @Override
190 public boolean equals( Object obj )
191 {
192 if ( this == obj )
193 return true;
194 if ( obj == null )
195 return false;
196 if ( getClass() != obj.getClass() )
197 return false;
198 final Unit other = (Unit) obj;
199 if ( name == null )
200 {
201 if ( other.name != null )
202 return false;
203 }
204 else if ( !name.equals( other.name ) )
205 return false;
206 return true;
207 }
208
209 }