001    /**
002     *   GRANITE DATA SERVICES
003     *   Copyright (C) 2006-2013 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     */
022    
023    package org.granite.util;
024    
025    import java.lang.reflect.Method;
026    
027    public class PropertyDescriptor {
028            
029            private String name;
030        private Method getter;
031        private Method setter;
032    
033        public PropertyDescriptor(String propertyName, Method getter, Method setter) {
034            this.name = propertyName;
035            setReadMethod(getter);
036            setWriteMethod(setter);
037        }
038        
039        public String getName() {
040            return this.name;
041        }
042    
043        public void setWriteMethod(Method setter) {
044            this.setter = setter;
045        }
046    
047        public void setReadMethod(Method getter) {
048            this.getter = getter;
049        }
050    
051        public Method getWriteMethod() {
052            return setter;
053        }
054    
055        public Method getReadMethod() {
056            return getter;
057        }
058    
059        @Override
060        public boolean equals(Object object) {
061            if (!(object instanceof PropertyDescriptor))
062                    return false;
063    
064            PropertyDescriptor pd = (PropertyDescriptor)object;
065            if (!((this.getter == null && pd.getter == null) 
066                            || (this.getter != null && this.getter.equals(pd.getter))))
067                    return false;
068            
069            if (!((this.setter == null && pd.setter == null) 
070                            || (this.setter != null && this.setter.equals(pd.setter))))
071                    return false;
072            
073            return this.getPropertyType() == pd.getPropertyType();
074        }
075    
076        @Override
077        public int hashCode() {
078            int hashCode = getter != null ? getter.hashCode() : 0;
079            if (setter != null)
080                    hashCode = hashCode*31 + setter.hashCode();
081            if (getPropertyType() != null)
082                    hashCode = hashCode*31 + getPropertyType().hashCode();
083            return hashCode;
084        }
085    
086        public Class<?> getPropertyType() {
087            if (getter != null)
088                return getter.getReturnType();
089            if (setter != null) {
090                Class<?>[] parameterTypes = setter.getParameterTypes();
091                return parameterTypes[0];
092            }
093            return null;
094        }
095    }