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    *      https://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          final MethodCall that = (MethodCall) o;
66          if (!Objects.equals(name, that.name) || !Objects.equals(params, that.params)) {
67              return false;
68          }
69          return Objects.equals(returned, that.returned);
70      }
71  
72      public String getName() {
73          return name;
74      }
75  
76      public List<Object> getParams() {
77          return params;
78      }
79  
80      public Object getReturned() {
81          return returned;
82      }
83  
84      @Override
85      public int hashCode() {
86          int result;
87          result = name != null ? name.hashCode() : 0;
88          result = 29 * result + (params != null ? params.hashCode() : 0);
89          result = 29 * result + (returned != null ? returned.hashCode() : 0);
90          return result;
91      }
92  
93      public MethodCall returned(final Object obj) {
94          setReturned(obj);
95          return this;
96      }
97  
98      public void setReturned(final Object returned) {
99          this.returned = returned;
100     }
101 
102     @Override
103     public String toString() {
104         final StringBuilder sb = new StringBuilder();
105         sb.append("MethodCall");
106         sb.append("{name='").append(name).append('\'');
107         if (!params.isEmpty()) {
108             sb.append(", params=").append(params);
109         }
110         if (returned != null) {
111             sb.append(", returned=").append(returned);
112         }
113         sb.append('}');
114         return sb.toString();
115     }
116 }