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