Lines 15-22
Link Here
|
15 |
import java.util.ArrayList; |
15 |
import java.util.ArrayList; |
16 |
import java.util.Arrays; |
16 |
import java.util.Arrays; |
17 |
import java.util.Collections; |
17 |
import java.util.Collections; |
18 |
import java.util.Iterator; |
|
|
19 |
import java.util.List; |
18 |
import java.util.List; |
|
|
19 |
import java.util.concurrent.CopyOnWriteArrayList; |
20 |
|
20 |
|
21 |
import org.eclipse.core.runtime.IAdaptable; |
21 |
import org.eclipse.core.runtime.IAdaptable; |
22 |
|
22 |
|
Lines 26-110
Link Here
|
26 |
* Clients may extend this class. |
26 |
* Clients may extend this class. |
27 |
* |
27 |
* |
28 |
* @author cbateman |
28 |
* @author cbateman |
29 |
* |
29 |
* |
30 |
*/ |
30 |
*/ |
31 |
public abstract class AbstractDelegatingFactory implements IDelegatingFactory { |
31 |
public abstract class AbstractDelegatingFactory implements IDelegatingFactory |
|
|
32 |
{ |
33 |
|
34 |
/* instance attributes */ |
32 |
|
35 |
|
33 |
/* instance attributes */ |
36 |
/** |
34 |
|
37 |
* the list of registered factory delegates |
35 |
/** |
38 |
*/ |
36 |
* the list of registered factory delegates |
39 |
protected final CopyOnWriteArrayList<IAdaptable> _delegates; |
37 |
*/ |
40 |
|
38 |
protected final List<IAdaptable> _delegates; |
41 |
private final List<Class> _supportedDelegates; |
39 |
|
42 |
|
40 |
private final List<Class> _supportedDelegates; |
43 |
/** |
41 |
|
44 |
* @param supportedDelegateTypes |
42 |
/** |
45 |
* -- populates the list of classes used by the isValidDelegate |
43 |
* @param supportedDelegateTypes -- populates the list of classes used |
46 |
* contract |
44 |
* by the isValidDelegate contract |
47 |
*/ |
45 |
*/ |
48 |
protected AbstractDelegatingFactory(final Class[] supportedDelegateTypes) |
46 |
protected AbstractDelegatingFactory(Class[] supportedDelegateTypes) |
49 |
{ |
47 |
{ |
50 |
_delegates = new CopyOnWriteArrayList<IAdaptable>(); |
48 |
_delegates = new ArrayList<IAdaptable>(); |
51 |
|
49 |
|
52 |
final List<Class> supportedTypes = new ArrayList<Class>(); |
50 |
final List<Class> supportedTypes = new ArrayList<Class>(); |
53 |
supportedTypes.addAll(Arrays.asList(supportedDelegateTypes)); |
51 |
supportedTypes.addAll(Arrays.asList(supportedDelegateTypes)); |
54 |
_supportedDelegates = Collections.unmodifiableList(supportedTypes); |
52 |
_supportedDelegates = Collections.unmodifiableList(supportedTypes); |
55 |
} |
53 |
} |
56 |
|
54 |
|
57 |
/** |
55 |
/** |
58 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#addFactoryDelegate(org.eclipse.core.runtime.IAdaptable) |
56 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#addFactoryDelegate(org.eclipse.core.runtime.IAdaptable) |
59 |
*/ |
57 |
*/ |
60 |
public final void addFactoryDelegate(final IAdaptable delegate) |
58 |
public final void addFactoryDelegate(IAdaptable delegate) |
61 |
{ |
59 |
{ |
62 |
if (isValidDelegate(delegate)) |
60 |
synchronized(_delegates) |
63 |
{ |
61 |
{ |
64 |
_delegates.addIfAbsent(delegate); |
62 |
|
65 |
} |
63 |
if (!_delegates.contains(delegate) |
66 |
} |
64 |
&& isValidDelegate(delegate)) |
67 |
|
65 |
{ |
68 |
/** |
66 |
_delegates.add(delegate); |
69 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#removeFactoryDelegate(org.eclipse.core.runtime.IAdaptable) |
67 |
} |
70 |
*/ |
68 |
} |
71 |
public final boolean removeFactoryDelegate(final IAdaptable delegate) |
69 |
} |
72 |
{ |
70 |
|
73 |
return _delegates.remove(delegate); |
71 |
/** |
74 |
} |
72 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#removeFactoryDelegate(org.eclipse.core.runtime.IAdaptable) |
75 |
|
73 |
*/ |
76 |
/** |
74 |
public final boolean removeFactoryDelegate(IAdaptable delegate) |
77 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#getValidDelegateTypes() |
75 |
{ |
78 |
*/ |
76 |
synchronized(_delegates) |
79 |
public final List<Class> getValidDelegateTypes() |
77 |
{ |
80 |
{ |
78 |
return _delegates.remove(delegate); |
81 |
return _supportedDelegates; |
79 |
} |
82 |
} |
80 |
} |
83 |
|
81 |
|
84 |
/** |
82 |
/** |
85 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#isValidDelegate(org.eclipse.core.runtime.IAdaptable) |
83 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#getValidDelegateTypes() |
86 |
*/ |
84 |
*/ |
87 |
public final boolean isValidDelegate(final IAdaptable delegate) |
85 |
public final List<Class> getValidDelegateTypes() |
88 |
{ |
86 |
{ |
89 |
for (final Class clazz : _supportedDelegates) |
87 |
return _supportedDelegates; |
90 |
{ |
88 |
} |
91 |
// if the delegate supports one of the valid delegate classes |
89 |
|
92 |
// via adaptation, then it is a valid delegate |
90 |
/** |
93 |
if (delegate.getAdapter(clazz) != null) |
91 |
* @see org.eclipse.jst.jsf.context.IDelegatingFactory#isValidDelegate(org.eclipse.core.runtime.IAdaptable) |
94 |
{ |
92 |
*/ |
95 |
return true; |
93 |
public final boolean isValidDelegate(IAdaptable delegate) |
96 |
} |
94 |
{ |
97 |
} |
95 |
for (final Iterator<Class> it = _supportedDelegates.iterator(); it.hasNext();) |
98 |
|
96 |
{ |
99 |
// if no found, delegate is not supported |
97 |
final Class clazz = it.next(); |
100 |
return false; |
98 |
|
101 |
} |
99 |
// if the delegate supports one of the valid delegate classes |
|
|
100 |
// via adaptation, then it is a valid delegate |
101 |
if (delegate.getAdapter(clazz) != null) |
102 |
{ |
103 |
return true; |
104 |
} |
105 |
} |
106 |
|
107 |
// if no found, delegate is not supported |
108 |
return false; |
109 |
} |
110 |
} |
102 |
} |