View Javadoc
1   /*
2    * #%L
3    * Nuiton Utils
4    * %%
5    * Copyright (C) 2004 - 2010 CodeLutin
6    * %%
7    * This program is free software: you can redistribute it and/or modify
8    * it under the terms of the GNU Lesser General Public License as 
9    * published by the Free Software Foundation, either version 3 of the 
10   * License, or (at your option) any later version.
11   * 
12   * This program is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Lesser Public License for more details.
16   * 
17   * You should have received a copy of the GNU General Lesser Public 
18   * License along with this program.  If not, see
19   * <http://www.gnu.org/licenses/lgpl-3.0.html>.
20   * #L%
21   */
22  
23  /* *
24  * TransparenteSoftReference.java
25  *
26  * Created: 10 mai 2004
27  *
28  * @author Benjamin Poussin - poussin@codelutin.com
29  * Copyright Code Lutin
30  *
31  *
32  * Mise a jour: $Date$
33  * par : $Author$
34  */
35  package org.nuiton.util;
36  
37  import java.lang.ref.Reference;
38  import java.lang.ref.ReferenceQueue;
39  import java.lang.ref.SoftReference;
40  
41  /**
42   * Cette classe etant SoftReference et surcharge les méthodes equals et
43   * hashCode pour que ces méthodes retournes les mêmes résultat que les objets
44   * contenu.
45   */
46  public class TransparenteSoftReference<T> extends SoftReference<T> {
47  
48      protected int hash;
49  
50      protected String toString;
51  
52      /**
53       * DOCUMENTME Constructor for the TransparenteSoftReference object
54       *
55       * @param o DOCUMENTME Description of the Parameter
56       */
57      public TransparenteSoftReference(T o) {
58          this(o, true);
59      }
60  
61      public TransparenteSoftReference(T o, ReferenceQueue<? super T> queue) {
62          this(o, queue, true);
63      }
64  
65      public TransparenteSoftReference(T o, boolean objectToStringUsed) {
66          super(o);
67          init(o, objectToStringUsed);
68      }
69  
70      public TransparenteSoftReference(T o,
71                                       ReferenceQueue<? super T> queue,
72                                       boolean objectToStringUsed) {
73          super(o, queue);
74          init(o, objectToStringUsed);
75      }
76  
77      /**
78       * On conserve le hash pour que la Reference puisse encore se faire
79       * passer pour l'objet alors que celui-ci a disparu de la memoire
80       *
81       * @param o                  TODO
82       * @param objectToStringUsed TODO
83       */
84      protected void init(T o, boolean objectToStringUsed) {
85          if (o == null) {
86              hash = 0;
87          } else {
88              hash = o.hashCode();
89              if (objectToStringUsed) {
90                  toString = o.toString();
91              }
92              if (toString == null) {
93                  toString = o.getClass().getName() + '@' +
94                             Integer.toHexString(hash);
95              }
96              if (toString.length() > 100) {
97                  toString = toString.substring(0, 100) + "...";
98              }
99          }
100     }
101 
102     /**
103      * DOCUMENTME Method
104      *
105      * @param o DOCUMENTME Description of the Parameter
106      * @return DOCUMENTME Description of the Return Value
107      */
108     public boolean equals(Object o) {
109         if (o == this) {
110             return true;
111         }
112         // on travail avec un variable local pour ne pas etre obligé de
113         // synchroniser la méthode
114         Object local = get();
115         if (o instanceof Reference) {
116             o = ((Reference<?>) o).get();
117         }
118 
119         boolean result =
120                 o == null && local == null
121                 || o != null && o.equals(local);
122 
123         return result;
124     }
125 
126     /**
127      * DOCUMENTME Method
128      *
129      * @return DOCUMENTME Description of the Return Value
130      */
131     public int hashCode() {
132         return hash;
133     }
134 
135     public String toString() {
136         return toString;
137     }
138 
139 }