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.adapter;
027
028import com.sun.javafx.property.adapter.JavaBeanPropertyBuilderHelper;
029import com.sun.javafx.property.adapter.PropertyDescriptor;
030
031import java.lang.reflect.Method;
032
033/**
034 * A {@code JavaBeanFloatPropertyBuilder} can be used to create
035 * {@link JavaBeanFloatProperty JavaBeanFloatProperties}. To create
036 * a {@code JavaBeanFloatProperty} one first has to call {@link #create()}
037 * to generate a builder, set the required properties, and then one can
038 * call {@link #build()} to generate the property.
039 * <p>
040 * Not all properties of a builder have to specified, there are several
041 * combinations possible. As a minimum the {@link #name(java.lang.String)} of
042 * the property and the {@link #bean(java.lang.Object)} have to be specified.
043 * If the names of the getter and setter follow the conventions, this is sufficient.
044 * Otherwise it is possible to specify an alternative name for the getter and setter
045 * ({@link #getter(java.lang.String)} and {@link #setter(java.lang.String)}) or
046 * the getter and setter {@code Methods} directly ({@link #getter(java.lang.reflect.Method)}
047 * and {@link #setter(java.lang.reflect.Method)}).
048 * <p>
049 * All methods to change properties return a reference to this builder, to enable
050 * method chaining.
051 * <p>
052 * If you have to generate adapters for the same property of several instances
053 * of the same class, you can reuse a {@code JavaBeanFloatPropertyBuilder}.
054 * by switching the Java Bean instance (with {@link #bean(java.lang.Object)} and
055 * calling {@link #build()}.
056 * 
057 * @see JavaBeanFloatProperty
058 */
059public final class JavaBeanFloatPropertyBuilder {
060
061    private JavaBeanPropertyBuilderHelper helper = new JavaBeanPropertyBuilderHelper();
062
063    /**
064     * Create a new instance of {@code JavaBeanFloatPropertyBuilder}
065     * 
066     * @return the new {@code JavaBeanFloatPropertyBuilder} 
067     */
068    public static JavaBeanFloatPropertyBuilder create() {
069        return new JavaBeanFloatPropertyBuilder();
070    }
071
072    /**
073     * Generate a new {@link JavaBeanFloatProperty} with the current settings.
074     * 
075     * @return the new {@code JavaBeanFloatProperty}
076     * @throws NoSuchMethodException if the settings were not sufficient to find
077     * the getter and the setter of the Java Bean property
078     * @throws IllegalArgumentException if the Java Bean property is not of type
079     * {@code float} or {@code Float}
080     */
081    public JavaBeanFloatProperty build() throws NoSuchMethodException {
082        final PropertyDescriptor descriptor = helper.getDescriptor();
083        if (!float.class.equals(descriptor.getType()) && !Number.class.isAssignableFrom(descriptor.getType())) {
084            throw new IllegalArgumentException("Not a float property");
085        }
086        return new JavaBeanFloatProperty(descriptor, helper.getBean());
087    }
088
089    /**
090     * Set the name of the property
091     * 
092     * @param name the name of the property
093     * @return a reference to this builder to enable method chaining
094     */
095    public JavaBeanFloatPropertyBuilder name(String name) {
096        helper.name(name);
097        return this;
098    }
099
100    /**
101     * Set the Java Bean instance the adapter should connect to
102     * 
103     * @param bean the Java Bean instance
104     * @return a reference to this builder to enable method chaining
105     */
106    public JavaBeanFloatPropertyBuilder bean(Object bean) {
107        helper.bean(bean);
108        return this;
109    }
110    
111    /**
112     * Set the Java Bean class in which the getter and setter should be searched.
113     * This can be useful, if the builder should generate adapters for several
114     * Java Beans of different types.
115     * 
116     * @param beanClass the Java Bean class
117     * @return a reference to this builder to enable method chaining
118     */
119    public JavaBeanFloatPropertyBuilder beanClass(Class<?> beanClass) {
120        helper.beanClass(beanClass);
121        return this;
122    }
123
124    /**
125     * Set an alternative name for the getter. This can be omitted, if the 
126     * name of the getter follows Java Bean naming conventions.
127     * 
128     * @param getter the alternative name of the getter
129     * @return a reference to this builder to enable method chaining
130     */
131    public JavaBeanFloatPropertyBuilder getter(String getter) {
132        helper.getterName(getter);
133        return this;
134    }
135
136    /**
137     * Set an alternative name for the setter. This can be omitted, if the 
138     * name of the setter follows Java Bean naming conventions.
139     * 
140     * @param setter the alternative name of the setter
141     * @return a reference to this builder to enable method chaining
142     */
143    public JavaBeanFloatPropertyBuilder setter(String setter) {
144        helper.setterName(setter);
145        return this;
146    }
147
148    /**
149     * Set the getter method directly. This can be omitted, if the 
150     * name of the getter follows Java Bean naming conventions.
151     * 
152     * @param getter the getter
153     * @return a reference to this builder to enable method chaining
154     */
155    public JavaBeanFloatPropertyBuilder getter(Method getter) {
156        helper.getter(getter);
157        return this;
158    }
159
160    /**
161     * Set the setter method directly. This can be omitted, if the 
162     * name of the setter follows Java Bean naming conventions.
163     * 
164     * @param setter the setter
165     * @return a reference to this builder to enable method chaining
166     */
167    public JavaBeanFloatPropertyBuilder setter(Method setter) {
168        helper.setter(setter);
169        return this;
170    }
171}