2 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
4 * This program and the accompanying materials are made available under the
5 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6 * and is available at http://www.eclipse.org/legal/epl-v10.html
9 package org.opendaylight.controller.concepts.transform;
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Comparator;
15 import java.util.TreeSet;
18 * Composite transformer which aggregates multiple implementation and selects
19 * the one which accepts the input.
25 * Input class for transformation
27 * Product of transformation
29 public class CompositeConditionalTransformer<I, P> implements
30 SimpleConditionalTransformer<I, P>,
31 AggregateTransformer<I,P> {
33 private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {
36 public int compare(TransformerWithPriority<I, P> o1,
37 TransformerWithPriority<I, P> o2) {
38 return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority));
42 private final Set<TransformerWithPriority<I, P>> transformers;
44 public CompositeConditionalTransformer() {
45 // FIXME: Add Ordering
46 transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);
50 public boolean isAcceptable(I input) {
51 for (SimpleConditionalTransformer<I, P> trans : transformers) {
52 if (trans.isAcceptable(input)) {
60 public P transform(I input) {
61 for (SimpleConditionalTransformer<I, P> trans : transformers) {
62 if (trans.isAcceptable(input)) {
63 return trans.transform(input);
66 throw new IllegalStateException(
67 "Transformer for provided input is not available.");
70 public void addTransformer(SimpleConditionalTransformer<I, P> transformer,
71 int priority) throws IllegalStateException {
72 if (transformer == null) {
73 throw new IllegalArgumentException(
74 "transformer should not be null.");
76 TransformerWithPriority<I, P> withPriority = new TransformerWithPriority<I, P>(
77 transformer, priority);
78 if (false == transformers.add(withPriority)) {
79 throw new IllegalStateException("transformer " + transformer
80 + "already registered");
84 public void removeTransformer(SimpleConditionalTransformer<I, P> transformer)
85 throws IllegalArgumentException {
86 if (transformer == null) {
87 throw new IllegalArgumentException(
88 "transformer should not be null.");
90 if (false == transformers.remove(transformer)) {
91 throw new IllegalStateException("transformer " + transformer
92 + "already registered");
97 public Collection<P> transformAll(Collection<? extends I> inputs) {
98 Collection<P> ret = new ArrayList<P>();
100 ret.add(transform(i));
105 private static class TransformerWithPriority<I, P> implements
106 SimpleConditionalTransformer<I, P> {
108 final SimpleConditionalTransformer<I, P> transformer;
110 public TransformerWithPriority(
111 SimpleConditionalTransformer<I, P> transformer, int priority) {
112 this.priority = priority;
113 this.transformer = transformer;
117 public int hashCode() {
118 final int prime = 31;
120 result = prime * result
121 + ((transformer == null) ? 0 : transformer.hashCode());
126 public boolean equals(Object obj) {
131 if (getClass() != obj.getClass())
133 TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;
134 if (transformer == null) {
135 if (other.transformer != null)
137 } else if (!transformer.equals(other.transformer))
143 public boolean isAcceptable(I input) {
144 return transformer.isAcceptable(input);
148 public P transform(I input) {
149 return transformer.transform(input);