001/* 002 * Copyright 2001-2013 Stephen Colebourne 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 */ 016package org.joda.beans.impl; 017 018import java.util.Set; 019 020import org.joda.beans.Bean; 021import org.joda.beans.JodaBeanUtils; 022import org.joda.beans.Property; 023 024/** 025 * Basic implementation of {@code Bean} intended for applications to subclass. 026 * <p> 027 * The subclass must to provide an implementation for {@link Bean#metaBean()}. 028 * This returns the complete definition of the bean at the meta level. 029 * 030 * @author Stephen Colebourne 031 */ 032public abstract class BasicBean implements Bean { 033 034 @Override 035 public <R> Property<R> property(String propertyName) { 036 return metaBean().<R>metaProperty(propertyName).createProperty(this); 037 } 038 039 @Override 040 public Set<String> propertyNames() { 041 return metaBean().metaPropertyMap().keySet(); 042 } 043 044 //----------------------------------------------------------------------- 045 /** 046 * Checks if this bean equals another. 047 * <p> 048 * This compares the class and all the properties of the bean. 049 * 050 * @param obj the object to compare to, null returns false 051 * @return true if the beans are equal 052 */ 053 @Override 054 public boolean equals(Object obj) { 055 if (obj == this) { 056 return true; 057 } 058 if (obj != null && getClass() == obj.getClass()) { 059 Bean other = (Bean) obj; 060 return JodaBeanUtils.propertiesEqual(this, other); 061 } 062 return false; 063 } 064 065 /** 066 * Returns a suitable hash code. 067 * <p> 068 * The hash code is derived from all the properties of the bean. 069 * 070 * @return a suitable hash code 071 */ 072 @Override 073 public int hashCode() { 074 return getClass().hashCode() ^ JodaBeanUtils.propertiesHashCode(this); 075 } 076 077 /** 078 * Returns a string that summarises the bean. 079 * <p> 080 * The string contains the class name and properties. 081 * 082 * @return a summary string, not null 083 */ 084 @Override 085 public String toString() { 086 return JodaBeanUtils.propertiesToString(this, metaBean().beanType().getSimpleName()); 087 } 088 089}