001    /*
002     *  jDTAUS Core API
003     *  Copyright (c) 2005 Christian Schulte
004     *
005     *  Christian Schulte, Haldener Strasse 72, 58095 Hagen, Germany
006     *  <schulte2005@users.sourceforge.net> (+49 2331 3543887)
007     *
008     *  This library is free software; you can redistribute it and/or
009     *  modify it under the terms of the GNU Lesser General Public
010     *  License as published by the Free Software Foundation; either
011     *  version 2.1 of the License, or any later version.
012     *
013     *  This library is distributed in the hope that it will be useful,
014     *  but WITHOUT ANY WARRANTY; without even the implied warranty of
015     *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
016     *  Lesser General Public License for more details.
017     *
018     *  You should have received a copy of the GNU Lesser General Public
019     *  License along with this library; if not, write to the Free Software
020     *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
021     *
022     */
023    package org.jdtaus.core.container;
024    
025    import java.io.Serializable;
026    
027    /**
028     * Property meta-data.
029     * <p>A property consists of the properties {@code name}, {@code type} and
030     * {@code value}. Property {@code name} holds the name uniquely identifying the
031     * property in a collection of properties. Property {@code type} holds the type
032     * of the property. Property {@code value} holds the properties value which is
033     * of type {@code type}. The {@code api} flag indicates if the property is part
034     * of a public API.</p>
035     *
036     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a>
037     * @version $Id: Property.java 8044 2009-07-02 01:29:05Z schulte2005 $
038     */
039    public class Property extends ModelObject implements Cloneable, Serializable
040    {
041        //--Constants---------------------------------------------------------------
042    
043        /** Serial version UID for backwards compatibility with 1.0.x classes. */
044        private static final long serialVersionUID = -5450366074586725711L;
045    
046        //---------------------------------------------------------------Constants--
047        //--Property----------------------------------------------------------------
048    
049        /**
050         * The name of the property.
051         * @serial
052         */
053        private String name;
054    
055        /**
056         * The type of the property.
057         * @serial
058         */
059        private Class type;
060    
061        /**
062         * The value of the property.
063         * @serial
064         */
065        private Object value;
066    
067        /**
068         * Flag indicating that the property is part of a public API.
069         * @serial
070         */
071        private boolean api;
072    
073        /**
074         * Gets the name of the property.
075         *
076         * @return the name of the property.
077         */
078        public String getName()
079        {
080            if ( this.name == null )
081            {
082                this.name = "";
083            }
084    
085            return this.name;
086        }
087    
088        /**
089         * Setter for property {@code name}.
090         *
091         * @param value the new name of the property.
092         */
093        public void setName( final String value )
094        {
095            this.name = value;
096        }
097    
098        /**
099         * Gets the type of the property.
100         *
101         * @return the type of the property.
102         */
103        public Class getType()
104        {
105            if ( this.type == null )
106            {
107                this.type = String.class;
108            }
109    
110            return this.type;
111        }
112    
113        /**
114         * Setter for property {@code type}.
115         *
116         * @param value the new type of the property.
117         */
118        public void setType( final Class value )
119        {
120            this.type = value;
121        }
122    
123        /**
124         * Gets the value of the property.
125         *
126         * @return the value of the property or {@code null}.
127         */
128        public Object getValue()
129        {
130            return this.value;
131        }
132    
133        /**
134         * Setter for property {@code value}.
135         *
136         * @param value the new value of the property.
137         */
138        public void setValue( final Object value )
139        {
140            this.value = value;
141        }
142    
143        /**
144         * Gets the flag indicating if the property is part of a public API.
145         *
146         * @return {@code true} if the property is part of a public API.
147         */
148        public boolean isApi()
149        {
150            return this.api;
151        }
152    
153        /**
154         * Setter for property {@code api}.
155         *
156         * @param value {@code true} if the property is part of a public API.
157         */
158        public void setApi( final boolean value )
159        {
160            this.api = value;
161        }
162    
163        /**
164         * Creates a string representing the properties of the instance.
165         *
166         * @return a string representing the properties of the instance.
167         */
168        private String internalString()
169        {
170            return new StringBuffer( 500 ).append( '{' ).
171                append( super.internalString( this ) ).
172                append( ", api=" ).append( this.api ).
173                append( ", name=" ).append( this.name ).
174                append( ", type=" ).append( this.type ).
175                append( ", value=" ).append( this.value ).
176                append( '}' ).toString();
177    
178        }
179    
180        //----------------------------------------------------------------Property--
181        //--Object------------------------------------------------------------------
182    
183        /**
184         * Returns a string representation of the object.
185         *
186         * @return a string representation of the object.
187         */
188        public String toString()
189        {
190            return super.toString() + this.internalString();
191        }
192    
193        /**
194         * Indicates whether some other object is equal to this one by comparing
195         * the values of all properties.
196         *
197         * @param o the reference object with which to compare.
198         *
199         * @return {@code true} if this object is the same as {@code o};
200         * {@code false} otherwise.
201         */
202        public boolean equals( final Object o )
203        {
204            boolean equal = this == o;
205    
206            if ( !equal && o instanceof Property )
207            {
208                final Property that = (Property) o;
209                equal = this.getName().equals( that.getName() ) &&
210                    this.getType().equals( that.getType() ) &&
211                    ( this.getValue() == null
212                    ? that.getValue() == null
213                    : this.getValue().equals( that.getValue() ) );
214    
215            }
216    
217            return equal;
218        }
219    
220        /**
221         * Returns a hash code value for this object.
222         *
223         * @return a hash code value for this object.
224         */
225        public int hashCode()
226        {
227            return this.getName().hashCode() +
228                this.getType().hashCode() +
229                ( this.getValue() == null ? 0 : this.getValue().hashCode() );
230    
231        }
232    
233        /**
234         * Creates and returns a copy of this object. This method  performs a
235         * "shallow copy" of this object, not a "deep copy" operation.
236         *
237         * @return a clone of this instance.
238         */
239        public Object clone()
240        {
241            try
242            {
243                return super.clone();
244            }
245            catch ( CloneNotSupportedException e )
246            {
247                throw new AssertionError( e );
248            }
249        }
250    
251        //------------------------------------------------------------------Object--
252    }