13
13
* See the License for the specific language governing permissions and
14
14
* limitations under the License.
15
15
*/
16
- package com .arpnetworking .tsdcore .model ;
16
+ package com .arpnetworking .metrics . mad .model ;
17
17
18
18
import com .arpnetworking .commons .builder .ThreadLocalBuilder ;
19
19
import com .arpnetworking .logback .annotations .Loggable ;
20
20
import com .google .common .base .MoreObjects ;
21
21
import edu .umd .cs .findbugs .annotations .SuppressFBWarnings ;
22
22
import net .sf .oval .constraint .NotNull ;
23
23
24
- import java .io .Serializable ;
25
24
import java .util .Objects ;
26
25
import java .util .Optional ;
27
26
28
27
/**
29
- * Represents a sample.
28
+ * Default sample implementation .
30
29
*
31
30
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
32
31
*/
33
32
@ Loggable
34
- public final class Quantity implements Comparable < Quantity >, Serializable {
33
+ public final class DefaultQuantity implements Quantity {
35
34
35
+ @ Override
36
36
public double getValue () {
37
37
return _value ;
38
38
}
39
39
40
+ @ Override
40
41
public Optional <Unit > getUnit () {
41
42
return _unit ;
42
43
}
43
44
44
- /**
45
- * Add this <code>Quantity</code> to the specified one returning the
46
- * result. Both <code>Quantity</code> instances must either not have a
47
- * <code>Unit</code> or the <code>Unit</code> must be of the same type.
48
- *
49
- * @param otherQuantity The other <code>Quantity</code>.
50
- * @return The resulting sum <code>Quantity</code>.
51
- */
45
+ @ Override
52
46
public Quantity add (final Quantity otherQuantity ) {
53
- if (_unit .isPresent () != otherQuantity ._unit .isPresent ()) {
47
+ if (_unit .isPresent () != otherQuantity .getUnit () .isPresent ()) {
54
48
throw new IllegalStateException (String .format (
55
49
"Units must both be present or absent; thisQuantity=%s otherQuantity=%s" ,
56
50
this ,
57
51
otherQuantity ));
58
52
}
59
- if (Objects .equals (_unit , otherQuantity ._unit )) {
60
- return new Quantity (_value + otherQuantity ._value , _unit );
53
+ if (Objects .equals (_unit , otherQuantity .getUnit () )) {
54
+ return new DefaultQuantity (_value + otherQuantity .getValue () , _unit );
61
55
}
62
56
final Unit smallerUnit = _unit .get ().getSmallerUnit (otherQuantity .getUnit ().get ());
63
- return new Quantity (
57
+ return new DefaultQuantity (
64
58
smallerUnit .convert (_value , _unit .get ())
65
- + smallerUnit .convert (otherQuantity ._value , otherQuantity ._unit .get ()),
59
+ + smallerUnit .convert (otherQuantity .getValue () , otherQuantity .getUnit () .get ()),
66
60
Optional .of (smallerUnit ));
67
61
}
68
62
69
- /**
70
- * Subtract the specified <code>Quantity</code> from this one returning
71
- * the result. Both <code>Quantity</code> instances must either not have
72
- * a <code>Unit</code> or the <code>Unit</code> must be of the same type.
73
- *
74
- * @param otherQuantity The other <code>Quantity</code>.
75
- * @return The resulting difference <code>Quantity</code>.
76
- */
63
+ @ Override
77
64
public Quantity subtract (final Quantity otherQuantity ) {
78
- if (_unit .isPresent () != otherQuantity ._unit .isPresent ()) {
65
+ if (_unit .isPresent () != otherQuantity .getUnit () .isPresent ()) {
79
66
throw new IllegalStateException (String .format (
80
67
"Units must both be present or absent; thisQuantity=%s otherQuantity=%s" ,
81
68
this ,
82
69
otherQuantity ));
83
70
}
84
- if (Objects .equals (_unit , otherQuantity ._unit )) {
85
- return new Quantity (_value - otherQuantity ._value , _unit );
71
+ if (Objects .equals (_unit , otherQuantity .getUnit () )) {
72
+ return new DefaultQuantity (_value - otherQuantity .getValue () , _unit );
86
73
}
87
74
final Unit smallerUnit = _unit .get ().getSmallerUnit (otherQuantity .getUnit ().get ());
88
- return new Quantity (
75
+ return new DefaultQuantity (
89
76
smallerUnit .convert (_value , _unit .get ())
90
- - smallerUnit .convert (otherQuantity ._value , otherQuantity ._unit .get ()),
77
+ - smallerUnit .convert (otherQuantity .getValue () , otherQuantity .getUnit () .get ()),
91
78
Optional .of (smallerUnit ));
92
79
}
93
80
94
- /**
95
- * Multiply this <code>Quantity</code> with the specified one returning
96
- * the result.
97
- *
98
- * @param otherQuantity The other <code>Quantity</code>.
99
- * @return The resulting product <code>Quantity</code>.
100
- */
81
+ @ Override
101
82
public Quantity multiply (final Quantity otherQuantity ) {
102
- if (_unit .isPresent () && otherQuantity ._unit .isPresent ()) {
83
+ if (_unit .isPresent () && otherQuantity .getUnit () .isPresent ()) {
103
84
throw new UnsupportedOperationException ("Compound units not supported yet" );
104
85
}
105
- return new Quantity (
106
- _value * otherQuantity ._value ,
107
- Optional .ofNullable (_unit .orElse (otherQuantity ._unit .orElse (null ))));
86
+ return new DefaultQuantity (
87
+ _value * otherQuantity .getValue () ,
88
+ Optional .ofNullable (_unit .orElse (otherQuantity .getUnit () .orElse (null ))));
108
89
}
109
90
110
- /**
111
- * Divide this <code>Quantity</code> by the specified one returning
112
- * the result.
113
- *
114
- * @param otherQuantity The other <code>Quantity</code>.
115
- * @return The resulting quotient <code>Quantity</code>.
116
- */
91
+ @ Override
117
92
public Quantity divide (final Quantity otherQuantity ) {
118
93
// TODO(vkoskela): Support division by quantity with unit [2F].
119
- if (otherQuantity ._unit .isPresent ()) {
94
+ if (otherQuantity .getUnit () .isPresent ()) {
120
95
throw new UnsupportedOperationException ("Compound units not supported yet" );
121
96
}
122
- if (Objects .equals (_unit , otherQuantity ._unit )) {
123
- return new Quantity (_value / otherQuantity ._value , Optional .empty ());
97
+ if (Objects .equals (_unit , otherQuantity .getUnit () )) {
98
+ return new DefaultQuantity (_value / otherQuantity .getValue () , Optional .empty ());
124
99
}
125
- return new Quantity (
126
- _value / otherQuantity ._value ,
100
+ return new DefaultQuantity (
101
+ _value / otherQuantity .getValue () ,
127
102
_unit );
128
103
}
129
104
130
105
@ Override
131
106
public int compareTo (final Quantity other ) {
132
- if (other ._unit .equals (_unit )) {
133
- return Double .compare (_value , other ._value );
107
+ if (other .getUnit () .equals (_unit )) {
108
+ return Double .compare (_value , other .getValue () );
134
109
}
135
110
throw new IllegalArgumentException (String .format (
136
111
"Cannot compare mismatched units; this=%s, other=%s" ,
@@ -148,14 +123,14 @@ public boolean equals(final Object o) {
148
123
if (this == o ) {
149
124
return true ;
150
125
}
151
- if (!(o instanceof Quantity )) {
126
+ if (!(o instanceof DefaultQuantity )) {
152
127
return false ;
153
128
}
154
129
155
- final Quantity sample = (Quantity ) o ;
130
+ final DefaultQuantity sample = (DefaultQuantity ) o ;
156
131
157
- return Double .compare (sample ._value , _value ) == 0
158
- && Objects .equals (_unit , sample ._unit );
132
+ return Double .compare (sample .getValue () , _value ) == 0
133
+ && Objects .equals (_unit , sample .getUnit () );
159
134
}
160
135
161
136
@ Override
@@ -167,11 +142,11 @@ public String toString() {
167
142
.toString ();
168
143
}
169
144
170
- private Quantity (final Builder builder ) {
145
+ private DefaultQuantity (final Builder builder ) {
171
146
this (builder ._value , Optional .ofNullable (builder ._unit ));
172
147
}
173
148
174
- private Quantity (final double value , final Optional <Unit > unit ) {
149
+ private DefaultQuantity (final double value , final Optional <Unit > unit ) {
175
150
_value = value ;
176
151
_unit = unit ;
177
152
}
@@ -185,24 +160,24 @@ private Quantity(final double value, final Optional<Unit> unit) {
185
160
/**
186
161
* <code>Builder</code> implementation for <code>Quantity</code>.
187
162
*/
188
- public static final class Builder extends ThreadLocalBuilder <Quantity > {
163
+ public static final class Builder extends ThreadLocalBuilder <DefaultQuantity > {
189
164
190
165
/**
191
166
* Public constructor.
192
167
*/
193
168
public Builder () {
194
- super ((java .util .function .Function <Builder , Quantity >) Quantity ::new );
169
+ super ((java .util .function .Function <Builder , DefaultQuantity >) DefaultQuantity ::new );
195
170
}
196
171
197
172
/**
198
173
* Public constructor.
199
174
*
200
175
* @param quantity the <code>Quantity</code> to initialize from
201
176
*/
202
- public Builder (final Quantity quantity ) {
203
- super ((java .util .function .Function <Builder , Quantity >) Quantity ::new );
204
- _value = quantity ._value ;
205
- _unit = quantity ._unit .orElse (null );
177
+ public Builder (final DefaultQuantity quantity ) {
178
+ super ((java .util .function .Function <Builder , DefaultQuantity >) DefaultQuantity ::new );
179
+ _value = quantity .getValue () ;
180
+ _unit = quantity .getUnit () .orElse (null );
206
181
}
207
182
208
183
/**
@@ -228,9 +203,9 @@ public Builder setUnit(final Unit value) {
228
203
}
229
204
230
205
@ Override
231
- public Quantity build () {
206
+ public DefaultQuantity build () {
232
207
normalize ();
233
- return new Quantity (this );
208
+ return new DefaultQuantity (this );
234
209
}
235
210
236
211
@ Override
0 commit comments