1 package org.csc.phynixx.watchdog.objectref;
2
3 /*
4 * #%L
5 * phynixx-watchdog
6 * %%
7 * Copyright (C) 2014 csc
8 * %%
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
12 *
13 * http://www.apache.org/licenses/LICENSE-2.0
14 *
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * #L%
21 */
22
23
24 import java.lang.ref.WeakReference;
25
26
27 /**
28 * the watchdog references the conditions weakly. If any condition is not referenced
29 * by anybody but the watchdog it shut be handed to gc.
30 *
31 * @author Christoph Schmidt-Casdorff
32 */
33
34 public class WeakObjectReference<T> extends WeakReference<T> implements IObjectReference<T> {
35
36 private String description = null;
37
38 public WeakObjectReference(T objectRef) {
39 super(objectRef);
40 this.description = (objectRef == null) ? "NULL" : objectRef.toString();
41 }
42
43
44 public String getObjectDescription() {
45 return this.description;
46 }
47
48
49 public boolean isWeakReference() {
50 return true;
51 }
52
53
54 public boolean isStale() {
55 return this.get() == null;
56 }
57
58 public boolean equals(Object obj) {
59 Object objRef = this.get();
60 if (objRef == null) {
61 return obj == null;
62 }
63 return objRef.equals(obj);
64 }
65
66 public int hashCode() {
67 Object objRef = this.get();
68 if (objRef == null) {
69 return "".hashCode();
70 }
71 return objRef.hashCode();
72 }
73
74 public String toString() {
75 Object objRef = this.get();
76 if (objRef == null) {
77 return "";
78 }
79 return objRef.toString();
80 }
81
82 }