1 /* 2 * Copyright (C) 2018 The Android Open Source Project 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 17 package com.google.android.textclassifier; 18 19 /** 20 * Represents a union of different basic types. 21 * 22 * @hide 23 */ 24 public final class NamedVariant { 25 public static final int TYPE_EMPTY = 0; 26 public static final int TYPE_INT = 1; 27 public static final int TYPE_LONG = 2; 28 public static final int TYPE_FLOAT = 3; 29 public static final int TYPE_DOUBLE = 4; 30 public static final int TYPE_BOOL = 5; 31 public static final int TYPE_STRING = 6; 32 public static final int TYPE_STRING_ARRAY = 7; 33 public static final int TYPE_FLOAT_ARRAY = 8; 34 public static final int TYPE_INT_ARRAY = 9; 35 public static final int TYPE_NAMED_VARIANT_ARRAY = 10; 36 37 private final String name; 38 private final int type; 39 private int intValue; 40 private long longValue; 41 private float floatValue; 42 private double doubleValue; 43 private boolean boolValue; 44 private String stringValue; 45 private String[] stringArrValue; 46 private float[] floatArrValue; 47 private int[] intArrValue; 48 private NamedVariant[] namedVariantArray; 49 NamedVariant(String name, int value)50 public NamedVariant(String name, int value) { 51 this.name = name; 52 this.intValue = value; 53 this.type = TYPE_INT; 54 } 55 NamedVariant(String name, long value)56 public NamedVariant(String name, long value) { 57 this.name = name; 58 this.longValue = value; 59 this.type = TYPE_LONG; 60 } 61 NamedVariant(String name, float value)62 public NamedVariant(String name, float value) { 63 this.name = name; 64 this.floatValue = value; 65 this.type = TYPE_FLOAT; 66 } 67 NamedVariant(String name, double value)68 public NamedVariant(String name, double value) { 69 this.name = name; 70 this.doubleValue = value; 71 this.type = TYPE_DOUBLE; 72 } 73 NamedVariant(String name, boolean value)74 public NamedVariant(String name, boolean value) { 75 this.name = name; 76 this.boolValue = value; 77 this.type = TYPE_BOOL; 78 } 79 NamedVariant(String name, String value)80 public NamedVariant(String name, String value) { 81 this.name = name; 82 this.stringValue = value; 83 this.type = TYPE_STRING; 84 } 85 NamedVariant(String name, String[] value)86 public NamedVariant(String name, String[] value) { 87 this.name = name; 88 this.stringArrValue = value; 89 this.type = TYPE_STRING_ARRAY; 90 } 91 NamedVariant(String name, float[] value)92 public NamedVariant(String name, float[] value) { 93 this.name = name; 94 this.floatArrValue = value; 95 this.type = TYPE_FLOAT_ARRAY; 96 } 97 NamedVariant(String name, int[] value)98 public NamedVariant(String name, int[] value) { 99 this.name = name; 100 this.intArrValue = value; 101 this.type = TYPE_INT_ARRAY; 102 } 103 NamedVariant(String name, NamedVariant[] value)104 public NamedVariant(String name, NamedVariant[] value) { 105 this.name = name; 106 this.namedVariantArray = value; 107 this.type = TYPE_NAMED_VARIANT_ARRAY; 108 } 109 getName()110 public String getName() { 111 return name; 112 } 113 getType()114 public int getType() { 115 return type; 116 } 117 getInt()118 public int getInt() { 119 assert (type == TYPE_INT); 120 return intValue; 121 } 122 getLong()123 public long getLong() { 124 assert (type == TYPE_LONG); 125 return longValue; 126 } 127 getFloat()128 public float getFloat() { 129 assert (type == TYPE_FLOAT); 130 return floatValue; 131 } 132 getDouble()133 public double getDouble() { 134 assert (type == TYPE_DOUBLE); 135 return doubleValue; 136 } 137 getBool()138 public boolean getBool() { 139 assert (type == TYPE_BOOL); 140 return boolValue; 141 } 142 getString()143 public String getString() { 144 assert (type == TYPE_STRING); 145 return stringValue; 146 } 147 getStringArray()148 public String[] getStringArray() { 149 assert (type == TYPE_STRING_ARRAY); 150 return stringArrValue; 151 } 152 getFloatArray()153 public float[] getFloatArray() { 154 assert (type == TYPE_FLOAT_ARRAY); 155 return floatArrValue; 156 } 157 getIntArray()158 public int[] getIntArray() { 159 assert (type == TYPE_INT_ARRAY); 160 return intArrValue; 161 } 162 getNamedVariantArray()163 public NamedVariant[] getNamedVariantArray() { 164 assert (type == TYPE_NAMED_VARIANT_ARRAY); 165 return namedVariantArray; 166 } 167 } 168