Clover coverage report - Convert - proto0
Coverage timestamp: Mo Nov 22 2004 13:19:16 CET
file stats: LOC: 111   Methods: 14
NCLOC: 77   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ShortestPathLookup.java 100% 85,2% 71,4% 83,7%
coverage coverage
 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.ChainedConverter;
 19    import org.rblasch.convert.dijkstra.ShortestPathEngine;
 20    import org.rblasch.convert.graph.Graphs;
 21    import org.rblasch.convert.graph.Path;
 22    import org.rblasch.convert.graph.WeightedDirectedGraph;
 23    import org.rblasch.convert.type.Type;
 24   
 25    import java.util.Collection;
 26    import java.util.HashSet;
 27    import java.util.Iterator;
 28    import java.util.LinkedList;
 29    import java.util.List;
 30    import java.util.Set;
 31   
 32    public class ShortestPathLookup implements ConverterLookup {
 33    private final WeightedDirectedGraph graph;
 34   
 35  16 public ShortestPathLookup(final WeightedDirectedGraph g) {
 36  16 this.graph = g;
 37    }
 38   
 39  14 public void addGraph(final WeightedDirectedGraph g) {
 40  14 Graphs.mergeInto(graph, g);
 41    }
 42   
 43  36 public void addType(final Type t) {
 44  36 graph.addVertex(new TypeVertex(t));
 45    }
 46   
 47  118 public void addConverter(final MetaConverter mc) {
 48  118 graph.addConnection(new TypeVertex(mc.getSourceType()),
 49    new ConverterEdge(mc, mc.getWeight()),
 50    new TypeVertex(mc.getDestinationType()));
 51    }
 52   
 53  8 public void addAllConverters(final Collection/*<MetaConverter>*/ mcs) {
 54  8 for (final Iterator i = mcs.iterator(); i.hasNext();) {
 55  104 addConverter((MetaConverter) i.next());
 56    }
 57    }
 58   
 59  45 public int getNumberOfKnownTypes() {
 60  45 return graph.getVertices().size();
 61    }
 62   
 63  0 public int getNumberOfKnownConversions() {
 64  0 return graph.getEdges().size();
 65    }
 66   
 67  14 public Set/*<Type>*/ getAllKnownTypes() {
 68  14 final Set/*<Type>*/ types = new HashSet();
 69  14 for (final Iterator i = graph.getVertices().iterator(); i.hasNext();) {
 70  292 final TypeVertex tv = (TypeVertex) i.next();
 71  292 types.add(tv.getType());
 72    }
 73  14 return types;
 74    }
 75   
 76  535 private List /*<MetaConverter>*/ getConvertersForPath(final Path path) {
 77  535 final List /*<MetaConverter>*/ converters = new LinkedList();
 78   
 79  535 for (final Iterator i = path.getEdges().iterator(); i.hasNext();) {
 80  1090 final MetaConverter c = ((ConverterEdge) i.next()).getConverter();
 81  1090 converters.add(c);
 82    }
 83   
 84  535 return converters;
 85    }
 86   
 87  655 private Path getShortestPath(final Type sourceType, final Type destinationType) throws NoSuchConversionException {
 88  655 final ShortestPathEngine spEngine = new ShortestPathEngine(graph);
 89  655 final Path shortestPath = spEngine.getShortestPath(new TypeVertex(sourceType), new TypeVertex(destinationType));
 90  655 if (shortestPath == null || shortestPath.getEdges().isEmpty()) {
 91  120 throw new NoSuchConversionException("No conversion from " + sourceType + " to " + destinationType + " in " + graph);
 92    }
 93  535 return shortestPath;
 94    }
 95   
 96  655 public MetaConverter lookup(final Type sourceType, final Type destinationType) throws NoSuchConversionException {
 97  655 return new ChainedConverter(getConvertersForPath(getShortestPath(sourceType, destinationType)));
 98    }
 99   
 100  0 public boolean equals(final Object obj) {
 101  0 return graph.equals(obj);
 102    }
 103   
 104  0 public int hashCode() {
 105  0 return graph.hashCode();
 106    }
 107   
 108  0 String dumpGraph() {
 109  0 return graph.toString();
 110    }
 111    }