001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.kernel.util;
016    
017    import java.io.Serializable;
018    
019    import java.lang.reflect.Method;
020    
021    import java.util.Map;
022    
023    /**
024     * @author Brian Wing Shun Chan
025     */
026    public class MethodKey implements Serializable {
027    
028            public static MethodKey create(
029                            String className, String methodName, String[] parameterTypeNames)
030                    throws ClassNotFoundException {
031    
032                    Class<?>[] parameterTypes = new Class[parameterTypeNames.length];
033    
034                    for (int i = 0; i < parameterTypeNames.length; i++) {
035                            String parameterTypeName = parameterTypeNames[i];
036    
037                            parameterTypes[i] = Class.forName(parameterTypeName);
038                    }
039    
040                    return new MethodKey(className, methodName, parameterTypes);
041            }
042    
043            public MethodKey(
044                    String className, String methodName, Class<?>[] parameterTypes) {
045    
046                    this(null, null, className, methodName, parameterTypes);
047            }
048    
049            public MethodKey(
050                    Map<String, Class<?>> classesMap, Map<MethodKey, Method> methodsMap,
051                    String className, String methodName, Class<?>[] parameterTypes) {
052    
053                    _classesMap = classesMap;
054                    _methodsMap = methodsMap;
055                    _className = className;
056                    _methodName = methodName;
057                    _parameterTypes = parameterTypes;
058            }
059    
060            public Map<String, Class<?>> getClassesMap() {
061                    return _classesMap;
062            }
063    
064            public Map<MethodKey, Method> getMethodsMap() {
065                    return _methodsMap;
066            }
067    
068            public String getClassName() {
069                    return _className;
070            }
071    
072            public String getMethodName() {
073                    return _methodName;
074            }
075    
076            public Class<?>[] getParameterTypes() {
077                    return _parameterTypes;
078            }
079    
080            public boolean equals(Object obj) {
081                    if (obj == null) {
082                            return false;
083                    }
084    
085                    MethodKey methodKey = (MethodKey)obj;
086    
087                    if (toString().equals(methodKey.toString())) {
088                            return true;
089                    }
090                    else {
091                            return false;
092                    }
093            }
094    
095            public int hashCode() {
096                    return toString().hashCode();
097            }
098    
099            public String toString() {
100                    return _toString();
101            }
102    
103            private String _toString() {
104                    if (_toString == null) {
105                            StringBundler sb = new StringBundler();
106    
107                            sb.append(_className);
108                            sb.append(_methodName);
109    
110                            if ((_parameterTypes != null) && (_parameterTypes.length > 0)) {
111                                    sb.append(StringPool.DASH);
112    
113                                    for (Class<?> parameterType : _parameterTypes) {
114                                            sb.append(parameterType.getName());
115                                    }
116                            }
117    
118                            _toString = sb.toString();
119                    }
120    
121                    return _toString;
122            }
123    
124            private Map<String, Class<?>> _classesMap;
125            private Map<MethodKey, Method> _methodsMap;
126            private String _className;
127            private String _methodName;
128            private Class<?>[] _parameterTypes;
129            private String _toString;
130    
131    }