7fd3e10e7041efd6173dcc35832b94e67d0cbc46
[xmlrouter] / impl / src / main / java / org / wamblee / xmlrouter / impl / Transformations.java
1 /*
2  * Copyright 2005-2011 the original author or authors.
3  * 
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  * 
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  * 
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.wamblee.xmlrouter.impl;
17
18 import java.util.ArrayList;
19 import java.util.Collection;
20 import java.util.Collections;
21 import java.util.HashSet;
22 import java.util.LinkedHashMap;
23 import java.util.List;
24 import java.util.Map;
25 import java.util.Set;
26 import java.util.concurrent.atomic.AtomicLong;
27
28 import org.wamblee.xmlrouter.common.Id;
29 import org.wamblee.xmlrouter.config.Transformation;
30
31 public class Transformations {
32
33     private AtomicLong sequenceNumber;
34     private Map<Id<Transformation>, Transformation> transformations;
35     private List<String> vertices;
36     private TransformationPath[][] matrix;
37
38     private Map<String, List<TransformationPath>> sequences;
39
40     public Transformations() {
41         sequenceNumber = new AtomicLong(1);
42         transformations = new LinkedHashMap<Id<Transformation>, Transformation>();
43         vertices = new ArrayList<String>();
44         matrix = new TransformationPath[0][0];
45     }
46
47     public Id<Transformation> addTransformation(Transformation aTransformation) {
48         long seqno = sequenceNumber.getAndIncrement();
49         Id<Transformation> id = new Id<Transformation>(seqno);
50         transformations.put(id, new RobustTransformation(id, aTransformation));
51         computeTransformationSequences();
52         return id;
53     }
54
55     public Collection<String> getPossibleTargetTypes(String aType) {
56         int index = vertices.indexOf(aType);
57         Set<String> res = new HashSet<String>();
58         for (int j = 0; j < vertices.size(); j++) {
59             if (matrix[index][j] != null) {
60                 String value = matrix[index][j].getToType();
61                 if (value == null) {
62                     value = aType;
63                 }
64                 res.add(value);
65             }
66         }
67         res.add(aType);
68         return res;
69     }
70
71     /**
72      * Gets the transformation path from A to B.
73      * 
74      * @param aFrom
75      *            From
76      * @param aTo
77      *            To
78      * @return Transformatkon path or null if not found.
79      */
80     public TransformationPath getPath(String aFrom, String aTo) {
81         int i = vertices.indexOf(aFrom);
82         if (i < 0) {
83             if (aFrom.equals(aTo)) {
84                 return new TransformationPath();
85             }
86             return null;
87         }
88
89         int j = vertices.indexOf(aTo);
90         if (j < 0) {
91             return null;
92         }
93         return matrix[i][j];
94     }
95
96     private void computeTransformationSequences() {
97         vertices = new ArrayList<String>();
98
99         // Obtain possible starting points.
100         Set<String> v = new HashSet<String>();
101         for (Transformation transformation : transformations.values()) {
102             v.add(transformation.getFromType());
103             v.add(transformation.getToType());
104         }
105
106         vertices.addAll(v);
107
108         matrix = new TransformationPath[vertices.size()][vertices.size()];
109
110         // Floyd's algorithm.
111         int nvertices = vertices.size();
112         for (int i = 0; i < nvertices; i++) {
113             matrix[i][i] = new TransformationPath();
114         }
115         for (Transformation transformation : transformations.values()) {
116             int from = vertices.indexOf(transformation.getFromType());
117             int to = vertices.indexOf(transformation.getToType());
118             TransformationPath path = new TransformationPath(transformation);
119             matrix[from][to] = path;
120         }
121
122         for (int k = 0; k < nvertices; k++) {
123             for (int i = 0; i < nvertices; i++) {
124                 for (int j = 0; j < nvertices; j++) {
125                     // if the path from i to j through k is shorter then the
126                     // existing path then
127                     // replace it.
128                     int lij = getPathLength(i, j);
129                     int lik = getPathLength(i, k);
130                     int lkj = getPathLength(k, j);
131                     if (lik + lkj < lij) {
132                         matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
133                     }
134                 }
135             }
136         }
137     }
138
139     private int getPathLength(int i, int j) {
140         // We use MAX_INT/3 as infinity. This ensures that the default integer
141         // comparison in Floyd's algorithm does not lead to overflow.
142         return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
143             .size();
144     }
145
146     public void removeTransformation(Id<Transformation> aId) {
147         transformations.remove(aId);
148         computeTransformationSequences();
149     }
150
151     public Collection<Id<Transformation>> getTransformations() {
152         return Collections.unmodifiableCollection(transformations.keySet());
153     }
154
155     public Transformation getTransformation(Id<Transformation> aId) {
156         return transformations.get(aId);
157     }
158
159     @Override
160     public String toString() {
161         StringBuffer buf = new StringBuffer();
162         buf.append("Transformations(");
163         int nvertices = vertices.size();
164         for (int i = 0; i < nvertices; i++) {
165             for (int j = 0; j < nvertices; j++) {
166                 TransformationPath path = matrix[i][j];
167                 if (path != null) {
168                     buf.append(vertices.get(i));
169                     buf.append(" -> ");
170                     buf.append(vertices.get(j));
171                     buf.append(": ");
172                     buf.append(path);
173                     buf.append("\n");
174                 }
175             }
176         }
177         buf.append(")");
178         return buf.toString();
179     }
180 }