merge the formfield patch from ooo-build
[ooovba.git] / jurt / com / sun / star / uno / WeakReference.java
blobf8be0cd0536f120b0ebe20c3bc142285f255adb0
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: WeakReference.java,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 package com.sun.star.uno;
33 import com.sun.star.uno.XWeak;
34 import com.sun.star.uno.UnoRuntime;
35 import com.sun.star.uno.XAdapter;
36 import com.sun.star.uno.XReference;
38 /** This class holds weak reference to an object. It actually holds a reference to a
39 com.sun.star.XAdapter implementation and obtains a hard reference if necessary.
41 public class WeakReference
43 private final boolean DEBUG= false;
44 private OWeakRefListener m_listener;
45 // There is no default constructor. Every instance must register itself with the
46 // XAdapter interface, which is done in the constructors. Assume we have this code
47 // WeakReference ref= new WeakReference();
48 // ref = someOtherWeakReference;
50 // ref would not be notified (XReference.dispose()) because it did not register
51 // itself. Therefore the XAdapter would be kept aliver although this is not
52 // necessary.
54 /** Creates an instance of this class.
55 *@param obj - another instance that is to be copied
57 public WeakReference(WeakReference obj)
59 if (obj != null)
61 Object weakImpl= obj.get();
62 if (weakImpl != null)
64 XWeak weak= UnoRuntime.queryInterface(XWeak.class, weakImpl);
65 if (weak != null)
67 XAdapter adapter= (XAdapter) weak.queryAdapter();
68 if (adapter != null)
69 m_listener= new OWeakRefListener(adapter);
75 /** Creates an instance of this class.
76 *@param obj XWeak implementation
78 public WeakReference(Object obj)
80 XWeak weak= UnoRuntime.queryInterface(XWeak.class, obj);
81 if (weak != null)
83 XAdapter adapter= (XAdapter) weak.queryAdapter();
84 if (adapter != null)
85 m_listener= new OWeakRefListener(adapter);
88 /** Returns a hard reference to the object that is kept weak by this class.
89 *@return a hard reference to the XWeak implementation.
91 public Object get()
93 if (m_listener != null)
94 return m_listener.get();
95 return null;
99 /** Implementation of com.sun.star.uno.XReference for use with WeakReference.
100 * It keeps the XAdapter implementation and registers always with it. Deregistering
101 * occurs on notification by the adapter and the adapter is released.
103 class OWeakRefListener implements XReference
105 private final boolean DEBUG= false;
106 private XAdapter m_adapter;
108 /** The constructor registered this object with adapter.
109 *@param adapter the XAdapter implementation.
111 OWeakRefListener( XAdapter adapter)
113 m_adapter= adapter;
114 m_adapter.addReference(this);
116 /** Method of com.sun.star.uno.XReference. When called, it deregisteres this
117 * object with the adapter and releases the reference to it.
119 synchronized public void dispose()
121 if (m_adapter != null)
123 m_adapter.removeReference(this);
124 m_adapter= null;
128 /** Obtains a hard reference to the object which is kept weak by the adapter
129 * and returns it.
130 * @return hard reference to the otherwise weakly kept object.
132 synchronized Object get()
134 Object retVal= null;
135 if (m_adapter != null)
137 retVal= m_adapter.queryAdapted();
138 if (retVal == null)
140 // If this object registered as listener with XAdapter while it was notifying
141 // the listeners then this object might not have been notified. If queryAdapted
142 // returned null then the weak kept object is dead and the listeners have already
143 // been notified. And we missed it.
144 m_adapter.removeReference(this);
145 m_adapter= null;
148 return retVal;