001: /*
002: * Copyright 2004 Sun Microsystems, Inc.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: *
016: */
017: package com.sun.syndication.feed.impl;
018:
019: import com.sun.syndication.feed.impl.CloneableBean;
020: import com.sun.syndication.feed.impl.EqualsBean;
021:
022: import java.io.Serializable;
023: import java.util.Set;
024:
025: /**
026: * Convenience class providing clone(), toString(), equals() and hashCode() functionality for Java Beans.
027: * <p>
028: * It works on all read/write properties, recursively.
029: * <p>
030: * It uses the CloneableBean, EqualsBean and ToStringBean classes in a delegation pattern.
031: * <p>
032: * <h3>ObjectBean programming conventions</h3>
033: * <P>
034: * All ObjectBean subclasses having properties that return collections they should never
035: * return null if the property has been set to <b>null</b> or if a collection has not been set.
036: * They should create and return an empty collection, this empty collection instance should
037: * also be set to the corresponding property.
038: * <P>
039: * All ObjectBean subclasses properties should be live references.
040: * <p>
041: * @author Alejandro Abdelnur
042: *
043: */
044: public class ObjectBean implements Serializable, Cloneable {
045: private EqualsBean _equalsBean;
046: private ToStringBean _toStringBean;
047: private CloneableBean _cloneableBean;
048:
049: /**
050: * Constructor.
051: * <p>
052: * @param beanClass the class/interface to be used for property scanning.
053: *
054: */
055: public ObjectBean(Class beanClass, Object obj) {
056: this (beanClass, obj, null);
057: }
058:
059: /**
060: * Constructor.
061: * <p>
062: * The property names in the ignoreProperties Set will not be copied into
063: * the cloned instance. This is useful for cases where the Bean has convenience
064: * properties (properties that are actually references to other properties or
065: * properties of properties). For example SyndFeed and SyndEntry beans have
066: * convenience properties, publishedDate, author, copyright and categories all
067: * of them mapped to properties in the DC Module.
068: * <p>
069: * @param beanClass the class/interface to be used for property scanning.
070: * @param ignoreProperties properties to ignore when cloning.
071: *
072: */
073: public ObjectBean(Class beanClass, Object obj, Set ignoreProperties) {
074: _equalsBean = new EqualsBean(beanClass, obj);
075: _toStringBean = new ToStringBean(beanClass, obj);
076: _cloneableBean = new CloneableBean(obj, ignoreProperties);
077: }
078:
079: /**
080: * Creates a deep 'bean' clone of the object.
081: * <p>
082: * @return a clone of the object.
083: * @throws CloneNotSupportedException thrown if an element of the object cannot be cloned.
084: *
085: */
086: public Object clone() throws CloneNotSupportedException {
087: return _cloneableBean.beanClone();
088: }
089:
090: /**
091: * Indicates whether some other object is "equal to" this one as defined by the Object equals() method.
092: * <p>
093: * @param other he reference object with which to compare.
094: * @return <b>true</b> if 'this' object is equal to the 'other' object.
095: *
096: */
097: public boolean equals(Object other) {
098: return _equalsBean.beanEquals(other);
099: }
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: }
|