DictionaryDispatcher.java
/***************************************************************************
Copyright 2012 Emily Estes
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
***************************************************************************/
package net.metanotion.util;
import java.util.Map;
/** A dispatcher that assumes the abstract dispatch data can be looked in up a Dictionary instance.
This dispatcher takes a dictionary in it's constructor, and then looks up the data from dispatched requests with that
dictionary. The dictionary will provide a dispatcher instance that is then used to dispatch the request(on the same data
element).
@param <D> The type values indexed by the dictionary keys to look up the appropriate dispatcher.
@param <I> the receiver type that all the dispatchers create messages for.
*/
public final class DictionaryDispatcher<I,D> implements Dispatcher<I,Map.Entry<String,D>> {
private final Dictionary<String,Dispatcher<I,D>> dispatchers;
private final Dispatcher<I,Map.Entry<String,D>> parent;
/** Create a DictionaryDispatcher using a {@link java.util.Map} as the dictionary.
@param map the map to use as the dictionary, will be wrapped in {@link net.metanotion.util.MapDictionaryAdapter}.
*/
public DictionaryDispatcher(final Map<String,Dispatcher<I,D>> map) {
this(new MapDictionaryAdapter<>(map), null);
}
/** Create a DictionaryDispatcher from a {@link java.util.Map}, with a fallback dispatcher in case look up fails.
@param map the map to use as the dictionary, will be wrapped in {@link net.metanotion.util.MapDictionaryAdapter}.
@param parent The dispatcher to use if look up fails.
*/
public DictionaryDispatcher(final Map<String,Dispatcher<I,D>> map, final Dispatcher<I,Map.Entry<String,D>> parent) {
this(new MapDictionaryAdapter<>(map), parent);
}
/** Create a basic DictionaryDispatcher.
@param dictionary The dictionary to use for dispatcher lookups.
*/
public DictionaryDispatcher(final Dictionary<String,Dispatcher<I,D>> dictionary) {
this(dictionary, null);
}
/** Create a DictionaryDispatcher with a fallback dispatcher in case look up fails.
@param dictionary The dictionary to use for dispatcher lookups.
@param parent The dispatcher to use if look up fails.
*/
public DictionaryDispatcher(final Dictionary<String,Dispatcher<I,D>> dictionary,
final Dispatcher<I,Map.Entry<String,D>> parent) {
this.dispatchers = dictionary;
this.parent = parent;
}
// Dispatcher
@Override public Message<I> dispatch(final Map.Entry<String,D> data) {
final Dispatcher<I,D> d = dispatchers.get(data.getKey());
if(d != null) { return d.dispatch(data.getValue()); }
return parent.dispatch(data);
}
}