View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.pool;
19  
20  import java.util.List;
21  import java.util.Collections;
22  import java.util.Arrays;
23  
24  /**
25   * Holds method names, parameters, and return values for tracing method calls.
26   *
27   * @author Sandy McArthur
28   * @version $Revision: 480413 $ $Date: 2006-11-28 22:16:05 -0700 (Tue, 28 Nov 2006) $
29   */
30  public class MethodCall {
31      private final String name;
32      private final List params;
33      private Object returned;
34  
35      public MethodCall(final String name) {
36          this(name, null);
37      }
38  
39      public MethodCall(final String name, final Object param) {
40          this(name, Collections.singletonList(param));
41      }
42  
43      public MethodCall(final String name, final Object param1, final Object param2) {
44          this(name, Arrays.asList(new Object[] {param1, param2}));
45      }
46  
47      public MethodCall(final String name, final List params) {
48          if (name == null) {
49              throw new IllegalArgumentException("name must not be null.");
50          }
51          this.name = name;
52          if (params != null) {
53              this.params = params;
54          } else {
55              this.params = Collections.EMPTY_LIST;
56          }
57      }
58  
59      public String getName() {
60          return name;
61      }
62  
63      public List getParams() {
64          return params;
65      }
66  
67      public Object getReturned() {
68          return returned;
69      }
70  
71      public void setReturned(final Object returned) {
72          this.returned = returned;
73      }
74  
75      public MethodCall returned(Object obj) {
76          setReturned(obj);
77          return this;
78      }
79  
80      public boolean equals(final Object o) {
81          if (this == o) return true;
82          if (o == null || getClass() != o.getClass()) return false;
83  
84          final MethodCall that = (MethodCall)o;
85  
86          if (name != null ? !name.equals(that.name) : that.name != null) return false;
87          if (params != null ? !params.equals(that.params) : that.params != null) return false;
88          if (returned != null ? !returned.equals(that.returned) : that.returned != null) return false;
89  
90          return true;
91      }
92  
93      public int hashCode() {
94          int result;
95          result = (name != null ? name.hashCode() : 0);
96          result = 29 * result + (params != null ? params.hashCode() : 0);
97          result = 29 * result + (returned != null ? returned.hashCode() : 0);
98          return result;
99      }
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 }