~ubuntu-branches/ubuntu/karmic/rhino/karmic

« back to all changes in this revision

Viewing changes to src/org/mozilla/javascript/ListenerArray.java

  • Committer: Bazaar Package Importer
  • Author(s): Jerry Haltom
  • Date: 2005-03-19 16:56:07 UTC
  • mto: (11.1.1 squeeze)
  • mto: This revision was merged to the branch mainline in revision 3.
  • Revision ID: james.westby@ubuntu.com-20050319165607-geu3j3fnqlkpqkh1
Tags: upstream-1.6.R1
ImportĀ upstreamĀ versionĀ 1.6.R1

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* -*- Mode: java; tab-width: 4; indent-tabs-mode: 1; c-basic-offset: 4 -*-
2
 
 *
3
 
 * The contents of this file are subject to the Netscape Public
4
 
 * License Version 1.1 (the "License"); you may not use this file
5
 
 * except in compliance with the License. You may obtain a copy of
6
 
 * the License at http://www.mozilla.org/NPL/
7
 
 *
8
 
 * Software distributed under the License is distributed on an "AS
9
 
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express oqr
10
 
 * implied. See the License for the specific language governing
11
 
 * rights and limitations under the License.
12
 
 *
13
 
 * The Original Code is Rhino code, released
14
 
 * May 6, 1999.
15
 
 *
16
 
 * The Initial Developer of the Original Code is Netscape
17
 
 * Communications Corporation.  Portions created by Netscape are
18
 
 * Copyright (C) 1997-1999 Netscape Communications Corporation. All
19
 
 * Rights Reserved.
20
 
 *
21
 
 * Contributor(s):
22
 
 * Igor Bukanov
23
 
 *
24
 
 * Alternatively, the contents of this file may be used under the
25
 
 * terms of the GNU Public License (the "GPL"), in which case the
26
 
 * provisions of the GPL are applicable instead of those above.
27
 
 * If you wish to allow use of your version of this file only
28
 
 * under the terms of the GPL and not to allow others to use your
29
 
 * version of this file under the NPL, indicate your decision by
30
 
 * deleting the provisions above and replace them with the notice
31
 
 * and other provisions required by the GPL.  If you do not delete
32
 
 * the provisions above, a recipient may use your version of this
33
 
 * file under either the NPL or the GPL.
34
 
 */
35
 
 
36
 
/* Helper class to add/remove listeners from Object array */
37
 
 
38
 
package org.mozilla.javascript;
39
 
 
40
 
/**
41
 
 * Utility class to manage listeners array.
42
 
 * A possible usage would be:
43
 
<pre>
44
 
    private Object[] listeners;
45
 
    ...
46
 
    void addListener(ListenerType listener) {
47
 
        synchronized (this) {
48
 
            listeners = ListenerArray.add(listeners, listener);
49
 
        }
50
 
    }
51
 
 
52
 
    void removeListener(ListenerType listener) {
53
 
        synchronized (this) {
54
 
            listeners = ListenerArray.remove(listeners, listener);
55
 
        }
56
 
    }
57
 
</pre>
58
 
  * Here is a thread safe while synchronization free example of event firing
59
 
<pre>
60
 
    void fireEvent(EventType event) {
61
 
        Object[] array = listeners;
62
 
        if (array != null) {
63
 
            for (int i = array.length; i-- != 0;) {
64
 
                ((ListenerType)array[i]).onEvent(event);
65
 
            }
66
 
        }
67
 
 
68
 
    }
69
 
</pre>
70
 
 
71
 
 * or if listeners of different types can present in listeners array:
72
 
<pre>
73
 
    void fireEvent(EventType event) {
74
 
        Object[] array = listeners;
75
 
        if (array != null) {
76
 
            for (int i = array.length; i-- != 0;) {
77
 
                Object obj = array[i];
78
 
                if (obj instanceof ListenerType) {
79
 
                    ((ListenerType)obj).onEvent(event);
80
 
                }
81
 
            }
82
 
        }
83
 
 
84
 
    }
85
 
</pre>
86
 
 */
87
 
public class ListenerArray {
88
 
 
89
 
    /** Return newly allocated array that contains listener and all elements
90
 
     ** from data array.
91
 
     ** Note: listener is added to resulting array even if it is already
92
 
     ** present in data */
93
 
    public static Object[] add(Object[] data, Object listener) {
94
 
        if (data == null) {
95
 
            data = new Object[1];
96
 
        }
97
 
        else {
98
 
            int N = data.length;
99
 
            Object[] tmp = new Object[N + 1];
100
 
            System.arraycopy(data, 0, tmp, 1, N);
101
 
            data = tmp;
102
 
        }
103
 
        data[0] = listener;
104
 
        return data;
105
 
    }
106
 
 
107
 
    /** Return a copy of data array with the first occurrence of listener
108
 
     ** removed.
109
 
     ** If listener is not present in data, simply return data.
110
 
     ** Note: return <code>null</code> if listener is the single element
111
 
     ** of data. */
112
 
    public static Object[] remove(Object[] data, Object listener) {
113
 
        if (data != null) {
114
 
            int N = data.length;
115
 
            for (int i = 0; i != N; ++i) {
116
 
                if (data[i] == listener) {
117
 
                    if (N == 1) { data = null; }
118
 
                    else {
119
 
                        Object[] tmp = new Object[N - 1];
120
 
                        System.arraycopy(data, 0, tmp, 0, i);
121
 
                        System.arraycopy(data, i + 1, tmp, i, N - 1 - i);
122
 
                        data = tmp;
123
 
                    }
124
 
                    break;
125
 
                }
126
 
            }
127
 
        }
128
 
        return data;
129
 
    }
130
 
 
131
 
}