2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
\r
4 * This program and the accompanying materials are made available under the
\r
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
\r
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
\r
9 package org.opendaylight.controller.concepts.tranform;
\r
11 import java.util.ArrayList;
\r
12 import java.util.Collection;
\r
13 import java.util.Map;
\r
14 import java.util.concurrent.ConcurrentHashMap;
\r
18 * Transformer which aggregates multiple implementations of
\r
19 * {@link InputClassBasedTransformer}.
\r
21 * The transformation process is driven by {@link Class} of input. The selection
\r
22 * of used {@link InputClassBasedTransformer} is done by using the {@link Class}
\r
23 * of input as a key to select the transformer.
\r
25 * This approach provides quick resolution of transformer, but does not support
\r
26 * registering a super type of input to provide transformation support for all
\r
27 * subclasses, one must register a new instance of transformer for each valid
\r
30 * If you need more flexible selection of transformation consider using
\r
31 * {@link CompositeConditionalTransformer} which is slower but most flexible or
\r
32 * {@link RuleBasedTransformer} which provides declarative approach for
\r
35 * See {@link #transform(Object)} for more information about tranformation
\r
38 * @author Tony Tkacik <ttkacik@cisco.com>
\r
45 public abstract class CompositeClassBasedTransformer<I, P> implements
\r
46 InputClassBasedTransformer<I, I, P>,
\r
47 AggregateTransformer<I, P> {
\r
49 private Map<Class<? extends I>, InputClassBasedTransformer<I, ? extends I, P>> transformers = new ConcurrentHashMap<Class<? extends I>, InputClassBasedTransformer<I, ? extends I, P>>();
\r
52 * Transforms an input into instance of Product class.
\r
54 * The final registered transformer is the one which match following
\r
57 * <code>input.getClass() == transformer.getInputClass()</code>
\r
59 * This means that transformers are not resolved by class hierarchy, only
\r
60 * selected based on final class of the input. If you need more flexible
\r
61 * selection of transformation consider using
\r
62 * {@link CompositeConditionalTransformer} which is slower but more
\r
67 public P transform(I input) {
\r
68 @SuppressWarnings("unchecked")
\r
69 InputClassBasedTransformer<I, I, P> transformer = (InputClassBasedTransformer<I, I, P>) transformers
\r
70 .get(input.getClass());
\r
71 if (transformer == null)
\r
72 throw new IllegalArgumentException("Transformation of: " + input
\r
73 + " is not supported");
\r
74 return transformer.transform(input);
\r
78 * Registers a new transformer.
\r
80 * The transformer is registered for class returned by
\r
81 * {@link InputClassBasedTransformer#getInputClass()}. Only one transformer
\r
82 * can be registered for particular input class.
\r
85 public void addTransformer(
\r
86 InputClassBasedTransformer<I, ? extends I, P> transformer)
\r
87 throws IllegalStateException {
\r
88 if (transformer == null)
\r
89 throw new IllegalArgumentException("Transformer should not be null");
\r
90 if (transformer.getInputClass() == null)
\r
91 throw new IllegalArgumentException(
\r
92 "Transformer should specify input class.");
\r
93 transformers.put(transformer.getInputClass(), transformer);
\r
97 * Removes an registered transformer.
\r
99 * Note: Removal is currently unsupported.
\r
101 * @param transformer
\r
102 * Tranformer to be removed.
\r
103 * @throws IllegalArgumentException
\r
104 * If the provided transformer is null or is not registered.
\r
106 public void removeTransformer(
\r
107 InputClassBasedTransformer<I, ? extends I, P> transformer)
\r
108 throws IllegalArgumentException {
\r
109 throw new UnsupportedOperationException("Not implemented yet");
\r
113 public Collection<P> transformAll(Collection<? extends I> inputs) {
\r
114 Collection<P> ret = new ArrayList<P>();
\r
115 for (I i : inputs) {
\r
116 ret.add(transform(i));
\r