20455e8a5e2b8633d4d783a5e1e26fed46e2c602
[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<Long, 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<Long, 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(seqno,
51             new RobustTransformation(id, aTransformation));
52         computeTransformationSequences();
53         return id;
54     }
55
56     public Collection<String> getPossibleTargetTypes(String aType) {
57         int index = vertices.indexOf(aType);
58         Set<String> res = new HashSet<String>();
59         for (int j = 0; j < vertices.size(); j++) {
60             if (matrix[index][j] != null) {
61                 String value = matrix[index][j].getToType();
62                 if (value == null) {
63                     value = aType;
64                 }
65                 res.add(value);
66             }
67         }
68         res.add(aType);
69         return res;
70     }
71
72     /**
73      * Gets the transformation path from A to B.
74      * 
75      * @param aFrom
76      *            From
77      * @param aTo
78      *            To
79      * @return Transformatkon path or null if not found.
80      */
81     public TransformationPath getPath(String aFrom, String aTo) {
82         int i = vertices.indexOf(aFrom);
83         if (i < 0) {
84             if (aFrom.equals(aTo)) {
85                 return new TransformationPath();
86             }
87             return null;
88         }
89
90         int j = vertices.indexOf(aTo);
91         if (j < 0) {
92             return null;
93         }
94         return matrix[i][j];
95     }
96
97     private void computeTransformationSequences() {
98         vertices = new ArrayList<String>();
99
100         // Obtain possible starting points.
101         Set<String> v = new HashSet<String>();
102         for (Transformation transformation : transformations.values()) {
103             v.add(transformation.getFromType());
104             v.add(transformation.getToType());
105         }
106
107         vertices.addAll(v);
108
109         matrix = new TransformationPath[vertices.size()][vertices.size()];
110
111         // Floyd's algorithm.
112         int nvertices = vertices.size();
113         for (int i = 0; i < nvertices; i++) {
114             matrix[i][i] = new TransformationPath();
115         }
116         for (Transformation transformation : transformations.values()) {
117             int from = vertices.indexOf(transformation.getFromType());
118             int to = vertices.indexOf(transformation.getToType());
119             TransformationPath path = new TransformationPath(transformation);
120             matrix[from][to] = path;
121         }
122
123         for (int k = 0; k < nvertices; k++) {
124             for (int i = 0; i < nvertices; i++) {
125                 for (int j = 0; j < nvertices; j++) {
126                     // if the path from i to j through k is shorter then the
127                     // existing path then
128                     // replace it.
129                     int lij = getPathLength(i, j);
130                     int lik = getPathLength(i, k);
131                     int lkj = getPathLength(k, j);
132                     if (lik + lkj < lij) {
133                         matrix[i][j] = matrix[i][k].appendPath(matrix[k][j]);
134                     }
135                 }
136             }
137         }
138     }
139
140     private int getPathLength(int i, int j) {
141         // We use MAX_INT/3 as infinity. This ensures that the default integer
142         // comparison in Floyd's algorithm does not lead to overflow.
143         return matrix[i][j] == null ? Integer.MAX_VALUE / 3 : matrix[i][j]
144             .size();
145     }
146
147     public void removeTransformation(Id<Transformation> aId) {
148         transformations.remove(aId.getId());
149         computeTransformationSequences();
150     }
151
152     public Collection<Transformation> getTransformations() {
153         return Collections.unmodifiableCollection(transformations.values());
154     }
155
156     @Override
157     public String toString() {
158         StringBuffer buf = new StringBuffer();
159         buf.append("Transformations(");
160         int nvertices = vertices.size();
161         for (int i = 0; i < nvertices; i++) {
162             for (int j = 0; j < nvertices; j++) {
163                 TransformationPath path = matrix[i][j];
164                 if (path != null) {
165                     buf.append(vertices.get(i));
166                     buf.append(" -> ");
167                     buf.append(vertices.get(j));
168                     buf.append(": ");
169                     buf.append(path);
170                     buf.append("\n");
171                 }
172             }
173         }
174         buf.append(")");
175         return buf.toString();
176     }
177 }