1 /* 2 * Copyright (c) 2011, 2014, 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.property; 27 28 import javafx.beans.InvalidationListener; 29 import javafx.beans.Observable; 30 import javafx.beans.WeakInvalidationListener; 31 import javafx.beans.binding.LongExpression; 32 33 /** 34 * Super class for all readonly properties wrapping a {@code long}. 35 * 36 * @see javafx.beans.value.ObservableLongValue 37 * @see javafx.beans.binding.LongExpression 38 * @see ReadOnlyProperty 39 * 40 * @since JavaFX 2.0 41 */ 42 public abstract class ReadOnlyLongProperty extends LongExpression implements 43 ReadOnlyProperty<Number> { 44 45 /** 46 * The constructor of {@code ReadOnlyLongProperty}. 47 */ 48 public ReadOnlyLongProperty() { 49 } 50 51 /** 52 * Returns a string representation of this {@code ReadOnlyLongProperty} object. 53 * @return a string representation of this {@code ReadOnlyLongProperty} object. 54 */ 55 @Override 56 public String toString() { 57 final Object bean = getBean(); 58 final String name = getName(); 59 final StringBuilder result = new StringBuilder("ReadOnlyLongProperty ["); 60 if (bean != null) { 61 result.append("bean: ").append(bean).append(", "); 62 } 63 if ((name != null) && !name.equals("")) { 64 result.append("name: ").append(name).append(", "); 65 } 66 result.append("value: ").append(get()).append("]"); 67 return result.toString(); 68 } 69 70 /** 71 * Returns a {@code ReadOnlyLongProperty} that wraps a 72 * {@link javafx.beans.property.ReadOnlyProperty}. If the 73 * {@code ReadOnlyProperty} is already a {@code ReadOnlyLongProperty}, it 74 * will be returned. Otherwise a new 75 * {@code ReadOnlyLongProperty} is created that is bound to 76 * the {@code ReadOnlyProperty}. 77 * 78 * Note: null values will be interpreted as 0L 79 * 80 * @param property 81 * The source {@code ReadOnlyProperty} 82 * @return A {@code ReadOnlyLongProperty} that wraps the 83 * {@code ReadOnlyProperty} if necessary 84 * @throws NullPointerException 85 * if {@code property} is {@code null} 86 * @since JavaFX 8.0 87 */ 88 public static <T extends Number> ReadOnlyLongProperty readOnlyLongProperty(final ReadOnlyProperty<T> property) { 89 if (property == null) { 90 throw new NullPointerException("Property cannot be null"); 91 } 92 93 return property instanceof ReadOnlyLongProperty ? (ReadOnlyLongProperty) property: 94 new ReadOnlyLongPropertyBase() { 95 private boolean valid = true; 96 private final InvalidationListener listener = observable -> { 97 if (valid) { 98 valid = false; 99 fireValueChangedEvent(); 100 } 101 }; 102 103 { 104 property.addListener(new WeakInvalidationListener(listener)); 105 } 106 107 @Override 108 public long get() { 109 valid = true; 110 final T value = property.getValue(); 111 return value == null ? 0L : value.longValue(); 112 } 113 114 @Override 115 public Object getBean() { 116 return null; // Virtual property, no bean 117 } 118 119 @Override 120 public String getName() { 121 return property.getName(); 122 } 123 }; 124 } 125 126 /** 127 * Creates a {@link javafx.beans.property.ReadOnlyObjectProperty} that holds the value 128 * of this {@code ReadOnlyLongProperty}. If the 129 * value of this {@code ReadOnlyLongProperty} changes, the value of the 130 * {@code ReadOnlyObjectProperty} will be updated automatically. 131 * 132 * @return the new {@code ReadOnlyObjectProperty} 133 * @since JavaFX 8.0 134 */ 135 @Override 136 public ReadOnlyObjectProperty<Long> asObject() { 137 return new ReadOnlyObjectPropertyBase<Long>() { 138 139 private boolean valid = true; 140 private final InvalidationListener listener = observable -> { 141 if (valid) { 142 valid = false; 143 fireValueChangedEvent(); 144 } 145 }; 146 147 { 148 ReadOnlyLongProperty.this.addListener(new WeakInvalidationListener(listener)); 149 } 150 151 @Override 152 public Object getBean() { 153 return null; // Virtual property, does not exist on a bean 154 } 155 156 @Override 157 public String getName() { 158 return ReadOnlyLongProperty.this.getName(); 159 } 160 161 @Override 162 public Long get() { 163 valid = true; 164 return ReadOnlyLongProperty.this.getValue(); 165 } 166 }; 167 }; 168 169 }