aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/src/com/yahoo/jrt/Value.java
blob: 4a6a8bd1426c9a52b5f83151d11b3b3fe580db54 (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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


import com.yahoo.text.Utf8Array;

import java.nio.ByteBuffer;


/**
 * <p>A single value that may be either a parameter or a return value
 * associated with a {@link Request}. Multiple values are bundled
 * together with the {@link Values} class. The value type identifiers
 * are defined by the RPC protocol. Each identifier matches the value
 * of an ASCII character (listed after the Java class for the type).</p>
 *
 * <p>Most Value subclasses that are constructed from a Java array
 * will not copy the array. This enables the same data to back
 * multiple Value objects, but it also means that the application
 * should be careful not to change the backing data under the feet of
 * a Value object.</p>
 **/
public abstract class Value
{
    /** type identifier for {@link Int8Value} (b) **/
    public static final byte INT8         = 'b';

    /** type identifier for {@link Int8Array} (B) **/
    public static final byte INT8_ARRAY   = 'B';

    /** type identifier for {@link Int16Value} (h) **/
    public static final byte INT16        = 'h';

    /** type identifier for {@link Int16Array} (H) **/
    public static final byte INT16_ARRAY  = 'H';

    /** type identifier for {@link Int32Value} (i) **/
    public static final byte INT32        = 'i';

    /** type identifier for {@link Int32Array} (I) **/
    public static final byte INT32_ARRAY  = 'I';

    /** type identifier for {@link Int64Value} (l) **/
    public static final byte INT64        = 'l';

    /** type identifier for {@link Int64Array} (L) **/
    public static final byte INT64_ARRAY  = 'L';

    /** type identifier for {@link FloatValue} (f) **/
    public static final byte FLOAT        = 'f';

    /** type identifier for {@link FloatArray} (F) **/
    public static final byte FLOAT_ARRAY  = 'F';

    /** type identifier for {@link DoubleValue} (d) **/
    public static final byte DOUBLE       = 'd';

    /** type identifier for {@link DoubleArray} (D) **/
    public static final byte DOUBLE_ARRAY = 'D';

    /** type identifier for {@link StringValue} (s) **/
    public static final byte STRING       = 's';

    /** type identifier for {@link StringArray} (S) **/
    public static final byte STRING_ARRAY = 'S';

    /** type identifier for {@link DataValue} (x) **/
    public static final byte DATA         = 'x';

    /** type identifier for {@link DataArray} (X) **/
    public static final byte DATA_ARRAY   = 'X';

    /**
     * Obtain the type identifier for this value
     *
     * @return type identifier
     **/
    public abstract byte type();

    /**
     * Obtain the number of entries stored in this value. This is 1
     * for basic data types and the size of the array for array types.
     *
     * @return the number of entries stored in this value
     **/
    public abstract int count();

    /**
     * Determine the number of bytes needed to store this value when
     * encoded into a buffer
     *
     * @return number of bytes needed for encoding this value
     **/
    abstract int bytes();

    /**
     * Encode this value into the given buffer
     *
     * @param dst where to encode this value
     **/
    abstract void encode(ByteBuffer dst);

    /**
     * Decode a value from the given buffer. This method also acts as
     * a factory for value objects
     *
     * @return the decoded value
     * @param type value type identifier
     * @param src where the value is stored
     * @throws IllegalArgumentException if the given type identifier is illegal
     **/
    static Value decode(byte type, ByteBuffer src) {
        switch (type) {
        case INT8:         return new Int8Value(src);
        case INT8_ARRAY:   return new Int8Array(src);
        case INT16:        return new Int16Value(src);
        case INT16_ARRAY:  return new Int16Array(src);
        case INT32:        return new Int32Value(src);
        case INT32_ARRAY:  return new Int32Array(src);
        case INT64:        return new Int64Value(src);
        case INT64_ARRAY:  return new Int64Array(src);
        case FLOAT:        return new FloatValue(src);
        case FLOAT_ARRAY:  return new FloatArray(src);
        case DOUBLE:       return new DoubleValue(src);
        case DOUBLE_ARRAY: return new DoubleArray(src);
        case STRING:       return new StringValue(src);
        case STRING_ARRAY: return new StringArray(src);
        case DATA:         return new DataValue(src);
        case DATA_ARRAY:   return new DataArray(src);
        }
        throw new IllegalArgumentException();
    }

    /**
     * Interpret this value as a {@link Int8Value} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int8Value}
     **/
    public byte     asInt8()        { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int8Array} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int8Array}
     **/
    public byte[]   asInt8Array()   { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int16Value} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int16Value}
     **/
    public short    asInt16()       { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int16Array} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int16Array}
     **/
    public short[]  asInt16Array()  { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int32Value} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int32Value}
     **/
    public int      asInt32()       { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int32Array} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int32Array}
     **/
    public int[]    asInt32Array()  { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int64Value} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int64Value}
     **/
    public long     asInt64()       { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link Int64Array} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Int64Array}
     **/
    public long[]   asInt64Array()  { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link FloatValue} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link FloatValue}
     **/
    public float    asFloat()       { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link FloatArray} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link FloatArray}
     **/
    public float[]  asFloatArray()  { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link DoubleValue} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link DoubleValue}
     **/
    public double   asDouble()      { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link DoubleArray} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link DoubleArray}
     **/
    public double[] asDoubleArray() { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link StringValue} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link StringValue}
     **/
    public String   asString()      { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link StringValue} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link Utf8Array}
     **/
    public Utf8Array asUtf8Array() { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link StringArray} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link StringArray}
     **/
    public String[] asStringArray() { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link DataValue} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link DataValue}
     **/
    public byte[]   asData()        { throw new ClassCastException(); }

    /**
     * Interpret this value as a {@link DataArray} and return the
     * contents as an appropriate Java type
     *
     * @return the value contained in this object as a Java type
     * @throws ClassCastException if this is not a {@link DataArray}
     **/
    public byte[][] asDataArray()   { throw new ClassCastException(); }

    /** Force a proper toString */
    public abstract @Override String toString();

}