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