View Javadoc

1   /*
2    * Copyright 2004 Sun Microsystems, Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   */
17  package com.sun.syndication.common;
18  
19  import java.io.Serializable;
20  
21  /***
22   * Convenience class providing clone(), toString(), equals() and hashCode() functionality for Java Beans.
23   * <p>
24   * It works on all read/write properties, recursively.
25   * <p>
26   * It uses the CloneableBean, EqualsBean and ToStringBean classes in a delegation pattern.
27   * <p>
28   * <h3>ObjectBean programming conventions</h3>
29   * <P>
30   * All ObjectBean subclasses having properties that return collections they should never
31   * return null if the property has been set to <b>null</b> or if a collection has not been set.
32   * They should create and return an empty collection, this empty collection instance should
33   * also be set to the corresponding property.
34   * <P>
35   * All ObjectBean subclasses properties should be live references.
36   * <p>
37   * @author Alejandro Abdelnur
38   *
39   */
40  public class ObjectBean implements Serializable, Cloneable, ToString {
41      private EqualsBean _equalsBean;
42      private ToStringBean _toStringBean;
43      private CloneableBean _cloneableBean;
44  
45      /***
46       * Default constructor.
47       * <p>
48       * @param beanClass the class/interface to be used for property scanning.
49       *
50       */
51      protected ObjectBean(Class beanClass) {
52          _equalsBean = new EqualsBean(beanClass,this);
53          _toStringBean = new ToStringBean(beanClass,this);
54          _cloneableBean = new CloneableBean(this);
55      }
56  
57      /***
58       * Creates a deep 'bean' clone of the object.
59       * <p>
60       * @return a clone of the object.
61       * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
62       *
63       */
64      public Object clone() throws CloneNotSupportedException {
65          return _cloneableBean.beanClone();
66      }
67  
68      /***
69       * Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
70       * <p>
71       * @param other he reference object with which to compare.
72       * @return <b>true</b> if 'this' object is equal to the 'other' object.
73       *
74       */
75      public boolean equals(Object other) {
76          return _equalsBean.beanEquals(other);
77      }
78  
79      /***
80       * Returns a hashcode value for the object.
81       * <p>
82       * It follows the contract defined by the Object hashCode() method.
83       * <p>
84       * @return the hashcode of the bean object.
85       *
86       */
87      public int hashCode() {
88          return _equalsBean.beanHashCode();
89      }
90  
91      /***
92       * Returns the String representation for the object.
93       * <p>
94       * @return String representation for the object.
95       *
96       */
97      public String toString() {
98          return _toStringBean.toString();
99      }
100 
101     /***
102      * Returns the String representation for the bean using a prefix.
103      * <p>
104      * This method is used by ToString implementations.
105      * <p>
106      * @param prefix prefix to use in the String representation.
107      * @return String representation for the bean using the given prefix.
108      *
109      */
110     public String toString(String prefix) {
111         return _toStringBean.toString(prefix);
112     }
113 
114 }
115