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 com.sun.javafx.binding.StringFormatter;
029import javafx.beans.value.ObservableObjectValue;
030import javafx.collections.FXCollections;
031import javafx.collections.ObservableList;
032
033import com.sun.javafx.collections.annotations.ReturnsUnmodifiableCollection;
034import java.util.Locale;
035
036/**
037 * A {@code ObjectExpression} is a
038 * {@link javafx.beans.value.ObservableObjectValue} plus additional convenience
039 * methods to generate bindings in a fluent style.
040 * <p>
041 * A concrete sub-class of {@code ObjectExpression} has to implement the method
042 * {@link javafx.beans.value.ObservableObjectValue#get()}, which provides the
043 * actual value of this expression.
044 */
045public abstract class ObjectExpression<T> implements ObservableObjectValue<T> {
046
047    @Override
048    public T getValue() {
049        return get();
050    }
051
052    /**
053     * Returns an {@code ObjectExpression} that wraps an
054     * {@link javafx.beans.value.ObservableObjectValue}. If the
055     * {@code ObservableObjectValue} is already an {@code ObjectExpression}, it
056     * will be returned. Otherwise a new
057     * {@link javafx.beans.binding.ObjectBinding} is created that is bound to
058     * the {@code ObservableObjectValue}.
059     *
060     * @param value
061     *            The source {@code ObservableObjectValue}
062     * @return A {@code ObjectExpression} that wraps the
063     *         {@code ObservableObjectValue} if necessary
064     * @throws NullPointerException
065     *             if {@code value} is {@code null}
066     */
067    public static <T> ObjectExpression<T> objectExpression(
068            final ObservableObjectValue<T> value) {
069        if (value == null) {
070            throw new NullPointerException("Value must be specified.");
071        }
072        return value instanceof ObjectExpression ? (ObjectExpression<T>) value
073                : new ObjectBinding<T>() {
074                    {
075                        super.bind(value);
076                    }
077
078                    @Override
079                    public void dispose() {
080                        super.unbind(value);
081                    }
082
083                    @Override
084                    protected T computeValue() {
085                        return value.get();
086                    }
087
088                    @Override
089                    @ReturnsUnmodifiableCollection
090                    public ObservableList<ObservableObjectValue<T>> getDependencies() {
091                        return FXCollections.singletonObservableList(value);
092                    }
093                };
094    }
095
096    /**
097     * Creates a new {@code BooleanExpression} that holds {@code true} if this and
098     * another {@link javafx.beans.value.ObservableObjectValue} are equal.
099     *
100     * @param other
101     *            the other {@code ObservableObjectValue}
102     * @return the new {@code BooleanExpression}
103     * @throws NullPointerException
104     *             if {@code other} is {@code null}
105     */
106    public BooleanBinding isEqualTo(final ObservableObjectValue<?> other) {
107        return Bindings.equal(this, other);
108    }
109
110    /**
111     * Creates a new {@code BooleanExpression} that holds {@code true} if this
112     * {@code ObjectExpression} is equal to a constant value.
113     *
114     * @param other
115     *            the constant value
116     * @return the new {@code BooleanExpression}
117     */
118    public BooleanBinding isEqualTo(final Object other) {
119        return Bindings.equal(this, other);
120    }
121
122    /**
123     * Creates a new {@code BooleanExpression} that holds {@code true} if this and
124     * another {@link javafx.beans.value.ObservableObjectValue} are not equal.
125     *
126     * @param other
127     *            the other {@code ObservableObjectValue}
128     * @return the new {@code BooleanExpression}
129     * @throws NullPointerException
130     *             if {@code other} is {@code null}
131     */
132    public BooleanBinding isNotEqualTo(final ObservableObjectValue<?> other) {
133        return Bindings.notEqual(this, other);
134    }
135
136    /**
137     * Creates a new {@code BooleanExpression} that holds {@code true} if this
138     * {@code ObjectExpression} is not equal to a constant value.
139     *
140     * @param other
141     *            the constant value
142     * @return the new {@code BooleanExpression}
143     */
144    public BooleanBinding isNotEqualTo(final Object other) {
145        return Bindings.notEqual(this, other);
146    }
147
148    /**
149     * Creates a new {@link BooleanBinding} that holds {@code true} if this
150     * {@code ObjectExpression} is {@code null}.
151     *
152     * @return the new {@code BooleanBinding}
153     */
154    public BooleanBinding isNull() {
155        return Bindings.isNull(this);
156    }
157
158    /**
159     * Creates a new {@link BooleanBinding} that holds {@code true} if this
160     * {@code ObjectExpression} is not {@code null}.
161     *
162     * @return the new {@code BooleanBinding}
163     */
164    public BooleanBinding isNotNull() {
165        return Bindings.isNotNull(this);
166    }
167
168    /**
169     * Creates a {@link javafx.beans.binding.StringBinding} that holds the value
170     * of this {@code ObjectExpression} turned into a {@code String}. If the
171     * value of this {@code ObjectExpression} changes, the value of the
172     * {@code StringBinding} will be updated automatically.
173     *
174     * @return the new {@code StringBinding}
175     * @since 8.0
176     */
177    public StringBinding asString() {
178        return (StringBinding) StringFormatter.convert(this);
179    }
180
181    /**
182     * Creates a {@link javafx.beans.binding.StringBinding} that holds the value
183     * of the {@code ObjectExpression} turned into a {@code String}. If the
184     * value of this {@code ObjectExpression} changes, the value of the
185     * {@code StringBinding} will be updated automatically.
186     * <p>
187     * The result is formatted according to the formatting {@code String}. See
188     * {@code java.util.Formatter} for formatting rules.
189     *
190     * @param format
191     *            the formatting {@code String}
192     * @return the new {@code StringBinding}
193     * @since 8.0
194     */
195    public StringBinding asString(String format) {
196        return (StringBinding) Bindings.format(format, this);
197    }
198
199    /**
200     * Creates a {@link javafx.beans.binding.StringBinding} that holds the value
201     * of the {@code NumberExpression} turned into a {@code String}. If the
202     * value of this {@code NumberExpression} changes, the value of the
203     * {@code StringBinding} will be updated automatically.
204     * <p>
205     * The result is formatted according to the formatting {@code String} and
206     * the passed in {@code Locale}. See {@code java.util.Formatter} for
207     * formatting rules. See {@code java.util.Locale} for details on
208     * {@code Locale}.
209     *
210     * @param format
211     *            the formatting {@code String}
212     * @return the new {@code StringBinding}
213     * @since 8.0
214     */
215    public StringBinding asString(Locale locale, String format) {
216        return (StringBinding) Bindings.format(locale, format, this);
217    }
218}