Skip to main content
summaryrefslogtreecommitdiffstats
blob: 4c654e2bf738f7e051ce8a8f9a9273c7453fdd5a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package org.eclipse.jst.jsf.common.internal.locator;

import java.util.concurrent.CopyOnWriteArrayList;

import org.eclipse.jst.jsf.common.internal.locator.ILocatorChangeListener.LocatorChangeEvent;

/**
 * The abstract base class of all ILocator implementations.
 * 
 * @author cbateman
 *
 * @param <LOCATORTYPE>
 * @param <CONTEXTTYPE>
 * @param <IDTYPE>
 */
public abstract class AbstractLocator<LOCATORTYPE, CONTEXTTYPE, IDTYPE>
        implements ILocator<LOCATORTYPE, CONTEXTTYPE, IDTYPE>
{
    /**
     * The default value used for "no result".
     */
    protected static final Object   DEFAULT_NO_RESULT_VALUE = null;
    
    private final CopyOnWriteArrayList<ILocatorChangeListener> _listeners;
    private final LOCATORTYPE _noResultValue;
    private final IDTYPE _id;
    private final String _displayName;
    private boolean _isStarted;

    /**
     * Available for sub-classes that want to use reasonable defaults and only provide
     * mandatory data.
     * 
     * No result value is null.
     * A new instance of CopyOnWriteArrayList is used and held private.
     * 
     * @param id 
     * @param displayName 
     * 
     */
    public AbstractLocator(final IDTYPE id, final String displayName)
    {
        this(id,
             displayName,
             null,
             new CopyOnWriteArrayList<ILocatorChangeListener>());
    }

    /**
     * Available for sub-classes to manually inject dependencies.
     * 
     * @param id 
     * @param displayName 
     * @param noResultValue 
     * @param mutableListenerList
     */
    protected AbstractLocator(
            final IDTYPE id,
            final String displayName,
            final LOCATORTYPE noResultValue,
            final CopyOnWriteArrayList<ILocatorChangeListener> mutableListenerList)
    {
        _id = id;
        _displayName = displayName;
        _listeners = mutableListenerList;
        _noResultValue = noResultValue;
    }

    public final LOCATORTYPE perform(final CONTEXTTYPE context)
            throws Exception
    {
        return locate(context);
    }

    public LOCATORTYPE getNoResult()
    {
        return _noResultValue;
    }

    public IDTYPE getId()
    {
        return _id;
    }

    public String getDisplayName()
    {
        return _displayName;
    }

    public LOCATORTYPE locate(final CONTEXTTYPE context)
    {
        if (isStarted())
        {
            return doLocate(context);
        }
        throw new IllegalStateException("Locator not started"); //$NON-NLS-1$
    }
    

    /**
     * @param context
     * @return the located type.
     */
    protected abstract LOCATORTYPE doLocate(CONTEXTTYPE context);

    public void start(final CONTEXTTYPE initialContext)
    {
        // set the started flag
        setStarted(true);
    }

    public void stop()
    {
        // set the started flag
        setStarted(false);
    }

    /**
     * @param listener
     */
    public  void addListener(final ILocatorChangeListener listener)
    {
        _listeners.addIfAbsent(listener);
    }

    /**
     * @param listener
     */
    public void removeListener(final ILocatorChangeListener listener)
    {
        _listeners.remove(listener);
    }
    
    /**
     * @param event
     */
    protected void fireChangeEvent(final LocatorChangeEvent event)
    {
        for (final ILocatorChangeListener listener : _listeners)
        {
            listener.changed(event);
        }
    }

    public final boolean isStarted()
    {
        return _isStarted;
    }

    public boolean canStart()
    {
        if (isStarted())
        {
            return false;
        }
        return true;
    }

    /**
     * @param newValue
     */
    protected final void setStarted(final boolean newValue)
    {
        _isStarted = newValue;
    }
}

Back to the top