-/*\r
- * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.\r
- *\r
- * This program and the accompanying materials are made available under the\r
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,\r
- * and is available at http://www.eclipse.org/legal/epl-v10.html\r
- */\r
-\r
-package org.opendaylight.controller.concepts.transform;\r
-\r
-import java.util.ArrayList;\r
-import java.util.Collection;\r
-import java.util.Comparator;\r
-import java.util.Set;\r
-import java.util.TreeSet;\r
-\r
-/**\r
- * Composite transformer which aggregates multiple implementation and selects\r
- * the one which accepts the input.\r
- *\r
- *\r
- * @author Tony Tkacik\r
- *\r
- * @param <I>\r
- * Input class for transformation\r
- * @param <P>\r
- * Product of transformation\r
- */\r
-public class CompositeConditionalTransformer<I, P> implements\r
- SimpleConditionalTransformer<I, P>,\r
- AggregateTransformer<I,P> {\r
-\r
- private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {\r
-\r
- @Override\r
- public int compare(TransformerWithPriority<I, P> o1,\r
- TransformerWithPriority<I, P> o2) {\r
- return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority));\r
- }\r
-\r
- };\r
- private final Set<TransformerWithPriority<I, P>> transformers;\r
-\r
- public CompositeConditionalTransformer() {\r
- // FIXME: Add Ordering\r
- transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);\r
- }\r
-\r
- @Override\r
- public boolean isAcceptable(I input) {\r
- for (SimpleConditionalTransformer<I, P> trans : transformers) {\r
- if (trans.isAcceptable(input)) {\r
- return true;\r
- }\r
- }\r
- return false;\r
- }\r
-\r
- @Override\r
- public P transform(I input) {\r
- for (SimpleConditionalTransformer<I, P> trans : transformers) {\r
- if (trans.isAcceptable(input)) {\r
- return trans.transform(input);\r
- }\r
- }\r
- throw new IllegalStateException(\r
- "Transformer for provided input is not available.");\r
- }\r
-\r
- public void addTransformer(SimpleConditionalTransformer<I, P> transformer,\r
- int priority) throws IllegalStateException {\r
- if (transformer == null) {\r
- throw new IllegalArgumentException(\r
- "transformer should not be null.");\r
- }\r
- TransformerWithPriority<I, P> withPriority = new TransformerWithPriority<I, P>(\r
- transformer, priority);\r
- if (false == transformers.add(withPriority)) {\r
- throw new IllegalStateException("transformer " + transformer\r
- + "already registered");\r
- }\r
- }\r
-\r
- public void removeTransformer(SimpleConditionalTransformer<I, P> transformer)\r
- throws IllegalArgumentException {\r
- if (transformer == null) {\r
- throw new IllegalArgumentException(\r
- "transformer should not be null.");\r
- }\r
- if (false == transformers.remove(transformer)) {\r
- throw new IllegalStateException("transformer " + transformer\r
- + "already registered");\r
- }\r
- }\r
-\r
- @Override\r
- public Collection<P> transformAll(Collection<? extends I> inputs) {\r
- Collection<P> ret = new ArrayList<P>();\r
- for (I i : inputs) {\r
- ret.add(transform(i));\r
- }\r
- return ret;\r
- }\r
-\r
- private static class TransformerWithPriority<I, P> implements\r
- SimpleConditionalTransformer<I, P> {\r
- final int priority;\r
- final SimpleConditionalTransformer<I, P> transformer;\r
-\r
- public TransformerWithPriority(\r
- SimpleConditionalTransformer<I, P> transformer, int priority) {\r
- this.priority = priority;\r
- this.transformer = transformer;\r
- }\r
-\r
- @Override\r
- public int hashCode() {\r
- final int prime = 31;\r
- int result = 1;\r
- result = prime * result\r
- + ((transformer == null) ? 0 : transformer.hashCode());\r
- return result;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj) {\r
- if (this == obj)\r
- return true;\r
- if (obj == null)\r
- return false;\r
- if (getClass() != obj.getClass())\r
- return false;\r
- TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;\r
- if (transformer == null) {\r
- if (other.transformer != null)\r
- return false;\r
- } else if (!transformer.equals(other.transformer))\r
- return false;\r
- return true;\r
- }\r
-\r
- @Override\r
- public boolean isAcceptable(I input) {\r
- return transformer.isAcceptable(input);\r
- }\r
-\r
- @Override\r
- public P transform(I input) {\r
- return transformer.transform(input);\r
- }\r
-\r
-\r
-\r
-\r
-\r
- }\r
-}\r
+/*
+ * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+
+package org.opendaylight.controller.concepts.transform;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Comparator;
+import java.util.Set;
+import java.util.TreeSet;
+
+/**
+ * Composite transformer which aggregates multiple implementation and selects
+ * the one which accepts the input.
+ *
+ *
+ * @author Tony Tkacik
+ *
+ * @param <I>
+ * Input class for transformation
+ * @param <P>
+ * Product of transformation
+ */
+public class CompositeConditionalTransformer<I, P> implements
+ SimpleConditionalTransformer<I, P>,
+ AggregateTransformer<I,P> {
+
+ private final Comparator<TransformerWithPriority<I, P>> comparator = new Comparator<TransformerWithPriority<I, P>>() {
+
+ @Override
+ public int compare(TransformerWithPriority<I, P> o1,
+ TransformerWithPriority<I, P> o2) {
+ return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority));
+ }
+
+ };
+ private final Set<TransformerWithPriority<I, P>> transformers;
+
+ public CompositeConditionalTransformer() {
+ // FIXME: Add Ordering
+ transformers = new TreeSet<TransformerWithPriority<I, P>>(comparator);
+ }
+
+ @Override
+ public boolean isAcceptable(I input) {
+ for (SimpleConditionalTransformer<I, P> trans : transformers) {
+ if (trans.isAcceptable(input)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ @Override
+ public P transform(I input) {
+ for (SimpleConditionalTransformer<I, P> trans : transformers) {
+ if (trans.isAcceptable(input)) {
+ return trans.transform(input);
+ }
+ }
+ throw new IllegalStateException(
+ "Transformer for provided input is not available.");
+ }
+
+ public void addTransformer(SimpleConditionalTransformer<I, P> transformer,
+ int priority) throws IllegalStateException {
+ if (transformer == null) {
+ throw new IllegalArgumentException(
+ "transformer should not be null.");
+ }
+ TransformerWithPriority<I, P> withPriority = new TransformerWithPriority<I, P>(
+ transformer, priority);
+ if (false == transformers.add(withPriority)) {
+ throw new IllegalStateException("transformer " + transformer
+ + "already registered");
+ }
+ }
+
+ public void removeTransformer(SimpleConditionalTransformer<I, P> transformer)
+ throws IllegalArgumentException {
+ if (transformer == null) {
+ throw new IllegalArgumentException(
+ "transformer should not be null.");
+ }
+ if (false == transformers.remove(transformer)) {
+ throw new IllegalStateException("transformer " + transformer
+ + "already registered");
+ }
+ }
+
+ @Override
+ public Collection<P> transformAll(Collection<? extends I> inputs) {
+ Collection<P> ret = new ArrayList<P>();
+ for (I i : inputs) {
+ ret.add(transform(i));
+ }
+ return ret;
+ }
+
+ private static class TransformerWithPriority<I, P> implements
+ SimpleConditionalTransformer<I, P> {
+ final int priority;
+ final SimpleConditionalTransformer<I, P> transformer;
+
+ public TransformerWithPriority(
+ SimpleConditionalTransformer<I, P> transformer, int priority) {
+ this.priority = priority;
+ this.transformer = transformer;
+ }
+
+ @Override
+ public int hashCode() {
+ final int prime = 31;
+ int result = 1;
+ result = prime * result
+ + ((transformer == null) ? 0 : transformer.hashCode());
+ return result;
+ }
+
+ @Override
+ public boolean equals(Object obj) {
+ if (this == obj)
+ return true;
+ if (obj == null)
+ return false;
+ if (getClass() != obj.getClass())
+ return false;
+ TransformerWithPriority<?,?> other = (TransformerWithPriority<?,?>) obj;
+ if (transformer == null) {
+ if (other.transformer != null)
+ return false;
+ } else if (!transformer.equals(other.transformer))
+ return false;
+ return true;
+ }
+
+ @Override
+ public boolean isAcceptable(I input) {
+ return transformer.isAcceptable(input);
+ }
+
+ @Override
+ public P transform(I input) {
+ return transformer.transform(input);
+ }
+
+
+
+
+
+ }
+}