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.Comparator;
\r
14 import java.util.Set;
\r
15 import java.util.TreeSet;
\r
18 * Composite transformer which aggregates multiple implementation and selects
\r
19 * the one which accepts the input.
\r
22 * @author Tony Tkacik
\r
25 * Input class for transformation
\r
27 * Product of transformation
\r
29 public class CompositeConditionalTransformer<I, P> implements
\r
30 SimpleConditionalTransformer<I, P>,
\r
31 AggregateTransformer<I,P> {
\r
33 private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {
\r
36 public int compare(TransformerWithPriority<I, P> o1,
\r
37 TransformerWithPriority<I, P> o2) {
\r
38 return Integer.compare(o1.priority, o2.priority);
\r
42 private final Set<TransformerWithPriority<I, P>> transformers;
\r
44 public CompositeConditionalTransformer() {
\r
45 // FIXME: Add Ordering
\r
46 transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);
\r
50 public boolean isAcceptable(I input) {
\r
51 for (SimpleConditionalTransformer<I, P> trans : transformers) {
\r
52 if (trans.isAcceptable(input)) {
\r
60 public P transform(I input) {
\r
61 for (SimpleConditionalTransformer<I, P> trans : transformers) {
\r
62 if (trans.isAcceptable(input)) {
\r
63 return trans.transform(input);
\r
66 throw new IllegalStateException(
\r
67 "Transformer for provided input is not available.");
\r
70 public void addTransformer(SimpleConditionalTransformer<I, P> transformer,
\r
71 int priority) throws IllegalStateException {
\r
72 if (transformer == null) {
\r
73 throw new IllegalArgumentException(
\r
74 "transformer should not be null.");
\r
76 TransformerWithPriority<I, P> withPriority = new TransformerWithPriority<I, P>(
\r
77 transformer, priority);
\r
78 if (false == transformers.add(withPriority)) {
\r
79 throw new IllegalStateException("transformer " + transformer
\r
80 + "already registered");
\r
84 public void removeTransformer(SimpleConditionalTransformer<I, P> transformer)
\r
85 throws IllegalArgumentException {
\r
86 if (transformer == null) {
\r
87 throw new IllegalArgumentException(
\r
88 "transformer should not be null.");
\r
90 if (false == transformers.remove(transformer)) {
\r
91 throw new IllegalStateException("transformer " + transformer
\r
92 + "already registered");
\r
97 public Collection<P> transformAll(Collection<? extends I> inputs) {
\r
98 Collection<P> ret = new ArrayList<P>();
\r
99 for (I i : inputs) {
\r
100 ret.add(transform(i));
\r
105 private static class TransformerWithPriority<I, P> implements
\r
106 SimpleConditionalTransformer<I, P> {
\r
107 final int priority;
\r
108 final SimpleConditionalTransformer<I, P> transformer;
\r
110 public TransformerWithPriority(
\r
111 SimpleConditionalTransformer<I, P> transformer, int priority) {
\r
112 this.priority = priority;
\r
113 this.transformer = transformer;
\r
117 public int hashCode() {
\r
118 final int prime = 31;
\r
120 result = prime * result
\r
121 + ((transformer == null) ? 0 : transformer.hashCode());
\r
126 public boolean equals(Object obj) {
\r
131 if (getClass() != obj.getClass())
\r
133 TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;
\r
134 if (transformer == null) {
\r
135 if (other.transformer != null)
\r
137 } else if (!transformer.equals(other.transformer))
\r
143 public boolean isAcceptable(I input) {
\r
144 return transformer.isAcceptable(input);
\r
148 public P transform(I input) {
\r
149 return transformer.transform(input);
\r