1 48 49 50 package com.caucho.portal.generic; 51 52 import javax.portlet.PortletMode; 53 import javax.portlet.WindowState; 54 import java.util.Locale ; 55 56 57 72 public class CacheKey { 73 private String _namespace; 74 private int _namespaceValue; 75 76 private PortletMode _portletMode; 77 private int _portletModeValue; 78 79 private WindowState _windowState; 80 private int _windowStateValue; 81 82 private long _value1, _value2; 84 private String _contentType; 85 private int _contentTypeValue; 86 private Locale _locale; 87 private int _localeValue; 88 private boolean _isPrivate = true; 89 private String _sessionId; 90 91 private String _stringValue; 92 93 public void setNamespace(String namespace) 94 { 95 _stringValue = null; 96 _namespace = namespace; 97 _namespaceValue = namespace.hashCode(); 98 } 99 100 public void setPortletMode(PortletMode portletMode) 101 { 102 _stringValue = null; 103 _portletMode = portletMode; 104 _portletModeValue = portletMode.hashCode(); 105 } 106 107 public void setWindowState(WindowState windowState) 108 { 109 _stringValue = null; 110 _windowState = windowState; 111 _windowStateValue = windowState.hashCode(); 112 } 113 114 private void add(String name, String value) 115 { 116 _stringValue = null; 117 int v = (name == null ? 65353 : name.hashCode()); 118 119 _value1 += v; 120 _value2 += v + (value == null ? 65353 : value.hashCode()); 121 } 122 123 public void addParameter(String name, String [] values) 124 { 125 _stringValue = null; 126 if (values == null || values.length == 0) 127 add(name, null); 128 else { 129 int i = values.length; 130 131 while (i-- > 0) 132 add(name, values[i]); 133 } 134 } 135 136 public void setContentType(String contentType) 137 { 138 _stringValue = null; 139 _contentType = contentType; 140 _contentTypeValue = contentType.hashCode(); 141 } 142 143 155 public void setLocale(Locale locale) 156 { 157 _stringValue = null; 158 _locale = locale; 159 _localeValue = locale.hashCode(); 160 } 161 162 public void setPrivate(boolean isPrivate) 163 { 164 _isPrivate = isPrivate; 165 } 166 167 public void setRequestedSessionId(String sessionId) 168 { 169 _stringValue = null; 170 _sessionId = sessionId; 171 } 172 173 public boolean isPrivate() 174 { 175 return _isPrivate; 176 } 177 178 public String getNamespace() 179 { 180 return _namespace; 181 } 182 183 public PortletMode getPortletMode() 184 { 185 return _portletMode; 186 } 187 188 public WindowState getWindowState() 189 { 190 return _windowState; 191 } 192 193 public String getContentType() 194 { 195 return _contentType; 196 } 197 198 public Locale getLocale() 199 { 200 return _locale; 201 } 202 203 206 public String getRequestedSessionId() 207 { 208 return _isPrivate ? _sessionId : null; 209 } 210 211 214 public void reset() 215 { 216 _stringValue = null; 217 218 _namespace = null; 219 _namespaceValue = 0; 220 _portletMode = null; 221 _portletModeValue = 0; 222 _windowState = null; 223 _windowStateValue = 0; 224 _value1 = 0; 225 _value2 = 0; 226 227 _localeValue = 0; 228 _locale = null; 229 _contentTypeValue = 0; 230 _contentType = null; 231 232 _isPrivate = true; 233 _sessionId = null; 234 } 235 236 public int hashCode() 237 { 238 int isPrivateValue = _isPrivate ? 31 : 33331; 239 240 int sessionIdValue = _isPrivate && _sessionId != null 241 ? _sessionId.hashCode() 242 : 7331; 243 return (int) 244 ((long) _namespaceValue 245 * _portletModeValue 246 * _windowStateValue 247 * _localeValue 248 * _contentTypeValue 249 * isPrivateValue 250 * sessionIdValue 251 * _value1 252 * _value2); 253 } 254 255 public boolean equals(Object o) 256 { 257 if (o == null || !(o instanceof CacheKey)) 258 return false; 259 260 CacheKey other = (CacheKey) o; 261 262 return 263 (_namespaceValue == other._namespaceValue) 264 && (_portletModeValue == other._portletModeValue) 265 && (_windowStateValue == other._windowStateValue) 266 && (_value1 == other._value1) 267 && (_value2 == other._value2) 268 && (_localeValue == other._localeValue) 269 && (_contentTypeValue == other._contentTypeValue) 270 && (_isPrivate == other._isPrivate) 271 && (_isPrivate ? _sessionId != null : true) 272 && (_isPrivate ? _sessionId.equals(other._sessionId) : true); 273 } 274 275 static private void encode(long number, StringBuffer buf) 276 { 277 int code; 278 char ch = '-'; 279 280 do { 281 code = ((int) number) & 0x3f; 283 if (code < 26) 284 ch = (char) ('a' + code); 285 else if (code < 52) 286 ch = (char) ('A' + code - 26); 287 else if (code < 62) 288 ch = (char) ('0' + code - 52); 289 else if (code == 62 || code == 63) 290 ch = '_'; 291 else 292 ch = 'z'; 293 294 number = number >> 6; 295 296 } while (number != 0); 297 } 298 299 304 public String toString() 305 { 306 if (_stringValue != null) 307 return _stringValue; 308 309 StringBuffer buf = new StringBuffer (); 310 311 encode(_value1, buf); 312 encode(_value2, buf); 313 314 int valuesIndex = buf.length(); 315 316 encode(_namespaceValue, buf); 317 encode(_portletModeValue, buf); 318 encode(_windowStateValue, buf); 319 encode(_localeValue, buf); 320 encode(_contentTypeValue, buf); 321 322 if (_isPrivate) 323 buf.append(getRequestedSessionId()); 324 325 328 int len = buf.length(); 329 int half = len / 2; 330 331 int i = valuesIndex; 332 int j = len - 1; 333 334 while (i < half) { 335 char ch = buf.charAt(i); 336 buf.setCharAt(i, buf.charAt(j)); 337 buf.setCharAt(j, ch); 338 i += 3; 339 j -= 3; 340 } 341 342 i = valuesIndex; 343 j = half > i ? half : len; 344 345 while (j < len) { 346 char ch = buf.charAt(i); 347 buf.setCharAt(i, buf.charAt(j)); 348 buf.setCharAt(j, ch); 349 i += 5; 350 j += 5; 351 } 352 353 355 _stringValue = buf.toString(); 356 357 return _stringValue; 358 } 359 } 361 | Popular Tags |