Clover coverage report - Convert - proto0
Coverage timestamp: Mo Nov 22 2004 13:19:16 CET
file stats: LOC: 134   Methods: 16
NCLOC: 93   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SptConverter.java 50% 63,9% 75% 66,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.converters.ObjectToMetaConvertersConverter;
 19    import org.rblasch.convert.converters.OnceConverterFactory;
 20    import org.rblasch.convert.converters.collections.ArrayConverterFactory;
 21    import org.rblasch.convert.converters.collections.ArrayToListConverterFactory;
 22    import org.rblasch.convert.converters.collections.ListConverterFactory;
 23    import org.rblasch.convert.converters.lang.JlConverterFactory;
 24    import org.rblasch.convert.converters.primitives.ByteToBooleanConverter;
 25    import org.rblasch.convert.converters.primitives.StringToBooleanConverter;
 26    import org.rblasch.convert.converters.primitives.StringToIntegerConverter;
 27    import org.rblasch.convert.type.Type;
 28    import org.rblasch.convert.type.Types;
 29   
 30    import java.util.Collections;
 31    import java.util.HashSet;
 32    import java.util.Iterator;
 33    import java.util.Set;
 34   
 35    /**
 36    * @author Ronald Blaschke
 37    * @api
 38    */
 39    public class SptConverter implements TargetedConverter {
 40    private final ShortestPathSearch search = new ShortestPathSearch();
 41   
 42  4 private static Set/*<MetaConverter>*/ defaultConverters() {
 43  4 final Set/*<MetaConverter>*/ cs = new HashSet();
 44    // currently disabled, until basic stuff is working, to avoid converter cluttering
 45  4 if (false) {
 46  0 cs.add(new ByteToBooleanConverter());
 47    //cs.add(new IntegerToByteConverter());
 48  0 cs.add(new StringToIntegerConverter());
 49  0 cs.add(new StringToBooleanConverter());
 50    }
 51  4 return cs;
 52    }
 53   
 54  4 private static Set/*<ConverterFactory>*/ defaultFactories() {
 55  4 final Set/*<ConverterFactory>*/ cf = new HashSet();
 56  4 cf.add(new JlConverterFactory());
 57  4 cf.add(new ArrayToListConverterFactory());
 58  4 cf.add(new ArrayConverterFactory());
 59  4 cf.add(new ListConverterFactory());
 60  4 return cf;
 61    }
 62   
 63  3 public static SptConverter createEmpty() {
 64  3 return new SptConverter();
 65    }
 66   
 67  3 public static SptConverter createJl() {
 68  3 final SptConverter c = new SptConverter();
 69  3 c.addFactory(new JlConverterFactory());
 70  3 return c;
 71    }
 72   
 73  4 public static SptConverter createDefault() {
 74  4 return new SptConverter(defaultConverters(), defaultFactories());
 75    }
 76   
 77  7 public SptConverter() {
 78    }
 79   
 80  5 public SptConverter(final Set/*<MetaConverter>*/ converters) {
 81  5 search.addAllConverters(converters);
 82    }
 83   
 84  4 public SptConverter(final Set/*<MetaConverter>*/ converters, final Set/*<ConverterFactory>*/ factories) {
 85  4 search.addAllConverters(converters);
 86  4 search.addAllFactories(factories);
 87    }
 88   
 89  4 public void addConverter(final MetaConverter converter) {
 90  4 search.addConverter(converter);
 91    }
 92   
 93  0 public void addDynamicConverter(final Object converter) {
 94  0 final SptConverter c = new SptConverter();
 95  0 c.addConverter(new ObjectToMetaConvertersConverter(100));
 96   
 97  0 final Set/*<MetaConverter>*/ converters = (Set) c.convert(converter, Types.findPTypeByClass(Set.class, MetaConverter.class));
 98  0 for (final Iterator i = converters.iterator(); i.hasNext();) {
 99  0 final MetaConverter mc = (MetaConverter) i.next();
 100  0 search.addConverter(mc);
 101    }
 102    }
 103   
 104  5 public void addFactory(final ConverterFactory factory) {
 105  5 search.addFactory(factory);
 106    }
 107   
 108  0 public void addOnceFactory(final OnceConverterFactory once) {
 109  0 search.addOnceFactory(once);
 110    }
 111   
 112  0 public Set/*<MetaConverter>*/ removeAllConverters(final Type sourceType, final Type destinationType) {
 113  0 throw new UnsupportedOperationException("removeAllConverters not yet implemented");
 114    }
 115   
 116  16 public Object convert(final Object value, final Type destinationType) throws ConversionException {
 117  16 return convert(value, value == null ? Types.nullType() : Types.findTypeByClass(value.getClass()), destinationType);
 118    }
 119   
 120  18 public Object convert(final Object value, final Type sourceType, final Type destinationType) throws ConversionException {
 121  18 try {
 122  18 return search.lookup(sourceType, destinationType).getConverter()
 123    .convert(value);
 124    } catch (final RuntimeException ex) {
 125  1 throw ex; // propagate runtime exceptions
 126    } catch (final Exception ex) {
 127  0 throw new ConversionFailedException(ex);
 128    }
 129    }
 130   
 131  0 String dumpGraph() {
 132  0 return search.dumpGraph();
 133    }
 134    }