1 // Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
2 package org.rocksdb;
3 
4 import static org.rocksdb.AbstractMutableOptions.INT_ARRAY_INT_SEPARATOR;
5 
6 public abstract class MutableOptionValue<T> {
7 
asDouble()8   abstract double asDouble() throws NumberFormatException;
asLong()9   abstract long asLong() throws NumberFormatException;
asInt()10   abstract int asInt() throws NumberFormatException;
asBoolean()11   abstract boolean asBoolean() throws IllegalStateException;
asIntArray()12   abstract int[] asIntArray() throws IllegalStateException;
asString()13   abstract String asString();
asObject()14   abstract T asObject();
15 
16   private static abstract class MutableOptionValueObject<T>
17       extends MutableOptionValue<T> {
18     protected final T value;
19 
MutableOptionValueObject(final T value)20     private MutableOptionValueObject(final T value) {
21       this.value = value;
22     }
23 
asObject()24     @Override T asObject() {
25       return value;
26     }
27   }
28 
fromString(final String s)29   static MutableOptionValue<String> fromString(final String s) {
30     return new MutableOptionStringValue(s);
31   }
32 
fromDouble(final double d)33   static MutableOptionValue<Double> fromDouble(final double d) {
34     return new MutableOptionDoubleValue(d);
35   }
36 
fromLong(final long d)37   static MutableOptionValue<Long> fromLong(final long d) {
38     return new MutableOptionLongValue(d);
39   }
40 
fromInt(final int i)41   static MutableOptionValue<Integer> fromInt(final int i) {
42     return new MutableOptionIntValue(i);
43   }
44 
fromBoolean(final boolean b)45   static MutableOptionValue<Boolean> fromBoolean(final boolean b) {
46     return new MutableOptionBooleanValue(b);
47   }
48 
fromIntArray(final int[] ix)49   static MutableOptionValue<int[]> fromIntArray(final int[] ix) {
50     return new MutableOptionIntArrayValue(ix);
51   }
52 
fromEnum(final N value)53   static <N extends Enum<N>> MutableOptionValue<N> fromEnum(final N value) {
54     return new MutableOptionEnumValue<>(value);
55   }
56 
57   static class MutableOptionStringValue
58       extends MutableOptionValueObject<String> {
MutableOptionStringValue(final String value)59     MutableOptionStringValue(final String value) {
60       super(value);
61     }
62 
63     @Override
asDouble()64     double asDouble() throws NumberFormatException {
65       return Double.parseDouble(value);
66     }
67 
68     @Override
asLong()69     long asLong() throws NumberFormatException {
70       return Long.parseLong(value);
71     }
72 
73     @Override
asInt()74     int asInt() throws NumberFormatException {
75       return Integer.parseInt(value);
76     }
77 
78     @Override
asBoolean()79     boolean asBoolean() throws IllegalStateException {
80       return Boolean.parseBoolean(value);
81     }
82 
83     @Override
asIntArray()84     int[] asIntArray() throws IllegalStateException {
85       throw new IllegalStateException("String is not applicable as int[]");
86     }
87 
88     @Override
asString()89     String asString() {
90       return value;
91     }
92   }
93 
94   static class MutableOptionDoubleValue
95       extends MutableOptionValue<Double> {
96     private final double value;
MutableOptionDoubleValue(final double value)97     MutableOptionDoubleValue(final double value) {
98       this.value = value;
99     }
100 
101     @Override
asDouble()102     double asDouble() {
103       return value;
104     }
105 
106     @Override
asLong()107     long asLong() throws NumberFormatException {
108       return Double.valueOf(value).longValue();
109     }
110 
111     @Override
asInt()112     int asInt() throws NumberFormatException {
113       if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
114         throw new NumberFormatException(
115             "double value lies outside the bounds of int");
116       }
117       return Double.valueOf(value).intValue();
118     }
119 
120     @Override
asBoolean()121     boolean asBoolean() throws IllegalStateException {
122       throw new IllegalStateException(
123           "double is not applicable as boolean");
124     }
125 
126     @Override
asIntArray()127     int[] asIntArray() throws IllegalStateException {
128       if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
129         throw new NumberFormatException(
130             "double value lies outside the bounds of int");
131       }
132       return new int[] { Double.valueOf(value).intValue() };
133     }
134 
135     @Override
asString()136     String asString() {
137       return String.valueOf(value);
138     }
139 
140     @Override
asObject()141     Double asObject() {
142       return value;
143     }
144   }
145 
146   static class MutableOptionLongValue
147       extends MutableOptionValue<Long> {
148     private final long value;
149 
MutableOptionLongValue(final long value)150     MutableOptionLongValue(final long value) {
151       this.value = value;
152     }
153 
154     @Override
asDouble()155     double asDouble() {
156       if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) {
157         throw new NumberFormatException(
158             "long value lies outside the bounds of int");
159       }
160       return Long.valueOf(value).doubleValue();
161     }
162 
163     @Override
asLong()164     long asLong() throws NumberFormatException {
165       return value;
166     }
167 
168     @Override
asInt()169     int asInt() throws NumberFormatException {
170       if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
171         throw new NumberFormatException(
172             "long value lies outside the bounds of int");
173       }
174       return Long.valueOf(value).intValue();
175     }
176 
177     @Override
asBoolean()178     boolean asBoolean() throws IllegalStateException {
179       throw new IllegalStateException(
180           "long is not applicable as boolean");
181     }
182 
183     @Override
asIntArray()184     int[] asIntArray() throws IllegalStateException {
185       if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
186         throw new NumberFormatException(
187             "long value lies outside the bounds of int");
188       }
189       return new int[] { Long.valueOf(value).intValue() };
190     }
191 
192     @Override
asString()193     String asString() {
194       return String.valueOf(value);
195     }
196 
197     @Override
asObject()198     Long asObject() {
199       return value;
200     }
201   }
202 
203   static class MutableOptionIntValue
204       extends MutableOptionValue<Integer> {
205     private final int value;
206 
MutableOptionIntValue(final int value)207     MutableOptionIntValue(final int value) {
208       this.value = value;
209     }
210 
211     @Override
asDouble()212     double asDouble() {
213       if(value > Double.MAX_VALUE || value < Double.MIN_VALUE) {
214         throw new NumberFormatException("int value lies outside the bounds of int");
215       }
216       return Integer.valueOf(value).doubleValue();
217     }
218 
219     @Override
asLong()220     long asLong() throws NumberFormatException {
221       return value;
222     }
223 
224     @Override
asInt()225     int asInt() throws NumberFormatException {
226       return value;
227     }
228 
229     @Override
asBoolean()230     boolean asBoolean() throws IllegalStateException {
231       throw new IllegalStateException("int is not applicable as boolean");
232     }
233 
234     @Override
asIntArray()235     int[] asIntArray() throws IllegalStateException {
236       return new int[] { value };
237     }
238 
239     @Override
asString()240     String asString() {
241       return String.valueOf(value);
242     }
243 
244     @Override
asObject()245     Integer asObject() {
246       return value;
247     }
248   }
249 
250   static class MutableOptionBooleanValue
251       extends MutableOptionValue<Boolean> {
252     private final boolean value;
253 
MutableOptionBooleanValue(final boolean value)254     MutableOptionBooleanValue(final boolean value) {
255       this.value = value;
256     }
257 
258     @Override
asDouble()259     double asDouble() {
260       throw new NumberFormatException("boolean is not applicable as double");
261     }
262 
263     @Override
asLong()264     long asLong() throws NumberFormatException {
265       throw new NumberFormatException("boolean is not applicable as Long");
266     }
267 
268     @Override
asInt()269     int asInt() throws NumberFormatException {
270       throw new NumberFormatException("boolean is not applicable as int");
271     }
272 
273     @Override
asBoolean()274     boolean asBoolean() {
275       return value;
276     }
277 
278     @Override
asIntArray()279     int[] asIntArray() throws IllegalStateException {
280       throw new IllegalStateException("boolean is not applicable as int[]");
281     }
282 
283     @Override
asString()284     String asString() {
285       return String.valueOf(value);
286     }
287 
288     @Override
asObject()289     Boolean asObject() {
290       return value;
291     }
292   }
293 
294   static class MutableOptionIntArrayValue
295       extends MutableOptionValueObject<int[]> {
MutableOptionIntArrayValue(final int[] value)296     MutableOptionIntArrayValue(final int[] value) {
297       super(value);
298     }
299 
300     @Override
asDouble()301     double asDouble() {
302       throw new NumberFormatException("int[] is not applicable as double");
303     }
304 
305     @Override
asLong()306     long asLong() throws NumberFormatException {
307       throw new NumberFormatException("int[] is not applicable as Long");
308     }
309 
310     @Override
asInt()311     int asInt() throws NumberFormatException {
312       throw new NumberFormatException("int[] is not applicable as int");
313     }
314 
315     @Override
asBoolean()316     boolean asBoolean() {
317       throw new NumberFormatException("int[] is not applicable as boolean");
318     }
319 
320     @Override
asIntArray()321     int[] asIntArray() throws IllegalStateException {
322       return value;
323     }
324 
325     @Override
asString()326     String asString() {
327       final StringBuilder builder = new StringBuilder();
328       for(int i = 0; i < value.length; i++) {
329         builder.append(i);
330         if(i + 1 < value.length) {
331           builder.append(INT_ARRAY_INT_SEPARATOR);
332         }
333       }
334       return builder.toString();
335     }
336   }
337 
338   static class MutableOptionEnumValue<T extends Enum<T>>
339       extends MutableOptionValueObject<T> {
340 
MutableOptionEnumValue(final T value)341     MutableOptionEnumValue(final T value) {
342       super(value);
343     }
344 
345     @Override
asDouble()346     double asDouble() throws NumberFormatException {
347       throw new NumberFormatException("Enum is not applicable as double");
348     }
349 
350     @Override
asLong()351     long asLong() throws NumberFormatException {
352       throw new NumberFormatException("Enum is not applicable as long");
353     }
354 
355     @Override
asInt()356     int asInt() throws NumberFormatException {
357       throw new NumberFormatException("Enum is not applicable as int");
358     }
359 
360     @Override
asBoolean()361     boolean asBoolean() throws IllegalStateException {
362       throw new NumberFormatException("Enum is not applicable as boolean");
363     }
364 
365     @Override
asIntArray()366     int[] asIntArray() throws IllegalStateException {
367       throw new NumberFormatException("Enum is not applicable as int[]");
368     }
369 
370     @Override
asString()371     String asString() {
372       return value.name();
373     }
374   }
375 
376 }
377