bump product version to 6.4.0.3
[LibreOffice.git] / jurt / com / sun / star / uno / WeakReference.java
bloba6b171ac2e8ba8bd5ad853020bfe17fdc8d238af
1 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 package com.sun.star.uno;
22 /**
23 * This class holds weak reference to an object.
25 * <p>It actually holds a reference to a <code>com.sun.star.XAdapter</code>
26 * implementation and obtains a hard reference if necessary.
28 public class WeakReference
30 private OWeakRefListener m_listener;
31 // There is no default constructor. Every instance must register itself with the
32 // XAdapter interface, which is done in the constructors. Assume we have this code
33 // WeakReference ref= new WeakReference();
34 // ref = someOtherWeakReference;
36 // ref would not be notified (XReference.dispose()) because it did not register
37 // itself. Therefore the XAdapter would be kept alive although this is not
38 // necessary.
40 /**
41 * Creates an instance of this class.
43 * @param obj another instance that is to be copied.
45 public WeakReference(WeakReference obj)
47 if (obj == null) {
48 return;
50 Object weakImpl = obj.get();
51 if (weakImpl == null) {
52 return;
54 XWeak weak = UnoRuntime.queryInterface(XWeak.class, weakImpl);
55 if (weak != null) {
56 XAdapter adapter = weak.queryAdapter();
57 if (adapter != null)
58 m_listener = new OWeakRefListener(adapter);
62 /**
63 * Creates an instance of this class.
65 * @param obj XWeak implementation.
67 public WeakReference(Object obj)
69 XWeak weak= UnoRuntime.queryInterface(XWeak.class, obj);
70 if (weak != null)
72 XAdapter adapter= weak.queryAdapter();
73 if (adapter != null)
74 m_listener= new OWeakRefListener(adapter);
78 /**
79 * Returns a hard reference to the object that is kept weak by this class.
81 * @return a hard reference to the XWeak implementation.
83 public Object get()
85 if (m_listener != null)
86 return m_listener.get();
87 return null;
91 /**
92 * Implementation of com.sun.star.uno.XReference for use with WeakReference.
94 * <p>It keeps the XAdapter implementation and registers always with it.
95 * Deregistering occurs on notification by the adapter and the adapter is
96 * released.</p>
98 class OWeakRefListener implements XReference
100 private XAdapter m_adapter;
103 * The constructor registered this object with adapter.
105 * @param adapter the XAdapter implementation.
107 OWeakRefListener( XAdapter adapter)
109 m_adapter= adapter;
110 m_adapter.addReference(this);
114 * Method of <code>com.sun.star.uno.XReference</code>.
116 * <p>When called, it deregisters this object with the adapter and releases
117 * the reference to it.</p>
119 synchronized public void dispose()
121 if (m_adapter != null)
123 m_adapter.removeReference(this);
124 m_adapter= null;
129 * Obtains a hard reference to the object which is kept weak by the adapter
130 * and returns it.
132 * @return hard reference to the otherwise weakly kept object.
134 synchronized Object get()
136 Object retVal= null;
137 if (m_adapter != null)
139 retVal= m_adapter.queryAdapted();
140 if (retVal == null)
142 // If this object registered as listener with XAdapter while it was notifying
143 // the listeners then this object might not have been notified. If queryAdapted
144 // returned null then the weak kept object is dead and the listeners have already
145 // been notified. And we missed it.
146 m_adapter.removeReference(this);
147 m_adapter= null;
150 return retVal;
154 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */