001/**
002 *   GRANITE DATA SERVICES
003 *   Copyright (C) 2006-2013 GRANITE DATA SERVICES S.A.S.
004 *
005 *   This file is part of the Granite Data Services Platform.
006 *
007 *   Granite Data Services is free software; you can redistribute it and/or
008 *   modify it under the terms of the GNU Lesser General Public
009 *   License as published by the Free Software Foundation; either
010 *   version 2.1 of the License, or (at your option) any later version.
011 *
012 *   Granite Data Services is distributed in the hope that it will be useful,
013 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
014 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser
015 *   General Public License for more details.
016 *
017 *   You should have received a copy of the GNU Lesser General Public
018 *   License along with this library; if not, write to the Free Software
019 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
020 *   USA, or see <http://www.gnu.org/licenses/>.
021 */
022package org.granite.clustering;
023
024import java.io.Serializable;
025
026/**
027 * This class holds a <tt>transient</tt> reference to the object given to its
028 * constructor. When this class is serialized, the reference to the object is
029 * lost.
030 * 
031 * @author Franck WOLFF
032 * 
033 * @see org.granite.messaging.webapp.ServletGraniteContext
034 * @see TransientReference
035 */
036public final class TransientReferenceHolder implements Serializable {
037
038        private static final long serialVersionUID = 1L;
039        
040        private final transient Object object;
041        
042        public TransientReferenceHolder(Object object) {
043                this.object = object;
044        }
045
046        public Object get() {
047                return object;
048        }
049
050        @Override
051        public boolean equals(Object obj) {
052                if (this == obj)
053                        return true;
054                if (!(obj instanceof TransientReferenceHolder))
055                        return false;
056                Object reference = ((TransientReferenceHolder)obj).get();
057                if (reference == object)
058                        return true;
059                if (object == null)
060                        return false; 
061                return object.equals(reference);
062        }
063
064        @Override
065        public int hashCode() {
066                if (object != null)
067                        return object.hashCode();
068                return 0;
069        }
070
071        @Override
072        public String toString() {
073                return getClass().getName() + ": " + object;
074        }
075}