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.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            @Override
042            public boolean equals(Object obj) {
043                    if (this == obj) {
044                            return true;
045                    }
046    
047                    if (!(obj instanceof EqualityWeakReference<?>)) {
048                            return false;
049                    }
050    
051                    EqualityWeakReference<?> equalityWeakReference =
052                            (EqualityWeakReference<?>)obj;
053    
054                    if (Validator.equals(get(), equalityWeakReference.get())) {
055                            return true;
056                    }
057    
058                    return false;
059            }
060    
061            @Override
062            public int hashCode() {
063                    return _hashCode;
064            }
065    
066            private final int _hashCode;
067    
068    }