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

©2013 OpenDaylight, A Linux Foundation Collaborative Project. All Rights Reserved.
OpenDaylight is a registered trademark of The OpenDaylight Project, Inc.
Linux Foundation and OpenDaylight are registered trademarks of the Linux Foundation.
Linux is a registered trademark of Linus Torvalds.