aboutsummaryrefslogtreecommitdiffstats
path: root/container-core/src/main/java/com/yahoo/metrics/simple/Value.java
blob: 56af52f0d2deae5712bc1f041a88b6811ddf3236 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.metrics.simple;

/**
 * Wrapper for dimension values.
 *
 * @author steinar
 */
public abstract class Value {
    private static final String UNSUPPORTED_VALUE_TYPE = "Unsupported value type.";

    /**
     * Marker for the type of the contained value of a Value instance.
     */
    public enum Discriminator {
        LONG, DOUBLE, STRING;
    }

    /**
     * Get the long wrapped by a Value if one exists.
     *
     * @throws UnsupportedOperationException if LONG is not returned by {{@link #getType()}.
     */
    public long longValue() throws UnsupportedOperationException {
        throw new UnsupportedOperationException(UNSUPPORTED_VALUE_TYPE);
    }

    /**
     * Get the double wrapped by a Value if one exists.
     *
     * @throws UnsupportedOperationException if DOUBLE is not returned by {{@link #getType()}.
     */
    public double doubleValue() throws UnsupportedOperationException {
        throw new UnsupportedOperationException(UNSUPPORTED_VALUE_TYPE);
    }

    /**
     * Get the string wrapped by a Value if one exists.
     *
     * @throws UnsupportedOperationException if STRING is not returned by {{@link #getType()}.
     */
    public String stringValue() throws UnsupportedOperationException {
        throw new UnsupportedOperationException(UNSUPPORTED_VALUE_TYPE);
    }

    /**
     * Show the (single) supported standard type representation of a Value instance.
     */
    public abstract Discriminator getType();

    private static class LongValue extends Value {
        private final long value;

        LongValue(long value) {
            this.value = value;
        }

        @Override
        public long longValue() {
            return value;
        }

        @Override
        public Discriminator getType() {
            return Discriminator.LONG;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + (int) (value ^ (value >>> 32));
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            LongValue other = (LongValue) obj;
            if (value != other.value) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("LongValue [value=").append(value).append("]");
            return builder.toString();
        }
    }

    private static class DoubleValue extends Value {
        private final double value;

        DoubleValue(double value) {
            this.value = value;
        }

        @Override
        public double doubleValue() {
            return value;
        }

        @Override
        public Discriminator getType() {
            return Discriminator.DOUBLE;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            long temp;
            temp = Double.doubleToLongBits(value);
            result = prime * result + (int) (temp ^ (temp >>> 32));
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            DoubleValue other = (DoubleValue) obj;
            if (Double.doubleToLongBits(value) != Double.doubleToLongBits(other.value)) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("DoubleValue [value=").append(value).append("]");
            return builder.toString();
        }
    }

    private static class StringValue extends Value {
        private final String value;

        StringValue(String value) {
            this.value = value;
        }

        @Override
        public String stringValue() {
            return value;
        }

        @Override
        public Discriminator getType() {
            return Discriminator.STRING;
        }

        @Override
        public int hashCode() {
            final int prime = 31;
            int result = 1;
            result = prime * result + ((value == null) ? 0 : value.hashCode());
            return result;
        }

        @Override
        public boolean equals(Object obj) {
            if (this == obj) {
                return true;
            }
            if (obj == null) {
                return false;
            }
            if (getClass() != obj.getClass()) {
                return false;
            }
            StringValue other = (StringValue) obj;
            if (value == null) {
                if (other.value != null) {
                    return false;
                }
            } else if (!value.equals(other.value)) {
                return false;
            }
            return true;
        }

        @Override
        public String toString() {
            StringBuilder builder = new StringBuilder();
            builder.append("StringValue [value=").append(value).append("]");
            return builder.toString();
        }
    }

    /**
     * Helper method to wrap a long as a Value. The instance returned may or may
     * not be unique.
     *
     * @param value
     *            the value to wrap
     * @return an immutable wrapper
     */
    public static Value of(long value) {
        return new LongValue(value);
    }

    /**
     * Helper method to wrap a double as a Value. The instance returned may or
     * may not be unique.
     *
     * @param value
     *            the value to wrap
     * @return an immutable wrapper
     * */
    public static Value of(double value) {
        return new DoubleValue(value);
    }

    /**
     * Helper method to wrap a string as a Value. The instance returned may or
     * may not be unique.
     *
     * @param value
     *            the value to wrap
     * @return an immutable wrapper
     */
    public static Value of(String value) {
        return new StringValue(value);
    }

}