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