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