Clover coverage report - Convert - proto0
Coverage timestamp: Mo Nov 22 2004 13:19:16 CET
file stats: LOC: 126   Methods: 10
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
ShortestPathSearch.java 80% 79,1% 70% 78,1%
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.type.Type;
 19    import org.rblasch.convert.graph.SparseWeightedDirectedGraph;
 20    import org.rblasch.convert.graph.WeightedDirectedGraph;
 21    import org.rblasch.convert.converters.OnceConverterFactory;
 22   
 23    import java.util.Set;
 24    import java.util.HashSet;
 25    import java.util.Iterator;
 26    import java.util.List;
 27   
 28    /**
 29    * @author Ronald Blaschke
 30    */
 31    public class ShortestPathSearch implements ConverterLookup {
 32    private final Set/*<MetaConverter>*/ converters = new HashSet();
 33    private final Set/*<ConverterFactory>*/ factories = new HashSet();
 34    private final ShortestPathLookup lookup = new ShortestPathLookup(new SparseWeightedDirectedGraph());
 35   
 36  0 private static WeightedDirectedGraph convertersToGraph(final Set/*<MetaConverter>*/ cs) {
 37  0 final WeightedDirectedGraph g = new SparseWeightedDirectedGraph();
 38  0 for (final Iterator i = cs.iterator(); i.hasNext();) {
 39  0 final MetaConverter mc = (MetaConverter) i.next();
 40  0 g.addConnection(new TypeVertex(mc.getSourceType()),
 41    new ConverterEdge(mc, mc.getWeight()),
 42    new TypeVertex(mc.getDestinationType()));
 43    }
 44  0 return g;
 45    }
 46   
 47  0 private static void runFactories(final ShortestPathLookup lookup, final List/*<ConverterFactory>*/ factories, final Type sourceType, final Type destinationType) {
 48  0 for (final Iterator i = factories.iterator(); i.hasNext();) {
 49  0 final ConverterFactory f = (ConverterFactory) i.next();
 50  0 lookup.addGraph(convertersToGraph(f.create(sourceType, destinationType)));
 51    }
 52    }
 53   
 54  14 private static void runFactoriesOnAllKnownTypes(final ShortestPathLookup lookup, final Set/*<ConverterFactory>*/ factories) {
 55  14 final WeightedDirectedGraph g = new SparseWeightedDirectedGraph();
 56  14 final Set/*<Type>*/ allKnownTypes = lookup.getAllKnownTypes();
 57  14 for (final Iterator i = allKnownTypes.iterator(); i.hasNext();) {
 58  292 final Type sourceType = (Type) i.next();
 59  292 for (final Iterator j = allKnownTypes.iterator(); j.hasNext();) {
 60  7696 final Type destinationType = (Type) j.next();
 61  7696 for (final Iterator k = factories.iterator(); k.hasNext();) {
 62  29368 final ConverterFactory factory = (ConverterFactory) k.next();
 63  29368 for (final Iterator l = factory.create(sourceType, destinationType).iterator(); l.hasNext();) {
 64  1650 final MetaConverter mc = (MetaConverter) l.next();
 65  1650 g.addConnection(new TypeVertex(mc.getSourceType()),
 66    new ConverterEdge(mc, mc.getWeight()),
 67    new TypeVertex(mc.getDestinationType()));
 68    }
 69    //Graphs.mergeInto(g, convertersToGraph());
 70    }
 71    }
 72    }
 73  14 lookup.addGraph(g);
 74    }
 75   
 76  0 String dumpGraph() {
 77  0 return lookup.dumpGraph();
 78    }
 79   
 80  14 public void addConverter(final MetaConverter converter) {
 81  14 converters.add(converter);
 82  14 lookup.addConverter(converter);
 83    }
 84   
 85  9 public void addAllConverters(final Set/*<MetaConverter>*/ converters) {
 86  9 for (final Iterator i = converters.iterator(); i.hasNext();) {
 87  10 addConverter((MetaConverter) i.next());
 88    }
 89    }
 90   
 91  8 public void addOnceFactory(final OnceConverterFactory once) {
 92  8 lookup.addAllConverters(once.createOnce());
 93    }
 94   
 95  21 public void addFactory(final ConverterFactory factory) {
 96  21 if (factory instanceof OnceConverterFactory) {
 97  8 addOnceFactory((OnceConverterFactory) factory);
 98    }
 99  21 factory.setLookup(lookup);
 100  21 factories.add(factory);
 101    }
 102   
 103  4 public void addAllFactories(final Set/*<ConverterFactory>*/ factories) {
 104  4 for (final Iterator i = factories.iterator(); i.hasNext();) {
 105  16 addFactory((ConverterFactory) i.next());
 106    }
 107    }
 108   
 109  18 public MetaConverter lookup(final Type sourceType, final Type destinationType) {
 110  18 lookup.addType(sourceType);
 111  18 lookup.addType(destinationType);
 112   
 113  18 int numberOfTypes;
 114  18 NoSuchConversionException pendingEx;
 115  18 do {
 116  31 numberOfTypes = lookup.getNumberOfKnownTypes();
 117  31 try {
 118  31 return lookup.lookup(sourceType, destinationType);
 119    } catch (final NoSuchConversionException ex) {
 120  14 pendingEx = ex;
 121  14 runFactoriesOnAllKnownTypes(lookup, factories);
 122    }
 123  14 } while (numberOfTypes != lookup.getNumberOfKnownTypes());
 124  1 throw pendingEx;
 125    }
 126    }