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