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