001/* 002 GRANITE DATA SERVICES 003 Copyright (C) 2011 GRANITE DATA SERVICES S.A.S. 004 005 This file is part of Granite Data Services. 006 007 Granite Data Services is free software; you can redistribute it and/or modify 008 it under the terms of the GNU Library General Public License as published by 009 the Free Software Foundation; either version 2 of the License, or (at your 010 option) any later version. 011 012 Granite Data Services is distributed in the hope that it will be useful, but 013 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 014 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License 015 for more details. 016 017 You should have received a copy of the GNU Library General Public License 018 along with this library; if not, see <http://www.gnu.org/licenses/>. 019*/ 020 021package org.granite.util; 022 023import java.lang.reflect.Method; 024 025public class PropertyDescriptor { 026 027 private String name; 028 private Method getter; 029 private Method setter; 030 031 public PropertyDescriptor(String propertyName, Method getter, Method setter) { 032 this.name = propertyName; 033 setReadMethod(getter); 034 setWriteMethod(setter); 035 } 036 037 public String getName() { 038 return this.name; 039 } 040 041 public void setWriteMethod(Method setter) { 042 this.setter = setter; 043 } 044 045 public void setReadMethod(Method getter) { 046 this.getter = getter; 047 } 048 049 public Method getWriteMethod() { 050 return setter; 051 } 052 053 public Method getReadMethod() { 054 return getter; 055 } 056 057 @Override 058 public boolean equals(Object object) { 059 if (!(object instanceof PropertyDescriptor)) 060 return false; 061 062 PropertyDescriptor pd = (PropertyDescriptor)object; 063 if (!((this.getter == null && pd.getter == null) 064 || (this.getter != null && this.getter.equals(pd.getter)))) 065 return false; 066 067 if (!((this.setter == null && pd.setter == null) 068 || (this.setter != null && this.setter.equals(pd.setter)))) 069 return false; 070 071 return this.getPropertyType() == pd.getPropertyType(); 072 } 073 074 @Override 075 public int hashCode() { 076 int hashCode = getter != null ? getter.hashCode() : 0; 077 if (setter != null) 078 hashCode = hashCode*31 + setter.hashCode(); 079 if (getPropertyType() != null) 080 hashCode = hashCode*31 + getPropertyType().hashCode(); 081 return hashCode; 082 } 083 084 public Class<?> getPropertyType() { 085 if (getter != null) 086 return getter.getReturnType(); 087 if (setter != null) { 088 Class<?>[] parameterTypes = setter.getParameterTypes(); 089 return parameterTypes[0]; 090 } 091 return null; 092 } 093}