[Aprog]
[aprog.git] / Aprog / src / net / sourceforge / aprog / context / Context.java
blob28159cd498ac7a4a0870082655817876f0dbd9ed
1 /*
2 * The MIT License
3 *
4 * Copyright 2010 Codist Monk.
5 *
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
22 * THE SOFTWARE.
25 package net.sourceforge.aprog.context;
27 import static java.util.Collections.unmodifiableCollection;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.Map;
33 import net.sourceforge.aprog.events.AbstractObservable;
34 import net.sourceforge.aprog.events.AtomicVariable;
35 import net.sourceforge.aprog.events.Variable;
37 /**
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;
45 public Context() {
46 this.variables = new HashMap<String, Variable<?>>();
49 /**
51 * @param <T> The type of the variable value
52 * @param variableName
53 * <br>Not null
54 * @return
55 * <br>Maybe null
56 * <br>Shared
58 public final <T> T get(final String variableName) {
59 final Variable<T> variable = this.getVariable(variableName);
61 return variable == null ? null : variable.getValue();
64 /**
66 * @param <T> The type of the variable value
67 * @param variableName
68 * <br>Not null
69 * @param value
70 * <br>Maybe null
71 * <br>Shared
72 * @return The old value
73 * <br>Maybe null
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));
81 return null;
84 final T oldValue = variable.getValue();
86 variable.setValue(value);
88 return oldValue;
91 /**
93 * @param <T> The type of the variable value
94 * @param variableName
95 * <br>Not null
96 * @param value
97 * <br>Maybe null
98 * <br>Shared
99 * @return The old value
100 * <br>Maybe null
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
111 * <br>Maybe null
112 * @return The removed variable
113 * <br>Maybe null
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
130 * <br>Not null
131 * @return
132 * <br>Maybe null
133 * <br>Shared
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
144 * @param variable
145 * <br>Not null
146 * <br>Shared
147 * @return the old variable
148 * <br>Maybe null
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();
157 return oldVariable;
160 @Override
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 {
173 * @param event
174 * <br>Not null
176 public abstract void variableAdded(VariableAddedEvent<?> event);
180 * @param event
181 * <br>Not null
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;
198 * @param variable
199 * <br>Not null
200 * <br>Shared
202 public AbstractEvent(final Variable<T> variable) {
203 this.variable = variable;
208 * @return
209 * <br>Not null
210 * <br>Shared
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> {
227 * @param variable
228 * <br>Not null
229 * <br>Shared
231 public VariableAddedEvent(final Variable<T> variable) {
232 super(variable);
235 @Override
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
252 * <br>Not null
253 * <br>Shared
255 public VariableRemovedEvent(final Variable<T> removedVariable) {
256 super(removedVariable);
259 @Override
260 protected final void notifyListener(final Listener listener) {
261 listener.variableRemoved(this);