View Javadoc

1   /*
2    *  Copyright 2001-2013 Stephen Colebourne
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  package org.joda.beans.impl;
17  
18  import java.util.Set;
19  
20  import org.joda.beans.Bean;
21  import org.joda.beans.JodaBeanUtils;
22  import org.joda.beans.Property;
23  
24  /**
25   * Basic implementation of {@code Bean} intended for applications to subclass.
26   * <p>
27   * The subclass must to provide an implementation for {@link Bean#metaBean()}.
28   * This returns the complete definition of the bean at the meta level.
29   * 
30   * @author Stephen Colebourne
31   */
32  public abstract class BasicBean implements Bean {
33  
34      @Override
35      public <R> Property<R> property(String propertyName) {
36          return metaBean().<R>metaProperty(propertyName).createProperty(this);
37      }
38  
39      @Override
40      public Set<String> propertyNames() {
41          return metaBean().metaPropertyMap().keySet();
42      }
43  
44      //-----------------------------------------------------------------------
45      /**
46       * Checks if this bean equals another.
47       * <p>
48       * This compares the class and all the properties of the bean.
49       * 
50       * @param obj  the object to compare to, null returns false
51       * @return true if the beans are equal
52       */
53      @Override
54      public boolean equals(Object obj) {
55          if (obj == this) {
56              return true;
57          }
58          if (obj != null && getClass() == obj.getClass()) {
59              Bean other = (Bean) obj;
60              return JodaBeanUtils.propertiesEqual(this, other);
61          }
62          return false;
63      }
64  
65      /**
66       * Returns a suitable hash code.
67       * <p>
68       * The hash code is derived from all the properties of the bean.
69       * 
70       * @return a suitable hash code
71       */
72      @Override
73      public int hashCode() {
74          return getClass().hashCode() ^ JodaBeanUtils.propertiesHashCode(this);
75      }
76  
77      /**
78       * Returns a string that summarises the bean.
79       * <p>
80       * The string contains the class name and properties.
81       * 
82       * @return a summary string, not null
83       */
84      @Override
85      public String toString() {
86          return JodaBeanUtils.propertiesToString(this, metaBean().beanType().getSimpleName());
87      }
88  
89  }