1 /*
   2  * Copyright (c) 2010, 2013, 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 javafx.beans.value.ObservableLongValue;
  29 import javafx.collections.FXCollections;
  30 import javafx.collections.ObservableList;
  31 import javafx.beans.value.ObservableValue;
  32 
  33 /**
  34  * A {@code LongExpression} is a {@link javafx.beans.value.ObservableLongValue}
  35  * plus additional convenience methods to generate bindings in a fluent style.
  36  * <p>
  37  * A concrete sub-class of {@code LongExpression} has to implement the method
  38  * {@link javafx.beans.value.ObservableLongValue#get()}, which provides the
  39  * actual value of this expression.
  40  * @since JavaFX 2.0
  41  */
  42 public abstract class LongExpression extends NumberExpressionBase implements
  43         ObservableLongValue {
  44 
  45     @Override
  46     public int intValue() {
  47         return (int) get();
  48     }
  49 
  50     @Override
  51     public long longValue() {
  52         return get();
  53     }
  54 
  55     @Override
  56     public float floatValue() {
  57         return (float) get();
  58     }
  59 
  60     @Override
  61     public double doubleValue() {
  62         return (double) get();
  63     }
  64 
  65     @Override
  66     public Long getValue() {
  67         return get();
  68     }
  69 
  70     /**
  71      * Returns a {@code LongExpression} that wraps a
  72      * {@link javafx.beans.value.ObservableLongValue}. If the
  73      * {@code ObservableLongValue} is already a {@code LongExpression}, it will
  74      * be returned. Otherwise a new {@link javafx.beans.binding.LongBinding} is
  75      * created that is bound to the {@code ObservableLongValue}.
  76      *
  77      * @param value
  78      *            The source {@code ObservableLongValue}
  79      * @return A {@code LongExpression} that wraps the
  80      *         {@code ObservableLongValue} if necessary
  81      * @throws NullPointerException
  82      *             if {@code value} is {@code null}
  83      */
  84     public static LongExpression longExpression(final ObservableLongValue value) {
  85         if (value == null) {
  86             throw new NullPointerException("Value must be specified.");
  87         }
  88         return (value instanceof LongExpression) ? (LongExpression) value
  89                 : new LongBinding() {
  90                     {
  91                         super.bind(value);
  92                     }
  93 
  94                     @Override
  95                     public void dispose() {
  96                         super.unbind(value);
  97                     }
  98 
  99                     @Override
 100                     protected long computeValue() {
 101                         return value.get();
 102                     }
 103 
 104                     @Override
 105                     public ObservableList<ObservableLongValue> getDependencies() {
 106                         return FXCollections.singletonObservableList(value);
 107                     }
 108         };
 109     }
 110 
 111     /**
 112      * Returns a {@code LongExpression} that wraps an
 113      * {@link javafx.beans.value.ObservableValue}. If the
 114      * {@code ObservableValue} is already a {@code LongExpression}, it
 115      * will be returned. Otherwise a new
 116      * {@link javafx.beans.binding.LongBinding} is created that is bound to
 117      * the {@code ObservableValue}.
 118      *
 119      * <p>
 120      * Note: this method can be used to convert an {@link ObjectExpression} or
 121      * {@link javafx.beans.property.ObjectProperty} of specific number type to LongExpression, which
 122      * is essentially an {@code ObservableValue<Number>}. See sample below.
 123      *
 124      * <blockquote><pre>
 125      *   LongProperty longProperty = new SimpleLongProperty(1L);
 126      *   ObjectProperty&lt;Long&gt; objectProperty = new SimpleObjectProperty&lt;&gt;(2L);
 127      *   BooleanBinding binding = longProperty.greaterThan(LongExpression.longExpression(objectProperty));
 128      * </pre></blockquote>
 129      *
 130      * Note: null values will be interpreted as 0L
 131      *
 132      * @param value
 133      *            The source {@code ObservableValue}
 134      * @return A {@code LongExpression} that wraps the
 135      *         {@code ObservableValue} if necessary
 136      * @throws NullPointerException
 137      *             if {@code value} is {@code null}
 138      * @since JavaFX 8.0
 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             public ObservableList<ObservableValue<T>> getDependencies() {
 163                 return FXCollections.singletonObservableList(value);
 164             }
 165         };
 166     }
 167 
 168 
 169     @Override
 170     public LongBinding negate() {
 171         return (LongBinding) Bindings.negate(this);
 172     }
 173 
 174     @Override
 175     public DoubleBinding add(final double other) {
 176         return Bindings.add(this, other);
 177     }
 178 
 179     @Override
 180     public FloatBinding add(final float other) {
 181         return (FloatBinding) Bindings.add(this, other);
 182     }
 183 
 184     @Override
 185     public LongBinding add(final long other) {
 186         return (LongBinding) Bindings.add(this, other);
 187     }
 188 
 189     @Override
 190     public LongBinding add(final int other) {
 191         return (LongBinding) Bindings.add(this, other);
 192     }
 193 
 194     @Override
 195     public DoubleBinding subtract(final double other) {
 196         return Bindings.subtract(this, other);
 197     }
 198 
 199     @Override
 200     public FloatBinding subtract(final float other) {
 201         return (FloatBinding) Bindings.subtract(this, other);
 202     }
 203 
 204     @Override
 205     public LongBinding subtract(final long other) {
 206         return (LongBinding) Bindings.subtract(this, other);
 207     }
 208 
 209     @Override
 210     public LongBinding subtract(final int other) {
 211         return (LongBinding) Bindings.subtract(this, other);
 212     }
 213 
 214     @Override
 215     public DoubleBinding multiply(final double other) {
 216         return Bindings.multiply(this, other);
 217     }
 218 
 219     @Override
 220     public FloatBinding multiply(final float other) {
 221         return (FloatBinding) Bindings.multiply(this, other);
 222     }
 223 
 224     @Override
 225     public LongBinding multiply(final long other) {
 226         return (LongBinding) Bindings.multiply(this, other);
 227     }
 228 
 229     @Override
 230     public LongBinding multiply(final int other) {
 231         return (LongBinding) Bindings.multiply(this, other);
 232     }
 233 
 234     @Override
 235     public DoubleBinding divide(final double other) {
 236         return Bindings.divide(this, other);
 237     }
 238 
 239     @Override
 240     public FloatBinding divide(final float other) {
 241         return (FloatBinding) Bindings.divide(this, other);
 242     }
 243 
 244     @Override
 245     public LongBinding divide(final long other) {
 246         return (LongBinding) Bindings.divide(this, other);
 247     }
 248 
 249     @Override
 250     public LongBinding divide(final int other) {
 251         return (LongBinding) Bindings.divide(this, other);
 252     }
 253 
 254     /**
 255      * Creates an {@link javafx.beans.binding.ObjectExpression} that holds the value
 256      * of this {@code LongExpression}. If the
 257      * value of this {@code LongExpression} changes, the value of the
 258      * {@code ObjectExpression} will be updated automatically.
 259      *
 260      * @return the new {@code ObjectExpression}
 261      * @since JavaFX 8.0
 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 }