001    /**
002     * Copyright (c) 2000-present 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.kernel.concurrent;
016    
017    import com.liferay.portal.kernel.memory.FinalizeAction;
018    import com.liferay.portal.kernel.memory.FinalizeManager;
019    import com.liferay.portal.kernel.memory.FinalizeManager.ReferenceFactory;
020    
021    import java.lang.ref.Reference;
022    
023    import java.util.Map;
024    import java.util.concurrent.ConcurrentHashMap;
025    import java.util.concurrent.ConcurrentMap;
026    
027    /**
028     * @author Shuyang Zhou
029     */
030    public class ConcurrentReferenceKeyHashMap<K, V>
031            extends ConcurrentMapperHashMap<K, Reference<K>, V, V> {
032    
033            public ConcurrentReferenceKeyHashMap(
034                    ConcurrentMap<Reference<K>, V> innerConcurrentMap,
035                    ReferenceFactory referenceFactory) {
036    
037                    super(innerConcurrentMap);
038    
039                    _referenceFactory = referenceFactory;
040            }
041    
042            public ConcurrentReferenceKeyHashMap(
043                    int initialCapacity, float loadFactor, int concurrencyLevel,
044                    ReferenceFactory referenceFactory) {
045    
046                    this(
047                            new ConcurrentHashMap<Reference<K>, V>(
048                                    initialCapacity, loadFactor, concurrencyLevel),
049                            referenceFactory);
050            }
051    
052            public ConcurrentReferenceKeyHashMap(
053                    int initialCapacity, ReferenceFactory referenceFactory) {
054    
055                    this(
056                            new ConcurrentHashMap<Reference<K>, V>(initialCapacity),
057                            referenceFactory);
058            }
059    
060            public ConcurrentReferenceKeyHashMap(
061                    Map<? extends K, ? extends V> map, ReferenceFactory referenceFactory) {
062    
063                    this(new ConcurrentHashMap<Reference<K>, V>(), referenceFactory);
064    
065                    putAll(map);
066            }
067    
068            public ConcurrentReferenceKeyHashMap(ReferenceFactory referenceFactory) {
069                    this(new ConcurrentHashMap<Reference<K>, V>(), referenceFactory);
070            }
071    
072            @Override
073            protected Reference<K> mapKey(K key) {
074                    return FinalizeManager.register(
075                            key, _keyFinalizeAction, _referenceFactory);
076            }
077    
078            @Override
079            protected Reference<K> mapKeyForQuery(K key) {
080                    return _referenceFactory.createReference(key, null);
081            }
082    
083            @Override
084            protected V mapValue(K key, V value) {
085                    return value;
086            }
087    
088            @Override
089            protected V mapValueForQuery(V value) {
090                    return value;
091            }
092    
093            @Override
094            protected K unmapKey(Reference<K> reference) {
095                    K key = reference.get();
096    
097                    reference.clear();
098    
099                    return key;
100            }
101    
102            @Override
103            protected K unmapKeyForQuery(Reference<K> reference) {
104                    return reference.get();
105            }
106    
107            @Override
108            protected V unmapValue(V value) {
109                    return value;
110            }
111    
112            @Override
113            protected V unmapValueForQuery(V value) {
114                    return value;
115            }
116    
117            private final FinalizeAction _keyFinalizeAction = new FinalizeAction() {
118    
119                    @Override
120                    public void doFinalize(Reference<?> reference) {
121                            innerConcurrentMap.remove(reference);
122                    }
123    
124            };
125    
126            private final ReferenceFactory _referenceFactory;
127    
128    }