refactoring of the config interface towards more reuse in the implementation and...
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / Transformations.java
index 07d086b65a8dcbd03e3b5d47bdd1b623792ba854..c297fac787ce5ea66ca66339435fdd85bc59a8c7 100644 (file)
+/*
+ * Copyright 2005-2011 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
 package org.wamblee.xmlrouter.impl;
 
 import java.util.ArrayList;
 import java.util.Collection;
-import java.util.Collections;
 import java.util.HashSet;
-import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
-import java.util.concurrent.atomic.AtomicInteger;
 
 import org.wamblee.xmlrouter.common.Id;
+import org.wamblee.xmlrouter.config.Config;
 import org.wamblee.xmlrouter.config.Transformation;
 
+/**
+ * This class manages transformations and computes the shortest transformations
+ * paths based on the provided transformations.
+ * 
+ * @author Erik Brakkee
+ * 
+ */
 public class Transformations {
 
-    private AtomicInteger sequenceNumber;
-    private Map<Integer, Transformation> transformations;
+    private Config<Transformation> transformations;
     private List<String> vertices;
     private TransformationPath[][] matrix;
 
     private Map<String, List<TransformationPath>> sequences;
 
+    /**
+     * Construct the transformations.
+     */
     public Transformations() {
-        sequenceNumber = new AtomicInteger(1);
-        transformations = new LinkedHashMap<Integer, Transformation>();
+        transformations = new ConfigImpl<Transformation>() {
+            @Override
+            public Transformation wrap(Id<Transformation> aId,
+                Transformation aType) {
+                return new RobustTransformation(aId, aType);
+            }
+        };
         vertices = new ArrayList<String>();
         matrix = new TransformationPath[0][0];
     }
 
+    public Config<Transformation> getTransformationConfig() {
+        return new Config<Transformation>() {
+            @Override
+            public Id<Transformation> add(Transformation aT) {
+                return addTransformation(aT);
+            }
+
+            @Override
+            public Transformation get(Id<Transformation> aId) {
+                return transformations.get(aId);
+            }
+
+            @Override
+            public Collection<Id<Transformation>> ids() {
+                return transformations.ids();
+            }
+
+            @Override
+            public boolean remove(Id<Transformation> aId) {
+                return transformations.remove(aId);
+            }
+        };
+    }
+
+    /**
+     * Adds a transformation. Leads to recomputation of shortest paths.
+     * 
+     * @param aTransformation
+     *            Transformation to add.
+     * @return Id of the transformation.
+     */
     public Id<Transformation> addTransformation(Transformation aTransformation) {
-        int seqno = sequenceNumber.getAndIncrement();
-        Id<Transformation> id = new Id<Transformation>(seqno);
-        transformations.put(seqno,
-            new RobustTransformation(id, aTransformation));
+        Id<Transformation> id = transformations.add(aTransformation);
         computeTransformationSequences();
         return id;
     }
 
+    /**
+     * Gets the possible target types based on an input type.
+     * 
+     * @param aType
+     *            Input type.
+     * @return Possible target types.
+     */
     public Collection<String> getPossibleTargetTypes(String aType) {
         int index = vertices.indexOf(aType);
         Set<String> res = new HashSet<String>();
@@ -65,7 +127,7 @@ public class Transformations {
      */
     public TransformationPath getPath(String aFrom, String aTo) {
         int i = vertices.indexOf(aFrom);
-        if (i == -1) {
+        if (i < 0) {
             if (aFrom.equals(aTo)) {
                 return new TransformationPath();
             }
@@ -73,15 +135,22 @@ public class Transformations {
         }
 
         int j = vertices.indexOf(aTo);
+        if (j < 0) {
+            return null;
+        }
         return matrix[i][j];
     }
 
+    /**
+     * Computest the transformation sequences using Floyd's algorithm.
+     */
     private void computeTransformationSequences() {
         vertices = new ArrayList<String>();
 
         // Obtain possible starting points.
         Set<String> v = new HashSet<String>();
-        for (Transformation transformation : transformations.values()) {
+        for (Id<Transformation> id : transformations.ids()) {
+            Transformation transformation = transformations.get(id);
             v.add(transformation.getFromType());
             v.add(transformation.getToType());
         }
@@ -95,7 +164,8 @@ public class Transformations {
         for (int i = 0; i < nvertices; i++) {
             matrix[i][i] = new TransformationPath();
         }
-        for (Transformation transformation : transformations.values()) {
+        for (Id<Transformation> id : transformations.ids()) {
+            Transformation transformation = transformations.get(id);
             int from = vertices.indexOf(transformation.getFromType());
             int to = vertices.indexOf(transformation.getToType());
             TransformationPath path = new TransformationPath(transformation);
@@ -126,15 +196,17 @@ public class Transformations {
             .size();
     }
 
+    /**
+     * Removes a transformation.
+     * 
+     * @param aId
+     *            Id of the transformation.
+     */
     public void removeTransformation(Id<Transformation> aId) {
-        transformations.remove(aId.getId());
+        transformations.remove(aId);
         computeTransformationSequences();
     }
 
-    public Collection<Transformation> getTransformations() {
-        return Collections.unmodifiableCollection(transformations.values());
-    }
-
     @Override
     public String toString() {
         StringBuffer buf = new StringBuffer();
@@ -156,4 +228,4 @@ public class Transformations {
         buf.append(")");
         return buf.toString();
     }
-}
\ No newline at end of file
+}