4 * Copyright 2010 Codist Monk.
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 package net
.sourceforge
.aprog
.context
;
27 import static java
.util
.Collections
.unmodifiableCollection
;
29 import java
.util
.HashMap
;
30 import java
.util
.Iterator
;
33 import net
.sourceforge
.aprog
.events
.AbstractObservable
;
34 import net
.sourceforge
.aprog
.events
.AtomicVariable
;
35 import net
.sourceforge
.aprog
.events
.Variable
;
39 * @author codistmonk (creation 2010-06-20)
41 public final class Context
extends AbstractObservable
<Context
.Listener
> implements Iterable
<Variable
<?
>> {
43 private final Map
<String
, Variable
<?
>> variables
;
46 this.variables
= new HashMap
<String
, Variable
<?
>>();
51 * @param <T> The type of the variable value
58 public final <T
> T
get(final String variableName
) {
59 final Variable
<T
> variable
= this.getVariable(variableName
);
61 return variable
== null ?
null : variable
.getValue();
66 * @param <T> The type of the variable value
72 * @return The old value
75 public final <T
> T
set(final String variableName
, final T value
, final Class
<T
> variableType
) {
76 final Variable
<T
> variable
= this.getVariable(variableName
);
78 if (variable
== null) {
79 this.putVariable(new AtomicVariable
<T
>(variableType
, variableName
, value
));
84 final T oldValue
= variable
.getValue();
86 variable
.setValue(value
);
93 * @param <T> The type of the variable value
99 * @return The old value
102 @SuppressWarnings("unchecked")
103 public final <T
> T
set(final String variableName
, final T value
) {
104 return this.set(variableName
, value
, (Class
<T
>) (value
== null ? Object
.class : value
.getClass()));
109 * @param <T> The type of the variable value
110 * @param variableName
112 * @return The removed variable
115 public final <T
> Variable
<T
> remove(final String variableName
) {
116 @SuppressWarnings("unchecked")
117 final Variable
<T
> removedVariable
= (Variable
<T
>) this.variables
.remove(variableName
);
119 if (removedVariable
!= null) {
120 new VariableRemovedEvent
<T
>(removedVariable
).fire();
123 return removedVariable
;
128 * @param <T> The type of the variable value
129 * @param variableName
135 @SuppressWarnings("unchecked")
136 public final <T
> Variable
<T
> getVariable(final String variableName
) {
137 return (Variable
<T
>) this.variables
.get(variableName
);
142 * @param <T> The type of the new variable value
143 * @param <U> The type of the old variable value
147 * @return the old variable
150 public final <T
, U
> Variable
<U
> putVariable(final Variable
<T
> variable
) {
151 final Variable
<U
> oldVariable
= this.remove(variable
.getName());
153 this.variables
.put(variable
.getName(), variable
);
155 new VariableAddedEvent
<T
>(variable
).fire();
161 public final Iterator
<Variable
<?
>> iterator() {
162 return unmodifiableCollection(this.variables
.values()).iterator();
167 * @author codistmonk (creation 2010-06-20)
169 public static interface Listener
{
176 public abstract void variableAdded(VariableAddedEvent
<?
> event
);
183 public abstract void variableRemoved(VariableRemovedEvent
<?
> event
);
189 * @param <T> the type of the variable value
190 * @author codistmonk (creation 2010-06-20)
192 public abstract class AbstractEvent
<T
> extends AbstractObservable
<Listener
>.AbstractEvent
<Context
, Listener
> {
194 private final Variable
<T
> variable
;
202 public AbstractEvent(final Variable
<T
> variable
) {
203 this.variable
= variable
;
212 public final Variable
<T
> getVariable() {
213 return this.variable
;
220 * @param <T> The type of the variable value
221 * @author codistmonk (creation 2010-06-20)
223 public final class VariableAddedEvent
<T
> extends AbstractEvent
<T
> {
231 public VariableAddedEvent(final Variable
<T
> variable
) {
236 protected final void notifyListener(final Listener listener
) {
237 listener
.variableAdded(this);
244 * @param <T> The type of the variable value
245 * @author codistmonk (creation 2010-06-20)
247 public final class VariableRemovedEvent
<T
> extends AbstractEvent
<T
> {
251 * @param removedVariable
255 public VariableRemovedEvent(final Variable
<T
> removedVariable
) {
256 super(removedVariable
);
260 protected final void notifyListener(final Listener listener
) {
261 listener
.variableRemoved(this);