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       *
48       */
49      protected ObjectBean() {
50          _equalsBean = new EqualsBean(this);
51          _toStringBean = new ToStringBean(this);
52          _cloneableBean = new CloneableBean(this);
53      }
54  
55      /***
56       * Creates a deep 'bean' clone of the object.
57       * <p>
58       * @return a clone of the object.
59       * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
60       *
61       */
62      public Object clone() throws CloneNotSupportedException {
63          return _cloneableBean.beanClone();
64      }
65  
66      /***
67       * Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
68       * <p>
69       * @param other he reference object with which to compare.
70       * @return <b>true</b> if 'this' object is equal to the 'other' object.
71       *
72       */
73      public boolean equals(Object other) {
74          return _equalsBean.beanEquals(other);
75      }
76  
77      /***
78       * Returns a hashcode value for the object.
79       * <p>
80       * It follows the contract defined by the Object hashCode() method.
81       * <p>
82       * @return the hashcode of the bean object.
83       *
84       */
85      public int hashCode() {
86          return _equalsBean.beanHashCode();
87      }
88  
89      /***
90       * Returns the String representation for the object.
91       * <p>
92       * @return String representation for the object.
93       *
94       */
95      public String toString() {
96          return _toStringBean.toString();
97      }
98  
99      /***
100      * Returns the String representation for the bean using a prefix.
101      * <p>
102      * This method is used by ToString implementations.
103      * <p>
104      * @param prefix prefix to use in the String representation.
105      * @return String representation for the bean using the given prefix.
106      *
107      */
108     public String toString(String prefix) {
109         return _toStringBean.toString(prefix);
110     }
111 
112 }
113