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.lang;
10 import java.util.ArrayList;
11 import java.util.Collection;
12 import java.util.Comparator;
14 import java.util.TreeSet;
17 * Composite transformer which aggregates multiple implementation and selects
18 * the one which accepts the input.
24 * Input class for transformation
26 * Product of transformation
28 public class CompositeConditionalTransformer<I, P> implements
29 SimpleConditionalTransformer<I, P>,
30 AggregateTransformer<I,P> {
32 private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {
35 public int compare(TransformerWithPriority<I, P> o1,
36 TransformerWithPriority<I, P> o2) {
37 return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority));
41 private final Set<TransformerWithPriority<I, P>> transformers;
43 public CompositeConditionalTransformer() {
44 // FIXME: Add Ordering
45 transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);
49 public boolean isAcceptable(I input) {
50 for (SimpleConditionalTransformer<I, P> trans : transformers) {
51 if (trans.isAcceptable(input)) {
59 public P transform(I input) {
60 for (SimpleConditionalTransformer<I, P> trans : transformers) {
61 if (trans.isAcceptable(input)) {
62 return trans.transform(input);
65 throw new IllegalStateException(
66 "Transformer for provided input is not available.");
69 public void addTransformer(SimpleConditionalTransformer<I, P> transformer,
70 int priority) throws IllegalStateException {
71 if (transformer == null) {
72 throw new IllegalArgumentException(
73 "transformer should not be null.");
75 TransformerWithPriority<I, P> withPriority = new TransformerWithPriority<I, P>(
76 transformer, priority);
77 if (false == transformers.add(withPriority)) {
78 throw new IllegalStateException("transformer " + transformer
79 + "already registered");
83 public void removeTransformer(SimpleConditionalTransformer<I, P> transformer)
84 throws IllegalArgumentException {
85 if (transformer == null) {
86 throw new IllegalArgumentException(
87 "transformer should not be null.");
89 if (false == transformers.remove(transformer)) {
90 throw new IllegalStateException("transformer " + transformer
91 + "already registered");
96 public Collection<P> transformAll(Collection<? extends I> inputs) {
97 Collection<P> ret = new ArrayList<P>();
99 ret.add(transform(i));
104 private static class TransformerWithPriority<I, P> implements
105 SimpleConditionalTransformer<I, P> {
107 final SimpleConditionalTransformer<I, P> transformer;
109 public TransformerWithPriority(
110 SimpleConditionalTransformer<I, P> transformer, int priority) {
111 this.priority = priority;
112 this.transformer = transformer;
116 public int hashCode() {
117 final int prime = 31;
119 result = prime * result
120 + ((transformer == null) ? 0 : transformer.hashCode());
125 public boolean equals(Object obj) {
130 if (getClass() != obj.getClass())
132 TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;
133 if (transformer == null) {
134 if (other.transformer != null)
136 } else if (!transformer.equals(other.transformer))
142 public boolean isAcceptable(I input) {
143 return transformer.isAcceptable(input);
147 public P transform(I input) {
148 return transformer.transform(input);