2 * This file is part of the LibreOffice project.
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 * This file incorporates work covered by the following license notice:
10 * Licensed to the Apache Software Foundation (ASF) under one or more
11 * contributor license agreements. See the NOTICE file distributed
12 * with this work for additional information regarding copyright
13 * ownership. The ASF licenses this file to you under the Apache
14 * License, Version 2.0 (the "License"); you may not use this file
15 * except in compliance with the License. You may obtain a copy of
16 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
19 package com
.sun
.star
.lib
.uno
.helper
;
20 import java
.lang
.ref
.WeakReference
;
21 import com
.sun
.star
.uno
.XAdapter
;
22 import com
.sun
.star
.uno
.XReference
;
23 import java
.util
.List
;
24 import java
.util
.Collections
;
25 import java
.util
.LinkedList
;
27 /** An XAdapter implementation that holds a weak reference (java.lang.ref.WeakReference)
28 * to an object. Clients can register listener (com.sun.star.lang.XReference) which
29 * are notified when the object (the one which is kept weak) is being finalized. That
30 * is, that object is being destroyed because there are not any hard references
33 public class WeakAdapter
implements XAdapter
35 // references the XWeak implementation
36 private final WeakReference
<Object
> m_weakRef
;
37 // contains XReference objects registered by addReference
38 private final List
<XReference
> m_xreferenceList
;
41 *@param component the object that is to be held weak
43 public WeakAdapter(Object component
)
45 m_weakRef
= new WeakReference
<Object
>(component
);
46 m_xreferenceList
= Collections
.synchronizedList( new LinkedList
<XReference
>());
49 /** Called by the XWeak implementation (WeakBase) when it is being finalized.
50 * It is only being called once.
51 * The registered XReference listeners are notified. On notification they are
52 * to unregister themselves. The notification is thread-safe. However, it is possible
53 * to add a listener during the notification process, which will never receive a
54 * notification. To prevent this, one would have to synchronize this method with
55 * the addReference method. But this can result in deadlocks in a multi-threaded
61 XReference
[] references
= m_xreferenceList
.toArray(new XReference
[m_xreferenceList
.size()]);
62 for (int i
= references
.length
; i
> 0; i
--)
64 references
[i
-1].dispose();
68 /** Method of com.sun.star.uno.XAdapter. It is called to obtain a hard reference
69 * to the object which is kept weak by this instance.
70 * @return hard reference to the object
72 public Object
queryAdapted()
74 return m_weakRef
.get();
77 /** Method of com.sun.star.uno.XAdapter. Called by clients to register listener which
78 * are notified when the weak object is dying.
79 *@param xReference a listener
81 public void removeReference(XReference xReference
)
83 m_xreferenceList
.remove(xReference
);
86 /** Method of com.sun.star.uno.XAdapter. Called by clients to unregister listeners.
87 *@param xReference listener
89 public void addReference(XReference xReference
)
91 m_xreferenceList
.add(xReference
);