Skip to content

Commit 5f328d9

Browse files
authored
Relocate the tscore models into the mad models directory and extract interfaces where necessary. This will help with development of an external parser plugin. This is also in-alignment, although not identical, to what will happen in MAD-2.0. (#155)
1 parent 1dd1060 commit 5f328d9

File tree

61 files changed

+414
-348
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

61 files changed

+414
-348
lines changed

src/main/java/com/arpnetworking/metrics/mad/Bucket.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
import com.arpnetworking.commons.builder.OvalBuilder;
1919
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
2020
import com.arpnetworking.logback.annotations.LogValue;
21+
import com.arpnetworking.metrics.mad.model.DefaultQuantity;
2122
import com.arpnetworking.metrics.mad.model.Metric;
23+
import com.arpnetworking.metrics.mad.model.Quantity;
2224
import com.arpnetworking.metrics.mad.model.Record;
2325
import com.arpnetworking.steno.LogValueMapFactory;
2426
import com.arpnetworking.steno.Logger;
@@ -27,7 +29,6 @@
2729
import com.arpnetworking.tsdcore.model.CalculatedValue;
2830
import com.arpnetworking.tsdcore.model.Key;
2931
import com.arpnetworking.tsdcore.model.PeriodicData;
30-
import com.arpnetworking.tsdcore.model.Quantity;
3132
import com.arpnetworking.tsdcore.sinks.Sink;
3233
import com.arpnetworking.tsdcore.statistics.Accumulator;
3334
import com.arpnetworking.tsdcore.statistics.Calculator;
@@ -274,7 +275,7 @@ private void computeStatistics(
274275
CalculatedValue.Builder.class,
275276
b -> b.setValue(
276277
ThreadLocalBuilder.build(
277-
Quantity.Builder.class,
278+
DefaultQuantity.Builder.class,
278279
builder -> builder.setValue(-1.0))));
279280
}
280281

src/main/java/com/arpnetworking/metrics/mad/model/DefaultMetric.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.LogValue;
2020
import com.arpnetworking.steno.LogValueMapFactory;
21-
import com.arpnetworking.tsdcore.model.MetricType;
22-
import com.arpnetworking.tsdcore.model.Quantity;
2321
import com.google.common.base.MoreObjects;
2422
import com.google.common.base.Objects;
2523
import com.google.common.collect.ImmutableList;
@@ -68,7 +66,7 @@ public int hashCode() {
6866
public String toString() {
6967
return MoreObjects.toStringHelper(this)
7068
.add("id", Integer.toHexString(System.identityHashCode(this)))
71-
.add("Type", _type)
69+
.add("UnitType", _type)
7270
.add("Values", _values)
7371
.toString();
7472
}

src/main/java/com/arpnetworking/tsdcore/model/Quantity.java renamed to src/main/java/com/arpnetworking/metrics/mad/model/DefaultQuantity.java

Lines changed: 44 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -13,124 +13,99 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.arpnetworking.tsdcore.model;
16+
package com.arpnetworking.metrics.mad.model;
1717

1818
import com.arpnetworking.commons.builder.ThreadLocalBuilder;
1919
import com.arpnetworking.logback.annotations.Loggable;
2020
import com.google.common.base.MoreObjects;
2121
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
2222
import net.sf.oval.constraint.NotNull;
2323

24-
import java.io.Serializable;
2524
import java.util.Objects;
2625
import java.util.Optional;
2726

2827
/**
29-
* Represents a sample.
28+
* Default sample implementation.
3029
*
3130
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
3231
*/
3332
@Loggable
34-
public final class Quantity implements Comparable<Quantity>, Serializable {
33+
public final class DefaultQuantity implements Quantity {
3534

35+
@Override
3636
public double getValue() {
3737
return _value;
3838
}
3939

40+
@Override
4041
public Optional<Unit> getUnit() {
4142
return _unit;
4243
}
4344

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
5246
public Quantity add(final Quantity otherQuantity) {
53-
if (_unit.isPresent() != otherQuantity._unit.isPresent()) {
47+
if (_unit.isPresent() != otherQuantity.getUnit().isPresent()) {
5448
throw new IllegalStateException(String.format(
5549
"Units must both be present or absent; thisQuantity=%s otherQuantity=%s",
5650
this,
5751
otherQuantity));
5852
}
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);
6155
}
6256
final Unit smallerUnit = _unit.get().getSmallerUnit(otherQuantity.getUnit().get());
63-
return new Quantity(
57+
return new DefaultQuantity(
6458
smallerUnit.convert(_value, _unit.get())
65-
+ smallerUnit.convert(otherQuantity._value, otherQuantity._unit.get()),
59+
+ smallerUnit.convert(otherQuantity.getValue(), otherQuantity.getUnit().get()),
6660
Optional.of(smallerUnit));
6761
}
6862

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
7764
public Quantity subtract(final Quantity otherQuantity) {
78-
if (_unit.isPresent() != otherQuantity._unit.isPresent()) {
65+
if (_unit.isPresent() != otherQuantity.getUnit().isPresent()) {
7966
throw new IllegalStateException(String.format(
8067
"Units must both be present or absent; thisQuantity=%s otherQuantity=%s",
8168
this,
8269
otherQuantity));
8370
}
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);
8673
}
8774
final Unit smallerUnit = _unit.get().getSmallerUnit(otherQuantity.getUnit().get());
88-
return new Quantity(
75+
return new DefaultQuantity(
8976
smallerUnit.convert(_value, _unit.get())
90-
- smallerUnit.convert(otherQuantity._value, otherQuantity._unit.get()),
77+
- smallerUnit.convert(otherQuantity.getValue(), otherQuantity.getUnit().get()),
9178
Optional.of(smallerUnit));
9279
}
9380

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
10182
public Quantity multiply(final Quantity otherQuantity) {
102-
if (_unit.isPresent() && otherQuantity._unit.isPresent()) {
83+
if (_unit.isPresent() && otherQuantity.getUnit().isPresent()) {
10384
throw new UnsupportedOperationException("Compound units not supported yet");
10485
}
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))));
10889
}
10990

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
11792
public Quantity divide(final Quantity otherQuantity) {
11893
// TODO(vkoskela): Support division by quantity with unit [2F].
119-
if (otherQuantity._unit.isPresent()) {
94+
if (otherQuantity.getUnit().isPresent()) {
12095
throw new UnsupportedOperationException("Compound units not supported yet");
12196
}
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());
12499
}
125-
return new Quantity(
126-
_value / otherQuantity._value,
100+
return new DefaultQuantity(
101+
_value / otherQuantity.getValue(),
127102
_unit);
128103
}
129104

