1 /* 2 * Copyright (c) 2010, 2017, 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 package javafx.beans.binding; 27 28 import java.util.Locale; 29 30 import javafx.beans.value.ObservableDoubleValue; 31 import javafx.beans.value.ObservableFloatValue; 32 import javafx.beans.value.ObservableIntegerValue; 33 import javafx.beans.value.ObservableLongValue; 34 import javafx.beans.value.ObservableNumberValue; 35 36 import com.sun.javafx.binding.StringFormatter; 37 38 /** 39 * {@code NumberExpressionBase} contains convenience methods to generate bindings in a fluent style, 40 * that are common to all NumberExpression subclasses. 41 * <p> 42 * NumberExpressionBase serves as a place for common code of specific NumberExpression subclasses for the 43 * specific number type. 44 * @see IntegerExpression 45 * @see LongExpression 46 * @see FloatExpression 47 * @see DoubleExpression 48 * @since JavaFX 2.0 49 */ 50 public abstract class NumberExpressionBase implements NumberExpression { 51 52 /** 53 * Returns an {@code NumberExpressionBase} that wraps a 54 * {@link javafx.beans.value.ObservableNumberValue}. If the 55 * {@code ObservableNumberValue} is already an instance of 56 * {@code NumberExpressionBase}, it will be returned. Otherwise a new 57 * {@link javafx.beans.binding.NumberBinding} is created that is bound to 58 * the {@code ObservableNumberValue}. 59 * 60 * @param <S> The type of Number to be wrapped 61 * @param value 62 * The source {@code ObservableNumberValue} 63 * @return An {@code NumberExpressionBase} that wraps the 64 * {@code ObservableNumberValue} if necessary 65 * @throws NullPointerException 66 * if {@code value} is {@code null} 67 */ 68 public static <S extends Number> NumberExpressionBase numberExpression( 69 final ObservableNumberValue value) { 70 if (value == null) { 71 throw new NullPointerException("Value must be specified."); 72 } 73 NumberExpressionBase result = (NumberExpressionBase) ((value instanceof NumberExpressionBase) ? value 74 : (value instanceof ObservableIntegerValue) ? IntegerExpression 75 .integerExpression((ObservableIntegerValue) value) 76 : (value instanceof ObservableDoubleValue) ? DoubleExpression 77 .doubleExpression((ObservableDoubleValue) value) 78 : (value instanceof ObservableFloatValue) ? FloatExpression 79 .floatExpression((ObservableFloatValue) value) 80 : (value instanceof ObservableLongValue) ? LongExpression 81 .longExpression((ObservableLongValue) value) 82 : null); 83 if (result != null) { 84 return result; 85 } else { 86 throw new IllegalArgumentException("Unsupported Type"); 87 } 88 } 89 90 @Override 91 public NumberBinding add(final ObservableNumberValue other) { 92 return Bindings.add(this, other); 93 } 94 95 @Override 96 public NumberBinding subtract(final ObservableNumberValue other) { 97 return Bindings.subtract(this, other); 98 } 99 100 @Override 101 public NumberBinding multiply(final ObservableNumberValue other) { 102 return Bindings.multiply(this, other); 103 } 104 105 @Override 106 public NumberBinding divide(final ObservableNumberValue other) { 107 return Bindings.divide(this, other); 108 } 109 110 // =============================================================== 111 // IsEqualTo 112 113 @Override 114 public BooleanBinding isEqualTo(final ObservableNumberValue other) { 115 return Bindings.equal(this, other); 116 } 117 118 @Override 119 public BooleanBinding isEqualTo(final ObservableNumberValue other, 120 double epsilon) { 121 return Bindings.equal(this, other, epsilon); 122 } 123 124 @Override 125 public BooleanBinding isEqualTo(final double other, double epsilon) { 126 return Bindings.equal(this, other, epsilon); 127 } 128 129 @Override 130 public BooleanBinding isEqualTo(final float other, double epsilon) { 131 return Bindings.equal(this, other, epsilon); 132 } 133 134 @Override 135 public BooleanBinding isEqualTo(final long other) { 136 return Bindings.equal(this, other); 137 } 138 139 @Override 140 public BooleanBinding isEqualTo(final long other, double epsilon) { 141 return Bindings.equal(this, other, epsilon); 142 } 143 144 @Override 145 public BooleanBinding isEqualTo(final int other) { 146 return Bindings.equal(this, other); 147 } 148 149 @Override 150 public BooleanBinding isEqualTo(final int other, double epsilon) { 151 return Bindings.equal(this, other, epsilon); 152 } 153 154 // =============================================================== 155 // IsNotEqualTo 156 157 @Override 158 public BooleanBinding isNotEqualTo(final ObservableNumberValue other) { 159 return Bindings.notEqual(this, other); 160 } 161 162 @Override 163 public BooleanBinding isNotEqualTo(final ObservableNumberValue other, 164 double epsilon) { 165 return Bindings.notEqual(this, other, epsilon); 166 } 167 168 @Override 169 public BooleanBinding isNotEqualTo(final double other, double epsilon) { 170 return Bindings.notEqual(this, other, epsilon); 171 } 172 173 @Override 174 public BooleanBinding isNotEqualTo(final float other, double epsilon) { 175 return Bindings.notEqual(this, other, epsilon); 176 } 177 178 @Override 179 public BooleanBinding isNotEqualTo(final long other) { 180 return Bindings.notEqual(this, other); 181 } 182 183 @Override 184 public BooleanBinding isNotEqualTo(final long other, double epsilon) { 185 return Bindings.notEqual(this, other, epsilon); 186 } 187 188 @Override 189 public BooleanBinding isNotEqualTo(final int other) { 190 return Bindings.notEqual(this, other); 191 } 192 193 @Override 194 public BooleanBinding isNotEqualTo(final int other, double epsilon) { 195 return Bindings.notEqual(this, other, epsilon); 196 } 197 198 // =============================================================== 199 // IsGreaterThan 200 201 @Override 202 public BooleanBinding greaterThan(final ObservableNumberValue other) { 203 return Bindings.greaterThan(this, other); 204 } 205 206 @Override 207 public BooleanBinding greaterThan(final double other) { 208 return Bindings.greaterThan(this, other); 209 } 210 211 @Override 212 public BooleanBinding greaterThan(final float other) { 213 return Bindings.greaterThan(this, other); 214 } 215 216 @Override 217 public BooleanBinding greaterThan(final long other) { 218 return Bindings.greaterThan(this, other); 219 } 220 221 @Override 222 public BooleanBinding greaterThan(final int other) { 223 return Bindings.greaterThan(this, other); 224 } 225 226 // =============================================================== 227 // IsLesserThan 228 229 @Override 230 public BooleanBinding lessThan(final ObservableNumberValue other) { 231 return Bindings.lessThan(this, other); 232 } 233 234 @Override 235 public BooleanBinding lessThan(final double other) { 236 return Bindings.lessThan(this, other); 237 } 238 239 @Override 240 public BooleanBinding lessThan(final float other) { 241 return Bindings.lessThan(this, other); 242 } 243 244 @Override 245 public BooleanBinding lessThan(final long other) { 246 return Bindings.lessThan(this, other); 247 } 248 249 @Override 250 public BooleanBinding lessThan(final int other) { 251 return Bindings.lessThan(this, other); 252 } 253 254 // =============================================================== 255 // IsGreaterThanOrEqualTo 256 257 @Override 258 public BooleanBinding greaterThanOrEqualTo(final ObservableNumberValue other) { 259 return Bindings.greaterThanOrEqual(this, other); 260 } 261 262 @Override 263 public BooleanBinding greaterThanOrEqualTo(final double other) { 264 return Bindings.greaterThanOrEqual(this, other); 265 } 266 267 @Override 268 public BooleanBinding greaterThanOrEqualTo(final float other) { 269 return Bindings.greaterThanOrEqual(this, other); 270 } 271 272 @Override 273 public BooleanBinding greaterThanOrEqualTo(final long other) { 274 return Bindings.greaterThanOrEqual(this, other); 275 } 276 277 @Override 278 public BooleanBinding greaterThanOrEqualTo(final int other) { 279 return Bindings.greaterThanOrEqual(this, other); 280 } 281 282 // =============================================================== 283 // IsLessThanOrEqualTo 284 285 @Override 286 public BooleanBinding lessThanOrEqualTo(final ObservableNumberValue other) { 287 return Bindings.lessThanOrEqual(this, other); 288 } 289 290 @Override 291 public BooleanBinding lessThanOrEqualTo(final double other) { 292 return Bindings.lessThanOrEqual(this, other); 293 } 294 295 @Override 296 public BooleanBinding lessThanOrEqualTo(final float other) { 297 return Bindings.lessThanOrEqual(this, other); 298 } 299 300 @Override 301 public BooleanBinding lessThanOrEqualTo(final long other) { 302 return Bindings.lessThanOrEqual(this, other); 303 } 304 305 @Override 306 public BooleanBinding lessThanOrEqualTo(final int other) { 307 return Bindings.lessThanOrEqual(this, other); 308 } 309 310 // =============================================================== 311 // String conversions 312 313 @Override 314 public StringBinding asString() { 315 return (StringBinding) StringFormatter.convert(this); 316 } 317 318 @Override 319 public StringBinding asString(String format) { 320 return (StringBinding) Bindings.format(format, this); 321 } 322 323 @Override 324 public StringBinding asString(Locale locale, String format) { 325 return (StringBinding) Bindings.format(locale, format, this); 326 } 327 328 }