Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation.  Oracle designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Oracle in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
022 * or visit www.oracle.com if you need additional information or have any
023 * questions.
024 */
025
026package javafx.beans.binding;
027
028import java.util.Locale;
029
030import javafx.beans.value.ObservableDoubleValue;
031import javafx.beans.value.ObservableFloatValue;
032import javafx.beans.value.ObservableIntegerValue;
033import javafx.beans.value.ObservableLongValue;
034import javafx.beans.value.ObservableNumberValue;
035
036import com.sun.javafx.binding.StringFormatter;
037
038/**
039 * A {@code NumberExpressionBase} contains convenience methods to generate bindings in a fluent style,
040 * that are common to all NumberExpression subclasses.
041 * <p>
042 * NumberExpressionBase serves as a place for common code of specific NumberExpression subclasses for the
043 * specific number type.
044 * @see IntegerExpression
045 * @see LongExpression
046 * @see FloatExpression
047 * @see DoubleExpression
048 */
049public abstract class NumberExpressionBase implements NumberExpression {
050
051    /**
052     * Returns an {@code NumberExpressionBase} that wraps a
053     * {@link javafx.beans.value.ObservableNumberValue}. If the
054     * {@code ObservableNumberValue} is already an instance of
055     * {@code NumberExpressionBase}, it will be returned. Otherwise a new
056     * {@link javafx.beans.binding.NumberBinding} is created that is bound to
057     * the {@code ObservableNumberValue}.
058     * 
059     * @param value
060     *            The source {@code ObservableNumberValue}
061     * @return An {@code NumberExpressionBase} that wraps the
062     *         {@code ObservableNumberValue} if necessary
063     * @throws NullPointerException
064     *             if {@code value} is {@code null}
065     */
066    public static <S extends Number> NumberExpressionBase numberExpression(
067            final ObservableNumberValue value) {
068        if (value == null) {
069            throw new NullPointerException("Value must be specified.");
070        }
071        NumberExpressionBase result = (NumberExpressionBase) ((value instanceof NumberExpressionBase) ? value
072                : (value instanceof ObservableIntegerValue) ? IntegerExpression
073                        .integerExpression((ObservableIntegerValue) value)
074                        : (value instanceof ObservableDoubleValue) ? DoubleExpression
075                                .doubleExpression((ObservableDoubleValue) value)
076                                : (value instanceof ObservableFloatValue) ? FloatExpression
077                                        .floatExpression((ObservableFloatValue) value)
078                                        : (value instanceof ObservableLongValue) ? LongExpression
079                                                .longExpression((ObservableLongValue) value)
080                                                : null);
081        if (result != null) {
082            return result;
083        } else {
084            throw new IllegalArgumentException("Unsupported Type");
085        }
086    }
087
088    @Override
089    public NumberBinding add(final ObservableNumberValue other) {
090        return Bindings.add(this, other);
091    }
092
093    @Override
094    public NumberBinding subtract(final ObservableNumberValue other) {
095        return Bindings.subtract(this, other);
096    }
097
098    @Override
099    public NumberBinding multiply(final ObservableNumberValue other) {
100        return Bindings.multiply(this, other);
101    }
102
103    @Override
104    public NumberBinding divide(final ObservableNumberValue other) {
105        return Bindings.divide(this, other);
106    }
107
108    // ===============================================================
109    // IsEqualTo
110
111    @Override
112    public BooleanBinding isEqualTo(final ObservableNumberValue other) {
113        return Bindings.equal(this, other);
114    }
115
116    @Override
117    public BooleanBinding isEqualTo(final ObservableNumberValue other,
118            double epsilon) {
119        return Bindings.equal(this, other, epsilon);
120    }
121
122    @Override
123    public BooleanBinding isEqualTo(final double other, double epsilon) {
124        return Bindings.equal(this, other, epsilon);
125    }
126
127    @Override
128    public BooleanBinding isEqualTo(final float other, double epsilon) {
129        return Bindings.equal(this, other, epsilon);
130    }
131
132    @Override
133    public BooleanBinding isEqualTo(final long other) {
134        return Bindings.equal(this, other);
135    }
136
137    @Override
138    public BooleanBinding isEqualTo(final long other, double epsilon) {
139        return Bindings.equal(this, other, epsilon);
140    }
141
142    @Override
143    public BooleanBinding isEqualTo(final int other) {
144        return Bindings.equal(this, other);
145    }
146
147    @Override
148    public BooleanBinding isEqualTo(final int other, double epsilon) {
149        return Bindings.equal(this, other, epsilon);
150    }
151
152    // ===============================================================
153    // IsNotEqualTo
154
155    @Override
156    public BooleanBinding isNotEqualTo(final ObservableNumberValue other) {
157        return Bindings.notEqual(this, other);
158    }
159
160    @Override
161    public BooleanBinding isNotEqualTo(final ObservableNumberValue other,
162            double epsilon) {
163        return Bindings.notEqual(this, other, epsilon);
164    }
165
166    @Override
167    public BooleanBinding isNotEqualTo(final double other, double epsilon) {
168        return Bindings.notEqual(this, other, epsilon);
169    }
170
171    @Override
172    public BooleanBinding isNotEqualTo(final float other, double epsilon) {
173        return Bindings.notEqual(this, other, epsilon);
174    }
175
176    @Override
177    public BooleanBinding isNotEqualTo(final long other) {
178        return Bindings.notEqual(this, other);
179    }
180
181    @Override
182    public BooleanBinding isNotEqualTo(final long other, double epsilon) {
183        return Bindings.notEqual(this, other, epsilon);
184    }
185
186    @Override
187    public BooleanBinding isNotEqualTo(final int other) {
188        return Bindings.notEqual(this, other);
189    }
190
191    @Override
192    public BooleanBinding isNotEqualTo(final int other, double epsilon) {
193        return Bindings.notEqual(this, other, epsilon);
194    }
195
196    // ===============================================================
197    // IsGreaterThan
198
199    @Override
200    public BooleanBinding greaterThan(final ObservableNumberValue other) {
201        return Bindings.greaterThan(this, other);
202    }
203
204    @Override
205    public BooleanBinding greaterThan(final double other) {
206        return Bindings.greaterThan(this, other);
207    }
208
209    @Override
210    public BooleanBinding greaterThan(final float other) {
211        return Bindings.greaterThan(this, other);
212    }
213
214    @Override
215    public BooleanBinding greaterThan(final long other) {
216        return Bindings.greaterThan(this, other);
217    }
218
219    @Override
220    public BooleanBinding greaterThan(final int other) {
221        return Bindings.greaterThan(this, other);
222    }
223
224    // ===============================================================
225    // IsLesserThan
226
227    @Override
228    public BooleanBinding lessThan(final ObservableNumberValue other) {
229        return Bindings.lessThan(this, other);
230    }
231
232    @Override
233    public BooleanBinding lessThan(final double other) {
234        return Bindings.lessThan(this, other);
235    }
236
237    @Override
238    public BooleanBinding lessThan(final float other) {
239        return Bindings.lessThan(this, other);
240    }
241
242    @Override
243    public BooleanBinding lessThan(final long other) {
244        return Bindings.lessThan(this, other);
245    }
246
247    @Override
248    public BooleanBinding lessThan(final int other) {
249        return Bindings.lessThan(this, other);
250    }
251
252    // ===============================================================
253    // IsGreaterThanOrEqualTo
254
255    @Override
256    public BooleanBinding greaterThanOrEqualTo(final ObservableNumberValue other) {
257        return Bindings.greaterThanOrEqual(this, other);
258    }
259
260    @Override
261    public BooleanBinding greaterThanOrEqualTo(final double other) {
262        return Bindings.greaterThanOrEqual(this, other);
263    }
264
265    @Override
266    public BooleanBinding greaterThanOrEqualTo(final float other) {
267        return Bindings.greaterThanOrEqual(this, other);
268    }
269
270    @Override
271    public BooleanBinding greaterThanOrEqualTo(final long other) {
272        return Bindings.greaterThanOrEqual(this, other);
273    }
274
275    @Override
276    public BooleanBinding greaterThanOrEqualTo(final int other) {
277        return Bindings.greaterThanOrEqual(this, other);
278    }
279
280    // ===============================================================
281    // IsLessThanOrEqualTo
282
283    @Override
284    public BooleanBinding lessThanOrEqualTo(final ObservableNumberValue other) {
285        return Bindings.lessThanOrEqual(this, other);
286    }
287
288    @Override
289    public BooleanBinding lessThanOrEqualTo(final double other) {
290        return Bindings.lessThanOrEqual(this, other);
291    }
292
293    @Override
294    public BooleanBinding lessThanOrEqualTo(final float other) {
295        return Bindings.lessThanOrEqual(this, other);
296    }
297
298    @Override
299    public BooleanBinding lessThanOrEqualTo(final long other) {
300        return Bindings.lessThanOrEqual(this, other);
301    }
302
303    @Override
304    public BooleanBinding lessThanOrEqualTo(final int other) {
305        return Bindings.lessThanOrEqual(this, other);
306    }
307
308    // ===============================================================
309    // String conversions
310
311    @Override
312    public StringBinding asString() {
313        return (StringBinding) StringFormatter.convert(this);
314    }
315
316    @Override
317    public StringBinding asString(String format) {
318        return (StringBinding) Bindings.format(format, this);
319    }
320
321    @Override
322    public StringBinding asString(Locale locale, String format) {
323        return (StringBinding) Bindings.format(locale, format, this);
324    }
325
326}