1 /* 2 * Copyright (c) 2018, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 public inline class Value { 25 char char_v; 26 byte byte_v; 27 boolean boolean_v; 28 int int_v; 29 short short_v; 30 long long_v; 31 double double_v; 32 float float_v; 33 Number number_v; 34 Point point_v; 35 Object ref_v; 36 37 Value() { 38 char_v = 'z'; 39 byte_v = 0; 40 boolean_v = true; 41 int_v = 1; 42 short_v = 2; 43 long_v = 3; 44 float_v = 0.1f; 45 double_v = 0.2d; 46 number_v = null; 47 point_v = Point.makePoint(0,0); 48 ref_v = null; 49 } 50 static Value makeValue(char c, boolean z, byte b, int x, short y, long l, float f, double d, Number number, Point p, Object o) { 51 Value v = Value.default; 52 v = __WithField(v.char_v, c); 53 v = __WithField(v.byte_v, b); 54 v = __WithField(v.boolean_v, z); 55 v = __WithField(v.int_v, x); 56 v = __WithField(v.short_v, y); 57 v = __WithField(v.long_v, l); 58 v = __WithField(v.float_v, f); 59 v = __WithField(v.double_v, d); 60 v = __WithField(v.number_v, number); 61 v = __WithField(v.point_v, p); 62 v = __WithField(v.ref_v, o); 63 return v; 64 } 65 66 static class Builder { 67 private char c; 68 private byte b; 69 private boolean z; 70 private int i; 71 private short s; 72 private long l; 73 private double d; 74 private float f; 75 private Number n; 76 private Point p = Point.makePoint(0,0); 77 private Object ref; 78 79 public Builder() {} 80 Builder setChar(char c) { 81 this.c = c; 82 return this; 83 } 84 Builder setByte(byte b) { 85 this.b = b; 86 return this; 87 } 88 Builder setBoolean(boolean z) { 89 this.z = z; 90 return this; 91 } 92 Builder setInt(int i) { 93 this.i = i; 94 return this; 95 } 96 Builder setShort(short s) { 97 this.s = s; 98 return this; 99 } 100 Builder setLong(long l) { 101 this.l = l; 102 return this; 103 } 104 Builder setDouble(double d) { 105 this.d = d; 106 return this; 107 } 108 Builder setFloat(float f) { 109 this.f = f; 110 return this; 111 } 112 Builder setNumber(Number n) { 113 this.n = n; 114 return this; 115 } 116 Builder setPoint(Point p) { 117 this.p = p; 118 return this; 119 } 120 Builder setReference(Object o) { 121 this.ref = o; 122 return this; 123 } 124 Value build() { 125 return Value.makeValue(c, z, b, i, s, l, f, d, n, p, ref); 126 } 127 } 128 129 interface Number { 130 default int intValue() { 131 throw new UnsupportedOperationException(); 132 } 133 default short shortValue() { 134 throw new UnsupportedOperationException(); 135 } 136 137 static IntValue intValue(int i) { 138 IntValue v = IntValue.default; 139 v = __WithField(v.i, i); 140 return v; 141 } 142 143 static ShortValue shortValue(short s) { 144 ShortValue v = ShortValue.default; 145 v = __WithField(v.s, s); 146 return v; 147 } 148 } 149 150 static inline class IntValue implements Number { 151 int i; 152 IntValue() { 153 i = 0; 154 } 155 public int intValue() { 156 return i; 157 } 158 } 159 160 static inline class ShortValue implements Number { 161 short s; 162 ShortValue() { 163 s = 0; 164 } 165 public short shortValue() { 166 return s; 167 } 168 } 169 170 static class IntNumber implements Number { 171 final int i; 172 public IntNumber(int i) { 173 this.i = i; 174 } 175 176 public int intValue() { 177 return i; 178 } 179 180 @Override 181 public String toString() { 182 return Integer.toString(i); 183 } 184 } 185 }