001package jmri.beans;
002
003import java.util.Set;
004
005/**
006 * Generic implementation of {@link jmri.beans.BeanInterface} without support
007 * for arbitrary properties defined at runtime.
008 * <p>
009 * <b>NOTE</b> This class does not implement
010 * {@link java.beans.PropertyChangeSupport}. Subclass {@link jmri.beans.Bean} if
011 * you need to support property change listeners.
012 *
013 * @author Randall Wood
014 */
015public abstract class UnboundBean implements BeanInterface {
016
017    /**
018     * Get value of element at <i>index</i> of property array <i>key</i>.
019     * <p>
020     * This implementation calls a read method for the indexed property using
021     * JavaBeans introspection, and assumes, based on JavaBeans coding patterns,
022     * that the read method has the following parameter: <code>index</code>.
023     *
024     * Note that this method returns null instead of throwing
025     * {@link java.lang.ArrayIndexOutOfBoundsException} if the index is invalid
026     * since the Java introspection methods provide no reliable way to get the
027     * size of the indexed property.
028     *
029     * @return value of element or null
030     */
031    @Override
032    public Object getIndexedProperty(String key, int index) {
033        return BeanUtil.getIntrospectedIndexedProperty(this, key, index);
034    }
035
036    /**
037     * Get the value of property key.
038     * <p>
039     * If <i>null</i> is a valid (or expected) value for <i>key</i>, you might
040     * want to use {@link Bean#hasProperty(java.lang.String)} to test that the
041     * property exists.
042     *
043     * @return value of key or null.
044     * @see BeanInterface#getProperty(java.lang.String)
045     */
046    @Override
047    public Object getProperty(String key) {
048        return BeanUtil.getIntrospectedProperty(this, key);
049    }
050
051    /**
052     * Return a list of property names.
053     *
054     * @return a Set of names
055     * @see BeanInterface#getPropertyNames()
056     */
057    @Override
058    public Set<String> getPropertyNames() {
059        return BeanUtil.getIntrospectedPropertyNames(this);
060    }
061
062    /**
063     * Test if a property exists.
064     *
065     * @return true if property exists
066     * @see BeanInterface#hasProperty(java.lang.String)
067     */
068    @Override
069    public boolean hasProperty(String key) {
070        return BeanUtil.hasIntrospectedProperty(this, key);
071    }
072
073    @Override
074    public boolean hasIndexedProperty(String key) {
075        return BeanUtil.hasIntrospectedIndexedProperty(this, key);
076    }
077
078    /**
079     * Set element at <i>index</i> of property array <i>key</i> to <i>value</i>.
080     * <p>
081     * This implementation calls a write method for the indexed property using
082     * JavaBeans introspection, and assumes, based on JavaBeans coding patterns,
083     * that the write method has the following two parameters in order:
084     * <code>index</code>, <code>value</code>.
085     *
086     * @see BeanInterface#setIndexedProperty(java.lang.String, int,
087     * java.lang.Object)
088     */
089    @Override
090    public void setIndexedProperty(String key, int index, Object value) {
091        BeanUtil.setIntrospectedIndexedProperty(this, key, index, value);
092    }
093
094    /**
095     * Set property <i>key</i> to <i>value</i>.
096     * <p>
097     * This implementation calls a write method for the property using
098     * JavaBeans introspection.
099     *
100     * @see BeanInterface#setProperty(java.lang.String, java.lang.Object)
101     */
102    @Override
103    public void setProperty(String key, Object value) {
104        BeanUtil.setIntrospectedProperty(this, key, value);
105    }
106}