Spec-Zone .ru
спецификации, руководства, описания, API
001/*
002 * Copyright (c) 2011, 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.property;
027
028import javafx.beans.InvalidationListener;
029import javafx.beans.Observable;
030import javafx.beans.WeakInvalidationListener;
031import javafx.beans.binding.LongExpression;
032
033/**
034 * Super class for all readonly properties wrapping a {@code long}.
035 * 
036 * @see javafx.beans.value.ObservableLongValue
037 * @see javafx.beans.binding.LongExpression
038 * @see ReadOnlyProperty
039 * 
040 */
041public abstract class ReadOnlyLongProperty extends LongExpression implements
042        ReadOnlyProperty<Number> {
043
044    /**
045     * The constructor of {@code ReadOnlyLongProperty}.
046     */
047    public ReadOnlyLongProperty() {
048    }
049
050    /**
051     * Returns a string representation of this {@code ReadOnlyLongProperty} object.
052     * @return a string representation of this {@code ReadOnlyLongProperty} object.
053     */ 
054    @Override
055    public String toString() {
056        final Object bean = getBean();
057        final String name = getName();
058        final StringBuilder result = new StringBuilder("ReadOnlyLongProperty [");
059        if (bean != null) {
060            result.append("bean: ").append(bean).append(", ");
061        }
062        if ((name != null) && !name.equals("")) {
063            result.append("name: ").append(name).append(", ");
064        }
065        result.append("value: ").append(get()).append("]");
066        return result.toString();
067    }
068    
069    /**
070     * Returns a {@code ReadOnlyLongProperty} that wraps a
071     * {@link javafx.beans.property.ReadOnlyProperty}. If the
072     * {@code ReadOnlyProperty} is already a {@code ReadOnlyLongProperty}, it
073     * will be returned. Otherwise a new
074     * {@code ReadOnlyLongProperty} is created that is bound to
075     * the {@code ReadOnlyProperty}.
076     * 
077     * Note: null values will be interpreted as 0L
078     * 
079     * @param property
080     *            The source {@code ReadOnlyProperty}
081     * @return A {@code ReadOnlyLongProperty} that wraps the
082     *         {@code ReadOnlyProperty} if necessary
083     * @throws NullPointerException
084     *             if {@code value} is {@code null}
085     */
086    public static <T extends Number> ReadOnlyLongProperty readOnlyLongProperty(final ReadOnlyProperty<T> property) {
087        if (property == null) {
088            throw new NullPointerException("Property cannot be null");
089        }
090        
091        return property instanceof ReadOnlyLongProperty ? (ReadOnlyLongProperty) property:
092           new ReadOnlyLongPropertyBase() {
093            private boolean valid = true;
094            private final InvalidationListener listener = new InvalidationListener() {
095                @Override
096                public void invalidated(Observable observable) {
097                    if (valid) {
098                        valid = false;
099                        fireValueChangedEvent();
100                    }
101                }
102            };
103
104            {
105                property.addListener(new WeakInvalidationListener(listener));
106            }
107                    
108            @Override
109            public long get() {
110                valid = true;
111                final T value = property.getValue();
112                return value == null ? 0L : value.longValue();
113            }
114
115            @Override
116            public Object getBean() {
117                return null; // Virtual property, no bean
118            }
119
120            @Override
121            public String getName() {
122                return property.getName();
123            }
124        };
125    }
126
127    /**
128     * Creates a {@link javafx.beans.property.ReadOnlyObjectProperty} that holds the value
129     * of this {@code ReadOnlyLongProperty}. If the
130     * value of this {@code ReadOnlyLongProperty} changes, the value of the
131     * {@code ReadOnlyObjectProperty} will be updated automatically.
132     * 
133     * @return the new {@code ReadOnlyObjectProperty}
134     */
135    @Override
136    public ReadOnlyObjectProperty<Long> asObject() {
137        return new ReadOnlyObjectPropertyBase<Long>() {
138
139            private boolean valid = true;
140            private final InvalidationListener listener = new InvalidationListener() {
141                @Override
142                public void invalidated(Observable observable) {
143                    if (valid) {
144                        valid = false;
145                        fireValueChangedEvent();
146                    }
147                }
148            };
149
150            {
151                ReadOnlyLongProperty.this.addListener(new WeakInvalidationListener(listener));
152            }
153            
154            @Override
155            public Object getBean() {
156                return null; // Virtual property, does not exist on a bean
157            }
158
159            @Override
160            public String getName() {
161                return ReadOnlyLongProperty.this.getName();
162            }
163
164            @Override
165            public Long get() {
166                valid = true;
167                return ReadOnlyLongProperty.this.getValue();
168            }
169        };
170    };
171
172}