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 javafx.beans.value.ObservableFloatValue;
029import javafx.collections.FXCollections;
030import javafx.collections.ObservableList;
031import com.sun.javafx.collections.annotations.ReturnsUnmodifiableCollection;
032import javafx.beans.value.ObservableValue;
033
034/**
035 * A {@code FloatExpression} is a
036 * {@link javafx.beans.value.ObservableFloatValue} plus additional convenience
037 * methods to generate bindings in a fluent style.
038 * <p>
039 * A concrete sub-class of {@code FloatExpression} has to implement the method
040 * {@link javafx.beans.value.ObservableFloatValue#get()}, which provides the
041 * actual value of this expression.
042 */
043public abstract class FloatExpression extends NumberExpressionBase implements
044        ObservableFloatValue {
045
046    @Override
047    public int intValue() {
048        return (int) get();
049    }
050
051    @Override
052    public long longValue() {
053        return (long) get();
054    }
055
056    @Override
057    public float floatValue() {
058        return get();
059    }
060
061    @Override
062    public double doubleValue() {
063        return (double) get();
064    }
065
066    @Override
067    public Float getValue() {
068        return get();
069    }
070
071    /**
072     * Returns a {@code FloatExpression} that wraps a
073     * {@link javafx.beans.value.ObservableFloatValue}. If the
074     * {@code ObservableFloatValue} is already a {@code FloatExpression}, it
075     * will be returned. Otherwise a new
076     * {@link javafx.beans.binding.FloatBinding} is created that is bound to the
077     * {@code ObservableFloatValue}.
078     * 
079     * @param value
080     *            The source {@code ObservableFloatValue}
081     * @return A {@code FloatExpression} that wraps the
082     *         {@code ObservableFloatValue} if necessary
083     * @throws NullPointerException
084     *             if {@code value} is {@code null}
085     */
086    public static FloatExpression floatExpression(
087            final ObservableFloatValue value) {
088        if (value == null) {
089            throw new NullPointerException("Value must be specified.");
090        }
091        return (value instanceof FloatExpression) ? (FloatExpression) value
092                : new FloatBinding() {
093                    {
094                        super.bind(value);
095                    }
096
097                    @Override
098                    public void dispose() {
099                        super.unbind(value);
100                    }
101
102                    @Override
103                    protected float computeValue() {
104                        return value.get();
105                    }
106
107                    @Override
108                    @ReturnsUnmodifiableCollection
109                    public ObservableList<ObservableFloatValue> getDependencies() {
110                        return FXCollections.singletonObservableList(value);
111                    }
112                };
113    }
114    
115    /**
116     * Returns a {@code FloatExpression} that wraps an
117     * {@link javafx.beans.value.ObservableValue}. If the
118     * {@code ObservableValue} is already a {@code FloatExpression}, it
119     * will be returned. Otherwise a new
120     * {@link javafx.beans.binding.FloatBinding} is created that is bound to
121     * the {@code ObservableValue}.
122     * 
123     * <p>
124     * Note: this method can be used to convert an {@link ObjectExpression} or
125     * {@link javafx.beans.property.ObjectProperty} of specific number type to FloatExpression, which
126     * is essentially an {@code ObservableValue<Number>}. See sample below.
127     * 
128     * <blockquote><pre>
129     *   FloatProperty floatProperty = new SimpleFloatProperty(1.0f);
130     *   ObjectProperty&lt;Float&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2.0f);
131     *   BooleanBinding binding = floatProperty.greaterThan(FloatExpression.floatExpression(objectProperty));
132     * </pre></blockquote>
133     * 
134     *  Note: null values will be interpreted as 0f
135     * 
136     * @param value
137     *            The source {@code ObservableValue}
138     * @return A {@code FloatExpression} that wraps the
139     *         {@code ObservableValue} if necessary
140     * @throws NullPointerException
141     *             if {@code value} is {@code null}
142     */
143    public static <T extends Number> FloatExpression floatExpression(final ObservableValue<T> value) {
144        if (value == null) {
145            throw new NullPointerException("Value must be specified.");
146        }
147        return (value instanceof FloatExpression) ? (FloatExpression) value
148                : new FloatBinding() {
149            {
150                super.bind(value);
151            }
152
153            @Override
154            public void dispose() {
155                super.unbind(value);
156            }
157
158            @Override
159            protected float computeValue() {
160                final T val = value.getValue();
161                return val == null ? 0f :  val.floatValue();
162            }
163
164            @Override
165            @ReturnsUnmodifiableCollection
166            public ObservableList<ObservableValue<T>> getDependencies() {
167                return FXCollections.singletonObservableList(value);
168            }
169        };
170    }
171
172
173    @Override
174    public FloatBinding negate() {
175        return (FloatBinding) Bindings.negate(this);
176    }
177
178    @Override
179    public DoubleBinding add(final double other) {
180        return Bindings.add(this, other);
181    }
182
183    @Override
184    public FloatBinding add(final float other) {
185        return (FloatBinding) Bindings.add(this, other);
186    }
187
188    @Override
189    public FloatBinding add(final long other) {
190        return (FloatBinding) Bindings.add(this, other);
191    }
192
193    @Override
194    public FloatBinding add(final int other) {
195        return (FloatBinding) Bindings.add(this, other);
196    }
197
198    @Override
199    public DoubleBinding subtract(final double other) {
200        return Bindings.subtract(this, other);
201    }
202
203    @Override
204    public FloatBinding subtract(final float other) {
205        return (FloatBinding) Bindings.subtract(this, other);
206    }
207
208    @Override
209    public FloatBinding subtract(final long other) {
210        return (FloatBinding) Bindings.subtract(this, other);
211    }
212
213    @Override
214    public FloatBinding subtract(final int other) {
215        return (FloatBinding) Bindings.subtract(this, other);
216    }
217
218    @Override
219    public DoubleBinding multiply(final double other) {
220        return Bindings.multiply(this, other);
221    }
222
223    @Override
224    public FloatBinding multiply(final float other) {
225        return (FloatBinding) Bindings.multiply(this, other);
226    }
227
228    @Override
229    public FloatBinding multiply(final long other) {
230        return (FloatBinding) Bindings.multiply(this, other);
231    }
232
233    @Override
234    public FloatBinding multiply(final int other) {
235        return (FloatBinding) Bindings.multiply(this, other);
236    }
237
238    @Override
239    public DoubleBinding divide(final double other) {
240        return Bindings.divide(this, other);
241    }
242
243    @Override
244    public FloatBinding divide(final float other) {
245        return (FloatBinding) Bindings.divide(this, other);
246    }
247
248    @Override
249    public FloatBinding divide(final long other) {
250        return (FloatBinding) Bindings.divide(this, other);
251    }
252
253    @Override
254    public FloatBinding divide(final int other) {
255        return (FloatBinding) Bindings.divide(this, other);
256    }
257    
258    /**
259     * Creates an {@link javafx.beans.binding.ObjectExpression} that holds the value
260     * of this {@code FloatExpression}. If the
261     * value of this {@code FloatExpression} changes, the value of the
262     * {@code ObjectExpression} will be updated automatically.
263     * 
264     * @return the new {@code ObjectExpression}
265     */
266    public ObjectExpression<Float> asObject() {
267        return new ObjectBinding<Float>() {
268            {
269                bind(FloatExpression.this);
270            }
271
272            @Override
273            public void dispose() {
274                unbind(FloatExpression.this);
275            }
276            
277            @Override
278            protected Float computeValue() {
279                return FloatExpression.this.getValue();
280            }
281        };
282    }
283}