1/*
2 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25
26package sun.applet;
27
28import java.util.EventListener;
29import java.io.Serializable;
30import java.io.ObjectOutputStream;
31import java.io.IOException;
32
33/**
34 * AppletEventMulticaster class.  This class manages an immutable
35 * structure consisting of a chain of AppletListeners and is
36 * responsible for dispatching events to them.
37 *
38 * @author  Sunita Mani
39 *
40 * @deprecated The Applet API is deprecated. See the
41 * <a href="../../java/applet/package-summary.html"> java.applet package
42 * documentation</a> for further information.
43 */
44@Deprecated(since = "9")
45public class AppletEventMulticaster implements AppletListener {
46
47    private final AppletListener a, b;
48
49    public AppletEventMulticaster(AppletListener a, AppletListener b) {
50        this.a = a; this.b = b;
51    }
52
53    public void appletStateChanged(AppletEvent e) {
54        a.appletStateChanged(e);
55        b.appletStateChanged(e);
56    }
57
58    /**
59     * Adds Applet-listener-a with Applet-listener-b and
60     * returns the resulting multicast listener.
61     * @param a Applet-listener-a
62     * @param b Applet-listener-b
63     */
64    public static AppletListener add(AppletListener a, AppletListener b) {
65        return addInternal(a, b);
66    }
67
68    /**
69     * Removes the old Applet-listener from Applet-listener-l and
70     * returns the resulting multicast listener.
71     * @param l Applet-listener-l
72     * @param oldl the Applet-listener being removed
73     */
74    public static AppletListener remove(AppletListener l, AppletListener oldl) {
75        return removeInternal(l, oldl);
76    }
77
78    /**
79     * Returns the resulting multicast listener from adding listener-a
80     * and listener-b together.
81     * If listener-a is null, it returns listener-b;
82     * If listener-b is null, it returns listener-a
83     * If neither are null, then it creates and returns
84     * a new AppletEventMulticaster instance which chains a with b.
85     * @param a event listener-a
86     * @param b event listener-b
87     */
88    private static AppletListener addInternal(AppletListener a, AppletListener b) {
89        if (a == null)  return b;
90        if (b == null)  return a;
91        return new AppletEventMulticaster(a, b);
92    }
93
94
95    /**
96     * Removes a listener from this multicaster and returns the
97     * resulting multicast listener.
98     * @param oldl the listener to be removed
99     */
100    protected AppletListener remove(AppletListener oldl) {
101        if (oldl == a)  return b;
102        if (oldl == b)  return a;
103        AppletListener a2 = removeInternal(a, oldl);
104        AppletListener b2 = removeInternal(b, oldl);
105        if (a2 == a && b2 == b) {
106            return this;        // it's not here
107        }
108        return addInternal(a2, b2);
109    }
110
111
112    /**
113     * Returns the resulting multicast listener after removing the
114     * old listener from listener-l.
115     * If listener-l equals the old listener OR listener-l is null,
116     * returns null.
117     * Else if listener-l is an instance of AppletEventMulticaster
118     * then it removes the old listener from it.
119     * Else, returns listener l.
120     * @param l the listener being removed from
121     * @param oldl the listener being removed
122     */
123    private static AppletListener removeInternal(AppletListener l, AppletListener oldl) {
124        if (l == oldl || l == null) {
125            return null;
126        } else if (l instanceof AppletEventMulticaster) {
127            return ((AppletEventMulticaster)l).remove(oldl);
128        } else {
129            return l;           // it's not here
130        }
131    }
132}
133