View Javadoc

1   package org.apache.torque.manager;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.Serializable;
23  
24  import org.apache.commons.lang.builder.EqualsBuilder;
25  import org.apache.commons.lang.builder.HashCodeBuilder;
26  
27  /**
28   * The key for the MethodResultCache.
29   *
30   * @version $Id: MethodCacheKey.java 1379317 2012-08-31 06:56:48Z tfischer $
31   */
32  public class MethodCacheKey implements Serializable
33  {
34      /**
35       * Serial version
36       */
37      private static final long serialVersionUID = -1831486431185021200L;
38  
39      /**
40       * The Object on which the method is invoked.
41       * If the method is static, a String representing the class name is used.
42       */
43      private Serializable instanceOrClass;
44  
45      /** The method name. */
46      private String method;
47  
48      /** Optional arguments for the method. */
49      private Serializable[] args;
50  
51      /** The cache group key. */
52      private String groupKey;
53  
54      public MethodCacheKey()
55      {
56          // empty
57      }
58  
59      /**
60       * Construct key
61       *
62       * @param instanceOrClass the Object on which the method is invoked.  if
63       * the method is static, a String representing the class name is used.
64       * @param method the method name
65       * @param arg optional arguments for the method
66       */
67      public MethodCacheKey(Serializable instanceOrClass, String method, Serializable ... arg)
68      {
69          init(instanceOrClass, method, arg);
70      }
71  
72      /**
73       * Initialize the key
74       *
75       * @param instanceOrClass the Object on which the method is invoked.  if
76       * the method is static, a String representing the class name is used.
77       * @param method the method name
78       * @param arg optional arguments for the method
79       */
80      public void init(Serializable instanceOrClass, String method, Serializable ... arg)
81      {
82          this.instanceOrClass = instanceOrClass;
83          this.method = method;
84          groupKey = instanceOrClass.toString() + method;
85          this.args = arg;
86      }
87  
88      /**
89       * Return the group key
90       *
91       * @return the group key
92       */
93      public String getGroupKey()
94      {
95          return groupKey;
96      }
97  
98      @Override
99      public boolean equals(Object obj)
100     {
101         if (this == obj)
102         {
103             return true;
104         }
105         if (obj == null)
106         {
107             return false;
108         }
109         if (obj.getClass() != this.getClass())
110         {
111             return false;
112         }
113 
114         MethodCacheKey methodCacheKey = (MethodCacheKey) obj;
115         EqualsBuilder equalsBuilder = new EqualsBuilder();
116         equalsBuilder.append(methodCacheKey.method, method)
117                 .append(methodCacheKey.instanceOrClass, instanceOrClass)
118                 .append(methodCacheKey.args, args);
119         return equalsBuilder.isEquals();
120     }
121 
122     @Override
123     public int hashCode()
124     {
125         HashCodeBuilder hashCodeBuilder = new HashCodeBuilder();
126         hashCodeBuilder.append(method)
127                 .append(instanceOrClass)
128                 .append(args);
129         return hashCodeBuilder.toHashCode();
130     }
131 
132     @Override
133     public String toString()
134     {
135         StringBuilder sb = new StringBuilder();
136         sb.append(instanceOrClass);
137         sb.append("::");
138         sb.append(method).append('(');
139         for (int i = 0; i < args.length; i++)
140         {
141             sb.append(args[i]);
142             if (i < args.length - 1)
143             {
144                 sb.append(", ");
145             }
146         }
147         sb.append(')');
148         return sb.toString();
149     }
150 }