001    /**
002     * Copyright (c) 2000-2010 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.portal.kernel.memory;
016    
017    import com.liferay.portal.kernel.util.Validator;
018    
019    import java.lang.ref.ReferenceQueue;
020    import java.lang.ref.WeakReference;
021    
022    /**
023     * @author Shuyang Zhou
024     */
025    public class EqualityWeakReference<T> extends WeakReference<T> {
026    
027            public EqualityWeakReference(T referent) {
028                    super(referent);
029    
030                    _hashCode = referent.hashCode();
031            }
032    
033            public EqualityWeakReference(
034                    T referent, ReferenceQueue<? super T> referenceQueue) {
035    
036                    super(referent, referenceQueue);
037    
038                    _hashCode = referent.hashCode();
039            }
040    
041            public boolean equals(Object obj) {
042                    if (this == obj) {
043                            return true;
044                    }
045    
046                    if (!(obj instanceof EqualityWeakReference<?>)) {
047                            return false;
048                    }
049    
050                    EqualityWeakReference<?> equalityWeakReference =
051                            (EqualityWeakReference<?>)obj;
052    
053                    if (Validator.equals(get(), equalityWeakReference.get())) {
054                            return true;
055                    }
056    
057                    return false;
058            }
059    
060            public int hashCode() {
061                    return _hashCode;
062            }
063    
064            private final int _hashCode;
065    
066    }