X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=impl%2Fsrc%2Fmain%2Fjava%2Forg%2Fwamblee%2Fxmlrouter%2Fimpl%2FTransformations.java;h=c297fac787ce5ea66ca66339435fdd85bc59a8c7;hb=b2375f35a2f897e1417e8b5ec5b19b3257a11586;hp=7fd3e10e7041efd6173dcc35832b94e67d0cbc46;hpb=5b4ee8d862b98d127abb04c646017c184d005838;p=xmlrouter diff --git a/impl/src/main/java/org/wamblee/xmlrouter/impl/Transformations.java b/impl/src/main/java/org/wamblee/xmlrouter/impl/Transformations.java index 7fd3e10..c297fac 100644 --- a/impl/src/main/java/org/wamblee/xmlrouter/impl/Transformations.java +++ b/impl/src/main/java/org/wamblee/xmlrouter/impl/Transformations.java @@ -17,41 +17,89 @@ 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.AtomicLong; 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 AtomicLong sequenceNumber; - private Map, Transformation> transformations; + private Config transformations; private List vertices; private TransformationPath[][] matrix; private Map> sequences; + /** + * Construct the transformations. + */ public Transformations() { - sequenceNumber = new AtomicLong(1); - transformations = new LinkedHashMap, Transformation>(); + transformations = new ConfigImpl() { + @Override + public Transformation wrap(Id aId, + Transformation aType) { + return new RobustTransformation(aId, aType); + } + }; vertices = new ArrayList(); matrix = new TransformationPath[0][0]; } + public Config getTransformationConfig() { + return new Config() { + @Override + public Id add(Transformation aT) { + return addTransformation(aT); + } + + @Override + public Transformation get(Id aId) { + return transformations.get(aId); + } + + @Override + public Collection> ids() { + return transformations.ids(); + } + + @Override + public boolean remove(Id 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 addTransformation(Transformation aTransformation) { - long seqno = sequenceNumber.getAndIncrement(); - Id id = new Id(seqno); - transformations.put(id, new RobustTransformation(id, aTransformation)); + Id 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 getPossibleTargetTypes(String aType) { int index = vertices.indexOf(aType); Set res = new HashSet(); @@ -93,12 +141,16 @@ public class Transformations { return matrix[i][j]; } + /** + * Computest the transformation sequences using Floyd's algorithm. + */ private void computeTransformationSequences() { vertices = new ArrayList(); // Obtain possible starting points. Set v = new HashSet(); - for (Transformation transformation : transformations.values()) { + for (Id id : transformations.ids()) { + Transformation transformation = transformations.get(id); v.add(transformation.getFromType()); v.add(transformation.getToType()); } @@ -112,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 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); @@ -143,19 +196,17 @@ public class Transformations { .size(); } + /** + * Removes a transformation. + * + * @param aId + * Id of the transformation. + */ public void removeTransformation(Id aId) { transformations.remove(aId); computeTransformationSequences(); } - public Collection> getTransformations() { - return Collections.unmodifiableCollection(transformations.keySet()); - } - - public Transformation getTransformation(Id aId) { - return transformations.get(aId); - } - @Override public String toString() { StringBuffer buf = new StringBuffer();