130105
@Override
131106
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());
134109
}
135110
throw new IllegalArgumentException(String.format(
136111
"Cannot compare mismatched units; this=%s, other=%s",
@@ -148,14 +123,14 @@ public boolean equals(final Object o) {
148123
if (this == o) {
149124
return true;
150125
}
151-
if (!(o instanceof Quantity)) {
126+
if (!(o instanceof DefaultQuantity)) {
152127
return false;
153128
}
154129

155-
final Quantity sample = (Quantity) o;
130+
final DefaultQuantity sample = (DefaultQuantity) o;
156131

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());
159134
}
160135

161136
@Override
@@ -167,11 +142,11 @@ public String toString() {
167142
.toString();
168143
}
169144

170-
private Quantity(final Builder builder) {
145+
private DefaultQuantity(final Builder builder) {
171146
this(builder._value, Optional.ofNullable(builder._unit));
172147
}
173148

174-
private Quantity(final double value, final Optional<Unit> unit) {
149+
private DefaultQuantity(final double value, final Optional<Unit> unit) {
175150
_value = value;
176151
_unit = unit;
177152
}
@@ -185,24 +160,24 @@ private Quantity(final double value, final Optional<Unit> unit) {
185160
/**
186161
* <code>Builder</code> implementation for <code>Quantity</code>.
187162
*/
188-
public static final class Builder extends ThreadLocalBuilder<Quantity> {
163+
public static final class Builder extends ThreadLocalBuilder<DefaultQuantity> {
189164

190165
/**
191166
* Public constructor.
192167
*/
193168
public Builder() {
194-
super((java.util.function.Function<Builder, Quantity>) Quantity::new);
169+
super((java.util.function.Function<Builder, DefaultQuantity>) DefaultQuantity::new);
195170
}
196171

197172
/**
198173
* Public constructor.
199174
*
200175
* @param quantity the <code>Quantity</code> to initialize from
201176
*/
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);
206181
}
207182

208183
/**
@@ -228,9 +203,9 @@ public Builder setUnit(final Unit value) {
228203
}
229204

230205
@Override
231-
public Quantity build() {
206+
public DefaultQuantity build() {
232207
normalize();
233-
return new Quantity(this);
208+
return new DefaultQuantity(this);
234209
}
235210

236211
@Override

src/main/java/com/arpnetworking/metrics/mad/model/Metric.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
*/
1616
package com.arpnetworking.metrics.mad.model;
1717

18-
import com.arpnetworking.tsdcore.model.MetricType;
19-
import com.arpnetworking.tsdcore.model.Quantity;
20-
2118
import java.util.List;
2219

2320
/**

src/main/java/com/arpnetworking/tsdcore/model/MetricType.java renamed to src/main/java/com/arpnetworking/metrics/mad/model/MetricType.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package com.arpnetworking.tsdcore.model;
16+
package com.arpnetworking.metrics.mad.model;
1717

1818
/**
1919
* The type of data.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2019 Dropbox
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package com.arpnetworking.metrics.mad.model;
17+
18+
import java.io.Serializable;
19+
import java.util.Optional;
20+
21+
/**
22+
* Represents a sample.
23+
*
24+
* @author Brandon Arp (brandon dot arp at inscopemetrics dot com)
25+
*/
26+
public interface Quantity extends Comparable<Quantity>, Serializable {
27+
28+
/**
29+
* Access the {@link Quantity} value.
30+
*
31+
* @return the {@link Quantity} value
32+
*/
33+
double getValue();
34+
35+
/**
36+
* Access the {@link Quantity} {@link Unit}.
37+
*
38+
* @return the {@link Quantity} {@link Unit}
39+
*/
40+
Optional<Unit> getUnit();
41+
42+
/**
43+
* Add this <code>Quantity</code> to the specified one returning the
44+
* result. Both <code>Quantity</code> instances must either not have a
45+
* <code>Unit</code> or the <code>Unit</code> must be of the same type.
46+
*
47+
* @param otherQuantity The other <code>Quantity</code>.
48+
* @return The resulting sum <code>Quantity</code>.
49+
*/
50+
Quantity add(Quantity otherQuantity);
51+
52+
/**
53+
* Subtract the specified <code>Quantity</code> from this one returning
54+
* the result. Both <code>Quantity</code> instances must either not have
55+
* a <code>Unit</code> or the <code>Unit</code> must be of the same type.
56+
*
57+
* @param otherQuantity The other <code>Quantity</code>.
58+
* @return The resulting difference <code>Quantity</code>.
59+
*/
60+
Quantity subtract(Quantity otherQuantity);
61+
62+
/**
63+
* Multiply this <code>Quantity</code> with the specified one returning
64+
* the result.
65+
*
66+
* @param otherQuantity The other <code>Quantity</code>.
67+
* @return The resulting product <code>Quantity</code>.
68+
*/
69+
Quantity multiply(Quantity otherQuantity);
70+
71+
/**
72+
* Divide this <code>Quantity</code> by the specified one returning
73+
* the result.
74+
*
75+
* @param otherQuantity The other <code>Quantity</code>.
76+
* @return The resulting quotient <code>Quantity</code>.
77+
*/
78+
Quantity divide(Quantity otherQuantity);
79+
}

0 commit comments

Comments
 (0)