View Javadoc

1   /*
2    * Copyright 2004 Ronald Blaschke.
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.rblasch.convert;
17  
18  import org.rblasch.convert.converters.lang.CastConverter;
19  import org.rblasch.convert.converters.lang.IdentityConverter;
20  import org.rblasch.convert.converters.lang.ObjectToStringConverter;
21  import org.rblasch.convert.graph.Graph;
22  import org.rblasch.convert.graph.SparseWeightedDirectedGraph;
23  import org.rblasch.convert.graph.WeightedDirectedGraph;
24  import org.rblasch.convert.type.ClassType;
25  import org.rblasch.convert.type.ParameterizableType;
26  import org.rblasch.convert.type.Type;
27  
28  import java.util.Arrays;
29  import java.util.Iterator;
30  
31  /***
32   * @author Ronald Blaschke
33   * @deprecated no longer used
34   */
35  public class JlGraphFactory {
36      private static void addConnection(final Graph g, final MetaConverter mc) {
37          g.addConnection(new TypeVertex(mc.getSourceType()),
38                  new ConverterEdge(mc, mc.getWeight()),
39                  new TypeVertex(mc.getDestinationType()));
40      }
41  
42      public static WeightedDirectedGraph createGraphFor(final Type t) {
43          final WeightedDirectedGraph g = new SparseWeightedDirectedGraph();
44  
45          // identity
46          addConnection(g, new IdentityConverter(t));
47  
48          // class lookup
49          if (t instanceof ClassType) {
50              final ClassType ct = (ClassType) t;
51  
52              Class tmp = ct.getTheClass();
53              while (tmp.getSuperclass() != null) {
54                  addConnection(g, new CastConverter(tmp, tmp.getSuperclass()));
55  
56                  for (final Iterator i = Arrays.asList(tmp.getInterfaces()).iterator(); i.hasNext();) {
57                      final Class iface = (Class) i.next();
58                      addConnection(g, new CastConverter(tmp, iface));
59                  }
60  
61                  tmp = tmp.getSuperclass();
62              }
63          } else if (t instanceof ParameterizableType) {
64              //TODO: add parameter lookup
65          }
66  
67          // "default" .toString() conversion
68          addConnection(g, new ObjectToStringConverter());
69  
70          return g;
71      }
72  }