1   /**
2    * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
3    *
4    * This library is free software; you can redistribute it and/or modify it under
5    * the terms of the GNU Lesser General Public License as published by the Free
6    * Software Foundation; either version 2.1 of the License, or (at your option)
7    * any later version.
8    *
9    * This library is distributed in the hope that it will be useful, but WITHOUT
10   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11   * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12   * details.
13   */
14  
15  package com.liferay.portal.cache.key;
16  
17  import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
18  import com.liferay.portal.kernel.util.StringBundler;
19  
20  /**
21   * <a href="HashCodeCacheKeyGenerator.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Michael C. Han
24   * @author Shuyang Zhou
25   */
26  public class HashCodeCacheKeyGenerator extends BaseCacheKeyGenerator {
27  
28      public CacheKeyGenerator clone() {
29          return new HashCodeCacheKeyGenerator();
30      }
31  
32      public String getCacheKey(String key) {
33          return String.valueOf(key.hashCode());
34      }
35  
36      public String getCacheKey(String[] keys) {
37          int hashCode = 0;
38  
39          for (String key : keys) {
40              hashCode = 31 * hashCode + key.hashCode();
41          }
42  
43          return String.valueOf(hashCode);
44      }
45  
46      public String getCacheKey(StringBundler sb) {
47          int hashCode = 0;
48  
49          for (int i = 0; i < sb.index(); i++) {
50              hashCode = 31 * hashCode + sb.stringAt(i).hashCode();
51          }
52  
53          return String.valueOf(hashCode);
54      }
55  
56  }