Bug 4202: Migrate also toaster to use mdsal project
[controller.git] / opendaylight / commons / concepts / src / main / java / org / opendaylight / controller / concepts / transform / CompositeConditionalTransformer.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.concepts.transform;
10
11 import java.util.ArrayList;
12 import java.util.Collection;
13 import java.util.Comparator;
14 import java.util.Set;
15 import java.util.TreeSet;
16
17 /**
18  * Composite transformer which aggregates multiple implementation and selects
19  * the one which accepts the input.
20  *
21  *
22  * @author Tony Tkacik
23  *
24  * @param <I>
25  *            Input class for transformation
26  * @param <P>
27  *            Product of transformation
28  */
29 public class CompositeConditionalTransformer<I, P> implements
30         SimpleConditionalTransformer<I, P>,
31         AggregateTransformer<I,P> {
32
33     private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {
34
35         @Override
36         public int compare(TransformerWithPriority<I, P> o1,
37                 TransformerWithPriority<I, P> o2) {
38             return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority));
39         }
40
41     };
42     private final Set<TransformerWithPriority<I, P>> transformers;
43
44     public CompositeConditionalTransformer() {
45         // FIXME: Add Ordering
46         transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);
47     }
48
49     @Override
50     public boolean isAcceptable(I input) {
51         for (SimpleConditionalTransformer<I, P> trans : transformers) {
52             if (trans.isAcceptable(input)) {
53                 return true;
54             }
55         }
56         return false;
57     }
58
59     @Override
60     public P transform(I input) {
61         for (SimpleConditionalTransformer<I, P> trans : transformers) {
62             if (trans.isAcceptable(input)) {
63                 return trans.transform(input);
64             }
65         }
66         throw new IllegalStateException(
67                 "Transformer for provided input is not available.");
68     }
69
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.");
75         }
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");
81         }
82     }
83
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.");
89         }
90         if (false == transformers.remove(transformer)) {
91             throw new IllegalStateException("transformer " + transformer
92                     + "already registered");
93         }
94     }
95
96     @Override
97     public Collection<P> transformAll(Collection<? extends I> inputs) {
98         Collection<P> ret = new ArrayList<P>();
99         for (I i : inputs) {
100             ret.add(transform(i));
101         }
102         return ret;
103     }
104
105     private static class TransformerWithPriority<I, P> implements
106             SimpleConditionalTransformer<I, P> {
107         final int priority;
108         final SimpleConditionalTransformer<I, P> transformer;
109
110         public TransformerWithPriority(
111                 SimpleConditionalTransformer<I, P> transformer, int priority) {
112             this.priority = priority;
113             this.transformer = transformer;
114         }
115
116         @Override
117         public int hashCode() {
118             final int prime = 31;
119             int result = 1;
120             result = prime * result
121                     + ((transformer == null) ? 0 : transformer.hashCode());
122             return result;
123         }
124
125         @Override
126         public boolean equals(Object obj) {
127             if (this == obj)
128                 return true;
129             if (obj == null)
130                 return false;
131             if (getClass() != obj.getClass())
132                 return false;
133             TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;
134             if (transformer == null) {
135                 if (other.transformer != null)
136                     return false;
137             } else if (!transformer.equals(other.transformer))
138                 return false;
139             return true;
140         }
141
142         @Override
143         public boolean isAcceptable(I input) {
144             return transformer.isAcceptable(input);
145         }
146
147         @Override
148         public P transform(I input) {
149             return transformer.transform(input);
150         }
151
152
153
154
155
156     }
157 }