001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements.  See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache License, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License.  You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the License for the specific language governing permissions and
015     * limitations under the License.
016     */
017    
018    package org.apache.commons.pool;
019    
020    import java.util.List;
021    import java.util.Collections;
022    import java.util.Arrays;
023    
024    /**
025     * Holds method names, parameters, and return values for tracing method calls.
026     *
027     * @author Sandy McArthur
028     * @version $Revision: 480413 $ $Date: 2006-11-28 22:16:05 -0700 (Tue, 28 Nov 2006) $
029     */
030    public class MethodCall {
031        private final String name;
032        private final List params;
033        private Object returned;
034    
035        public MethodCall(final String name) {
036            this(name, null);
037        }
038    
039        public MethodCall(final String name, final Object param) {
040            this(name, Collections.singletonList(param));
041        }
042    
043        public MethodCall(final String name, final Object param1, final Object param2) {
044            this(name, Arrays.asList(new Object[] {param1, param2}));
045        }
046    
047        public MethodCall(final String name, final List params) {
048            if (name == null) {
049                throw new IllegalArgumentException("name must not be null.");
050            }
051            this.name = name;
052            if (params != null) {
053                this.params = params;
054            } else {
055                this.params = Collections.EMPTY_LIST;
056            }
057        }
058    
059        public String getName() {
060            return name;
061        }
062    
063        public List getParams() {
064            return params;
065        }
066    
067        public Object getReturned() {
068            return returned;
069        }
070    
071        public void setReturned(final Object returned) {
072            this.returned = returned;
073        }
074    
075        public MethodCall returned(Object obj) {
076            setReturned(obj);
077            return this;
078        }
079    
080        public boolean equals(final Object o) {
081            if (this == o) return true;
082            if (o == null || getClass() != o.getClass()) return false;
083    
084            final MethodCall that = (MethodCall)o;
085    
086            if (name != null ? !name.equals(that.name) : that.name != null) return false;
087            if (params != null ? !params.equals(that.params) : that.params != null) return false;
088            if (returned != null ? !returned.equals(that.returned) : that.returned != null) return false;
089    
090            return true;
091        }
092    
093        public int hashCode() {
094            int result;
095            result = (name != null ? name.hashCode() : 0);
096            result = 29 * result + (params != null ? params.hashCode() : 0);
097            result = 29 * result + (returned != null ? returned.hashCode() : 0);
098            return result;
099        }
100    
101        public String toString() {
102            final StringBuffer sb = new StringBuffer();
103            sb.append("MethodCall");
104            sb.append("{name='").append(name).append('\'');
105            if (!params.isEmpty()) {
106                sb.append(", params=").append(params);
107            }
108            if (returned != null) {
109                sb.append(", returned=").append(returned);
110            }
111            sb.append('}');
112            return sb.toString();
113        }
114    }