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  import java.util.Set;
21  
22  /***
23   * Convenience class providing clone(), toString(), equals() and hashCode() functionality for Java Beans.
24   * <p>
25   * It works on all read/write properties, recursively.
26   * <p>
27   * It uses the CloneableBean, EqualsBean and ToStringBean classes in a delegation pattern.
28   * <p>
29   * <h3>ObjectBean programming conventions</h3>
30   * <P>
31   * All ObjectBean subclasses having properties that return collections they should never
32   * return null if the property has been set to <b>null</b> or if a collection has not been set.
33   * They should create and return an empty collection, this empty collection instance should
34   * also be set to the corresponding property.
35   * <P>
36   * All ObjectBean subclasses properties should be live references.
37   * <p>
38   * @author Alejandro Abdelnur
39   *
40   */
41  public class ObjectBean implements Serializable, Cloneable, ToString {
42      private EqualsBean _equalsBean;
43      private ToStringBean _toStringBean;
44      private CloneableBean _cloneableBean;
45  
46      /***
47       * Constructor.
48       * <p>
49       * @param beanClass the class/interface to be used for property scanning.
50       *
51       */
52      protected ObjectBean(Class beanClass) {
53          this(beanClass,null);
54      }
55  
56      /***
57       * Constructor.
58       * <p>
59       * The property names in the ignoreProperties Set will not be copied into
60       * the cloned instance. This is useful for cases where the Bean has convenience
61       * properties (properties that are actually references to other properties or
62       * properties of properties). For example SyndFeed and SyndEntry beans have
63       * convenience properties, publishedDate, author, copyright and categories all
64       * of them mapped to properties in the DC Module.
65       * <p>
66       * @param beanClass the class/interface to be used for property scanning.
67       * @param ignoreProperties properties to ignore when cloning.
68       *
69       */
70      protected ObjectBean(Class beanClass,Set ignoreProperties) {
71          _equalsBean = new EqualsBean(beanClass,this);
72          _toStringBean = new ToStringBean(beanClass,this);
73          _cloneableBean = new CloneableBean(this,ignoreProperties);
74      }
75  
76      /***
77       * Creates a deep 'bean' clone of the object.
78       * <p>
79       * @return a clone of the object.
80       * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
81       *
82       */
83      public Object clone() throws CloneNotSupportedException {
84          return _cloneableBean.beanClone();
85      }
86  
87      /***
88       * Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
89       * <p>
90       * @param other he reference object with which to compare.
91       * @return <b>true</b> if 'this' object is equal to the 'other' object.
92       *
93       */
94      public boolean equals(Object other) {
95          return _equalsBean.beanEquals(other);
96      }
97  
98      /***
99       * Returns a hashcode value for the object.
100      * <p>
101      * It follows the contract defined by the Object hashCode() method.
102      * <p>
103      * @return the hashcode of the bean object.
104      *
105      */
106     public int hashCode() {
107         return _equalsBean.beanHashCode();
108     }
109 
110     /***
111      * Returns the String representation for the object.
112      * <p>
113      * @return String representation for the object.
114      *
115      */
116     public String toString() {
117         return _toStringBean.toString();
118     }
119 
120     /***
121      * Returns the String representation for the bean using a prefix.
122      * <p>
123      * This method is used by ToString implementations.
124      * <p>
125      * @param prefix prefix to use in the String representation.
126      * @return String representation for the bean using the given prefix.
127      *
128      */
129     public String toString(String prefix) {
130         return _toStringBean.toString(prefix);
131     }
132 
133 }
134