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 NamedVariant(String name, int value)33 public NamedVariant(String name, int value) { 34 this.name = name; 35 this.intValue = value; 36 this.type = TYPE_INT; 37 } 38 NamedVariant(String name, long value)39 public NamedVariant(String name, long value) { 40 this.name = name; 41 this.longValue = value; 42 this.type = TYPE_LONG; 43 } 44 NamedVariant(String name, float value)45 public NamedVariant(String name, float value) { 46 this.name = name; 47 this.floatValue = value; 48 this.type = TYPE_FLOAT; 49 } 50 NamedVariant(String name, double value)51 public NamedVariant(String name, double value) { 52 this.name = name; 53 this.doubleValue = value; 54 this.type = TYPE_DOUBLE; 55 } 56 NamedVariant(String name, boolean value)57 public NamedVariant(String name, boolean value) { 58 this.name = name; 59 this.boolValue = value; 60 this.type = TYPE_BOOL; 61 } 62 NamedVariant(String name, String value)63 public NamedVariant(String name, String value) { 64 this.name = name; 65 this.stringValue = value; 66 this.type = TYPE_STRING; 67 } 68 getName()69 public String getName() { 70 return name; 71 } 72 getType()73 public int getType() { 74 return type; 75 } 76 getInt()77 public int getInt() { 78 assert (type == TYPE_INT); 79 return intValue; 80 } 81 getLong()82 public long getLong() { 83 assert (type == TYPE_LONG); 84 return longValue; 85 } 86 getFloat()87 public float getFloat() { 88 assert (type == TYPE_FLOAT); 89 return floatValue; 90 } 91 getDouble()92 public double getDouble() { 93 assert (type == TYPE_DOUBLE); 94 return doubleValue; 95 } 96 getBool()97 public boolean getBool() { 98 assert (type == TYPE_BOOL); 99 return boolValue; 100 } 101 getString()102 public String getString() { 103 assert (type == TYPE_STRING); 104 return stringValue; 105 } 106 107 private final String name; 108 private final int type; 109 private int intValue; 110 private long longValue; 111 private float floatValue; 112 private double doubleValue; 113 private boolean boolValue; 114 private String stringValue; 115 } 116