X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=blobdiff_plain;f=opendaylight%2Fcommons%2Fconcepts%2Fsrc%2Fmain%2Fjava%2Forg%2Fopendaylight%2Fcontroller%2Fconcepts%2Ftransform%2FCompositeConditionalTransformer.java;h=0cdc3a156ffc8f55d5f1e6140f143ed51affc58f;hb=refs%2Fchanges%2F69%2F27369%2F1;hp=d11f319a79af3b727151b4c6cdd1bd74d02bd52d;hpb=fad07e38e57f1c0d8d687e3bae01532196d6e99d;p=controller.git diff --git a/opendaylight/commons/concepts/src/main/java/org/opendaylight/controller/concepts/transform/CompositeConditionalTransformer.java b/opendaylight/commons/concepts/src/main/java/org/opendaylight/controller/concepts/transform/CompositeConditionalTransformer.java index d11f319a79..0cdc3a156f 100644 --- a/opendaylight/commons/concepts/src/main/java/org/opendaylight/controller/concepts/transform/CompositeConditionalTransformer.java +++ b/opendaylight/commons/concepts/src/main/java/org/opendaylight/controller/concepts/transform/CompositeConditionalTransformer.java @@ -1,157 +1,157 @@ -/* - * 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 - * Input class for transformation - * @param

- * Product of transformation - */ -public class CompositeConditionalTransformer implements - SimpleConditionalTransformer, - AggregateTransformer { - - private final Comparator> comparator = new Comparator>() { - - @Override - public int compare(TransformerWithPriority o1, - TransformerWithPriority o2) { - return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority)); - } - - }; - private final Set> transformers; - - public CompositeConditionalTransformer() { - // FIXME: Add Ordering - transformers = new TreeSet>(comparator); - } - - @Override - public boolean isAcceptable(I input) { - for (SimpleConditionalTransformer trans : transformers) { - if (trans.isAcceptable(input)) { - return true; - } - } - return false; - } - - @Override - public P transform(I input) { - for (SimpleConditionalTransformer trans : transformers) { - if (trans.isAcceptable(input)) { - return trans.transform(input); - } - } - throw new IllegalStateException( - "Transformer for provided input is not available."); - } - - public void addTransformer(SimpleConditionalTransformer transformer, - int priority) throws IllegalStateException { - if (transformer == null) { - throw new IllegalArgumentException( - "transformer should not be null."); - } - TransformerWithPriority withPriority = new TransformerWithPriority( - transformer, priority); - if (false == transformers.add(withPriority)) { - throw new IllegalStateException("transformer " + transformer - + "already registered"); - } - } - - public void removeTransformer(SimpleConditionalTransformer 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

transformAll(Collection inputs) { - Collection

ret = new ArrayList

(); - for (I i : inputs) { - ret.add(transform(i)); - } - return ret; - } - - private static class TransformerWithPriority implements - SimpleConditionalTransformer { - final int priority; - final SimpleConditionalTransformer transformer; - - public TransformerWithPriority( - SimpleConditionalTransformer 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); - } - - - - - - } -} +/* + * 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 + * Input class for transformation + * @param

+ * Product of transformation + */ +public class CompositeConditionalTransformer implements + SimpleConditionalTransformer, + AggregateTransformer { + + private final Comparator> comparator = new Comparator>() { + + @Override + public int compare(TransformerWithPriority o1, + TransformerWithPriority o2) { + return Integer.valueOf(o1.priority).compareTo(Integer.valueOf(o2.priority)); + } + + }; + private final Set> transformers; + + public CompositeConditionalTransformer() { + // FIXME: Add Ordering + transformers = new TreeSet>(comparator); + } + + @Override + public boolean isAcceptable(I input) { + for (SimpleConditionalTransformer trans : transformers) { + if (trans.isAcceptable(input)) { + return true; + } + } + return false; + } + + @Override + public P transform(I input) { + for (SimpleConditionalTransformer trans : transformers) { + if (trans.isAcceptable(input)) { + return trans.transform(input); + } + } + throw new IllegalStateException( + "Transformer for provided input is not available."); + } + + public void addTransformer(SimpleConditionalTransformer transformer, + int priority) throws IllegalStateException { + if (transformer == null) { + throw new IllegalArgumentException( + "transformer should not be null."); + } + TransformerWithPriority withPriority = new TransformerWithPriority( + transformer, priority); + if (false == transformers.add(withPriority)) { + throw new IllegalStateException("transformer " + transformer + + "already registered"); + } + } + + public void removeTransformer(SimpleConditionalTransformer 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

transformAll(Collection inputs) { + Collection

ret = new ArrayList

(); + for (I i : inputs) { + ret.add(transform(i)); + } + return ret; + } + + private static class TransformerWithPriority implements + SimpleConditionalTransformer { + final int priority; + final SimpleConditionalTransformer transformer; + + public TransformerWithPriority( + SimpleConditionalTransformer 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); + } + + + + + + } +}