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.pool2;
19  
20  import java.util.Arrays;
21  import java.util.Collections;
22  import java.util.List;
23  import java.util.Objects;
24  
25  /**
26   * Holds method names, parameters, and return values for tracing method calls.
27   */
28  public class MethodCall {
29      private final String name;
30      private final List<Object> params;
31      private Object returned;
32  
33      public MethodCall(final String name) {
34          this(name, null);
35      }
36  
37      public MethodCall(final String name, final List<Object> params) {
38          if (name == null) {
39              throw new IllegalArgumentException("name must not be null.");
40          }
41          this.name = name;
42          if (params != null) {
43              this.params = params;
44          } else {
45              this.params = Collections.emptyList();
46          }
47      }
48  
49      public MethodCall(final String name, final Object param) {
50          this(name, Collections.singletonList(param));
51      }
52  
53      public MethodCall(final String name, final Object param1, final Object param2) {
54          this(name, Arrays.asList(param1, param2));
55      }
56  
57      @Override
58      public boolean equals(final Object o) {
59          if (this == o) {
60              return true;
61          }
62          if (o == null || getClass() != o.getClass()) {
63              return false;
64          }
65  
66          final MethodCall that = (MethodCall)o;
67  
68          if (!Objects.equals(name, that.name)) {
69              return false;
70          }
71          if (!Objects.equals(params, that.params)) {
72              return false;
73          }
74          return Objects.equals(returned, that.returned);
75      }
76  
77      public String getName() {
78          return name;
79      }
80  
81      public List<Object> getParams() {
82          return params;
83      }
84  
85      public Object getReturned() {
86          return returned;
87      }
88  
89      @Override
90      public int hashCode() {
91          int result;
92          result = name != null ? name.hashCode() : 0;
93          result = 29 * result + (params != null ? params.hashCode() : 0);
94          result = 29 * result + (returned != null ? returned.hashCode() : 0);
95          return result;
96      }
97  
98      public MethodCall returned(final Object obj) {
99          setReturned(obj);
100         return this;
101     }
102 
103     public void setReturned(final Object returned) {
104         this.returned = returned;
105     }
106 
107     @Override
108     public String toString() {
109         final StringBuilder sb = new StringBuilder();
110         sb.append("MethodCall");
111         sb.append("{name='").append(name).append('\'');
112         if (!params.isEmpty()) {
113             sb.append(", params=").append(params);
114         }
115         if (returned != null) {
116             sb.append(", returned=").append(returned);
117         }
118         sb.append('}');
119         return sb.toString();
120     }
121 }