001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.portal.cache.key;
016    
017    import com.liferay.portal.kernel.cache.key.CacheKeyGenerator;
018    import com.liferay.portal.kernel.util.StringBundler;
019    
020    /**
021     * @author Michael C. Han
022     * @author Shuyang Zhou
023     */
024    public class HashCodeCacheKeyGenerator extends BaseCacheKeyGenerator {
025    
026            @Override
027            public CacheKeyGenerator clone() {
028                    return new HashCodeCacheKeyGenerator();
029            }
030    
031            @Override
032            public Long getCacheKey(String key) {
033                    long hashCode = 0;
034    
035                    for (int i = 0; i < key.length(); i++) {
036                            hashCode = 31 * hashCode + key.charAt(i);
037                    }
038    
039                    return hashCode;
040            }
041    
042            @Override
043            public Long getCacheKey(String[] keys) {
044                    long hashCode = 0;
045    
046                    for (String key : keys) {
047                            if (key == null) {
048                                    continue;
049                            }
050    
051                            for (int i = 0; i < key.length(); i++) {
052                                    hashCode = 31 * hashCode + key.charAt(i);
053                            }
054                    }
055    
056                    return hashCode;
057            }
058    
059            @Override
060            public Long getCacheKey(StringBundler sb) {
061                    long hashCode = 0;
062    
063                    for (int i = 0; i < sb.index(); i++) {
064                            String key = sb.stringAt(i);
065    
066                            for (int j = 0; j < key.length(); j++) {
067                                    hashCode = 31 * hashCode + key.charAt(j);
068                            }
069                    }
070    
071                    return hashCode;
072            }
073    
074    }