org.forstdb.MutableOptionValue Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of forstjni Show documentation
Show all versions of forstjni Show documentation
ForSt fat jar with modifications specific for Apache Flink that contains .so files for linux32 and linux64 (glibc and musl-libc), jnilib files
for Mac OSX, and a .dll for Windows x64.
// Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
package org.forstdb;
import static org.forstdb.AbstractMutableOptions.INT_ARRAY_INT_SEPARATOR;
public abstract class MutableOptionValue {
abstract double asDouble() throws NumberFormatException;
abstract long asLong() throws NumberFormatException;
abstract int asInt() throws NumberFormatException;
abstract boolean asBoolean() throws IllegalStateException;
abstract int[] asIntArray() throws IllegalStateException;
abstract String asString();
abstract T asObject();
private abstract static class MutableOptionValueObject extends MutableOptionValue {
protected final T value;
protected MutableOptionValueObject(final T value) {
this.value = value;
}
@Override T asObject() {
return value;
}
}
static MutableOptionValue fromString(final String s) {
return new MutableOptionStringValue(s);
}
static MutableOptionValue fromDouble(final double d) {
return new MutableOptionDoubleValue(d);
}
static MutableOptionValue fromLong(final long d) {
return new MutableOptionLongValue(d);
}
static MutableOptionValue fromInt(final int i) {
return new MutableOptionIntValue(i);
}
static MutableOptionValue fromBoolean(final boolean b) {
return new MutableOptionBooleanValue(b);
}
static MutableOptionValue fromIntArray(final int[] ix) {
return new MutableOptionIntArrayValue(ix);
}
static > MutableOptionValue fromEnum(final N value) {
return new MutableOptionEnumValue<>(value);
}
static class MutableOptionStringValue
extends MutableOptionValueObject {
MutableOptionStringValue(final String value) {
super(value);
}
@Override
double asDouble() throws NumberFormatException {
return Double.parseDouble(value);
}
@Override
long asLong() throws NumberFormatException {
return Long.parseLong(value);
}
@Override
int asInt() throws NumberFormatException {
return Integer.parseInt(value);
}
@Override
boolean asBoolean() throws IllegalStateException {
return Boolean.parseBoolean(value);
}
@Override
int[] asIntArray() throws IllegalStateException {
throw new IllegalStateException("String is not applicable as int[]");
}
@Override
String asString() {
return value;
}
}
static class MutableOptionDoubleValue
extends MutableOptionValue {
private final double value;
MutableOptionDoubleValue(final double value) {
this.value = value;
}
@Override
double asDouble() {
return value;
}
@Override
long asLong() throws NumberFormatException {
return Double.valueOf(value).longValue();
}
@Override
int asInt() throws NumberFormatException {
if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
throw new NumberFormatException(
"double value lies outside the bounds of int");
}
return Double.valueOf(value).intValue();
}
@Override
boolean asBoolean() throws IllegalStateException {
throw new IllegalStateException(
"double is not applicable as boolean");
}
@Override
int[] asIntArray() throws IllegalStateException {
if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
throw new NumberFormatException(
"double value lies outside the bounds of int");
}
return new int[] { Double.valueOf(value).intValue() };
}
@Override
String asString() {
return String.valueOf(value);
}
@Override
Double asObject() {
return value;
}
}
static class MutableOptionLongValue
extends MutableOptionValue {
private final long value;
MutableOptionLongValue(final long value) {
this.value = value;
}
@Override
double asDouble() {
return Long.valueOf(value).doubleValue();
}
@Override
long asLong() throws NumberFormatException {
return value;
}
@Override
int asInt() throws NumberFormatException {
if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
throw new NumberFormatException(
"long value lies outside the bounds of int");
}
return Long.valueOf(value).intValue();
}
@Override
boolean asBoolean() throws IllegalStateException {
throw new IllegalStateException(
"long is not applicable as boolean");
}
@Override
int[] asIntArray() throws IllegalStateException {
if(value > Integer.MAX_VALUE || value < Integer.MIN_VALUE) {
throw new NumberFormatException(
"long value lies outside the bounds of int");
}
return new int[] { Long.valueOf(value).intValue() };
}
@Override
String asString() {
return String.valueOf(value);
}
@Override
Long asObject() {
return value;
}
}
static class MutableOptionIntValue
extends MutableOptionValue {
private final int value;
MutableOptionIntValue(final int value) {
this.value = value;
}
@Override
double asDouble() {
return Integer.valueOf(value).doubleValue();
}
@Override
long asLong() throws NumberFormatException {
return value;
}
@Override
int asInt() throws NumberFormatException {
return value;
}
@Override
boolean asBoolean() throws IllegalStateException {
throw new IllegalStateException("int is not applicable as boolean");
}
@Override
int[] asIntArray() throws IllegalStateException {
return new int[] { value };
}
@Override
String asString() {
return String.valueOf(value);
}
@Override
Integer asObject() {
return value;
}
}
static class MutableOptionBooleanValue
extends MutableOptionValue {
private final boolean value;
MutableOptionBooleanValue(final boolean value) {
this.value = value;
}
@Override
double asDouble() {
throw new NumberFormatException("boolean is not applicable as double");
}
@Override
long asLong() throws NumberFormatException {
throw new NumberFormatException("boolean is not applicable as Long");
}
@Override
int asInt() throws NumberFormatException {
throw new NumberFormatException("boolean is not applicable as int");
}
@Override
boolean asBoolean() {
return value;
}
@Override
int[] asIntArray() throws IllegalStateException {
throw new IllegalStateException("boolean is not applicable as int[]");
}
@Override
String asString() {
return String.valueOf(value);
}
@Override
Boolean asObject() {
return value;
}
}
static class MutableOptionIntArrayValue
extends MutableOptionValueObject {
MutableOptionIntArrayValue(final int[] value) {
super(value);
}
@Override
double asDouble() {
throw new NumberFormatException("int[] is not applicable as double");
}
@Override
long asLong() throws NumberFormatException {
throw new NumberFormatException("int[] is not applicable as Long");
}
@Override
int asInt() throws NumberFormatException {
throw new NumberFormatException("int[] is not applicable as int");
}
@Override
boolean asBoolean() {
throw new NumberFormatException("int[] is not applicable as boolean");
}
@Override
int[] asIntArray() throws IllegalStateException {
return value;
}
@Override
String asString() {
final StringBuilder builder = new StringBuilder();
for(int i = 0; i < value.length; i++) {
builder.append(value[i]);
if(i + 1 < value.length) {
builder.append(INT_ARRAY_INT_SEPARATOR);
}
}
return builder.toString();
}
}
static class MutableOptionEnumValue>
extends MutableOptionValueObject {
MutableOptionEnumValue(final T value) {
super(value);
}
@Override
double asDouble() throws NumberFormatException {
throw new NumberFormatException("Enum is not applicable as double");
}
@Override
long asLong() throws NumberFormatException {
throw new NumberFormatException("Enum is not applicable as long");
}
@Override
int asInt() throws NumberFormatException {
throw new NumberFormatException("Enum is not applicable as int");
}
@Override
boolean asBoolean() throws IllegalStateException {
throw new NumberFormatException("Enum is not applicable as boolean");
}
@Override
int[] asIntArray() throws IllegalStateException {
throw new NumberFormatException("Enum is not applicable as int[]");
}
@Override
String asString() {
return value.name();
}
}
}