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.kernel.dao.orm;
16  
17  import com.liferay.portal.kernel.util.StringBundler;
18  import com.liferay.portal.kernel.util.StringPool;
19  
20  /**
21   * <a href="FinderPath.java.html"><b><i>View Source</i></b></a>
22   *
23   * @author Brian Wing Shun Chan
24   */
25  public class FinderPath {
26  
27      public FinderPath(
28          boolean entityCacheEnabled, boolean finderCacheEnabled,
29          String className, String methodName, String[] params) {
30  
31          _entityCacheEnabled = entityCacheEnabled;
32          _finderCacheEnabled = finderCacheEnabled;
33          _className = className;
34          _methodName = methodName;
35          _params = params;
36  
37          _initCacheKeyPrefix();
38          _initLocalCacheKeyPrefix();
39      }
40  
41      public String encodeCacheKey(Object[] args) {
42          StringBundler sb = new StringBundler(args.length * 2 + 1);
43  
44          sb.append(_cacheKeyPrefix);
45  
46          for (Object arg : args) {
47              sb.append(StringPool.PERIOD);
48              sb.append(String.valueOf(arg));
49          }
50  
51          return sb.toString();
52      }
53  
54      public String encodeLocalCacheKey(Object[] args) {
55          StringBundler sb = new StringBundler(args.length * 2 + 1);
56  
57          sb.append(_localCacheKeyPrefix);
58  
59          for (Object arg : args) {
60              sb.append(StringPool.PERIOD);
61              sb.append(String.valueOf(arg));
62          }
63  
64          return sb.toString();
65      }
66  
67      public String getClassName() {
68          return _className;
69      }
70  
71      public String getMethodName() {
72          return _methodName;
73      }
74  
75      public String[] getParams() {
76          return _params;
77      }
78  
79      public boolean isEntityCacheEnabled() {
80          return _entityCacheEnabled;
81      }
82  
83      public boolean isFinderCacheEnabled() {
84          return _finderCacheEnabled;
85      }
86  
87      private void _initCacheKeyPrefix() {
88          StringBundler sb = new StringBundler(_params.length * 2 + 3);
89  
90          sb.append(_methodName);
91          sb.append(_PARAMS_SEPARATOR);
92  
93          for (String param : _params) {
94              sb.append(StringPool.PERIOD);
95              sb.append(param);
96          }
97  
98          sb.append(_ARGS_SEPARATOR);
99  
100         _cacheKeyPrefix = sb.toString();
101     }
102 
103     private void _initLocalCacheKeyPrefix() {
104         _localCacheKeyPrefix = _className.concat(StringPool.PERIOD).concat(
105             _cacheKeyPrefix);
106     }
107 
108     private static final String _ARGS_SEPARATOR = "_A_";
109 
110     private static final String _PARAMS_SEPARATOR = "_P_";
111 
112     private String _cacheKeyPrefix;
113     private String _className;
114     private boolean _entityCacheEnabled;
115     private boolean _finderCacheEnabled;
116     private String _localCacheKeyPrefix;
117     private String _methodName;
118     private String[] _params;
119 
120 }