Clover coverage report - Convert - proto0
Coverage timestamp: Mo Nov 22 2004 13:19:16 CET
file stats: LOC: 175   Methods: 18
NCLOC: 134   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
Types.java 89,3% 90,3% 88,9% 89,8%
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.type;
 17   
 18    import org.rblasch.convert.assertion.Assertion;
 19   
 20    import java.util.ArrayList;
 21    import java.util.Arrays;
 22    import java.util.Collections;
 23    import java.util.HashMap;
 24    import java.util.Iterator;
 25    import java.util.List;
 26    import java.util.Map;
 27   
 28    /**
 29    * @author Ronald Blaschke
 30    */
 31    public class Types {
 32    // cache for already resolved types
 33    private static Map/*<Object, Type>*/ types = new HashMap();
 34   
 35  49772 private static boolean isKnown(final Object key) {
 36  49772 return types.containsKey(key);
 37    }
 38   
 39  280 private static void makeKnown(final Object key, final Type t) {
 40  280 Type knownT = null;
 41  280 if (types.containsValue(t)) {
 42  50 for (final Iterator i = types.values().iterator(); i.hasNext();) {
 43  1780 final Type ti = (Type) i.next();
 44  1780 if (t.equals(ti)) {
 45  51 knownT = ti;
 46    }
 47    }
 48    } else {
 49  230 knownT = t;
 50    }
 51  280 Assertion.ensure(knownT != null, "Known type must not be null");
 52   
 53  280 types.put(key, knownT);
 54    }
 55   
 56  24746 private static Type knownType(final Object key) {
 57  24746 if (isKnown(key)) {
 58  24746 return (Type) types.get(key);
 59    } else {
 60  0 throw new UnknownTypeException(key + " is not a known type");
 61    }
 62    }
 63   
 64  2411 public static Type findTypeByName(final String name) throws UnknownTypeException {
 65  2411 return findType(name);
 66    }
 67   
 68  2078 public static Type primitiveArrayType() {
 69  2078 return ArrayType.getInstance();
 70    }
 71   
 72  8 public static Type nullType() {
 73  8 return NullType.getInstance();
 74    }
 75   
 76  0 public static Type findPrimitiveType(final Class c) {
 77  0 return new PrimitiveType(c);
 78    }
 79   
 80  14835 public static Type findTypeByClass(final Class c) {
 81  14835 return findType(c);
 82    }
 83   
 84  0 public static Type findPTypeByName(final String t, final String p1) throws UnknownTypeException {
 85  0 return findPType(t, Collections.singletonList(p1));
 86    }
 87   
 88  202 public static Type findPTypeByClass(final Class c, final Class p1) throws UnknownTypeException {
 89  202 return findPType(c, Collections.singletonList(p1));
 90    }
 91   
 92  4488 public static Type findPTypeByType(final Type t, final Type p1) throws UnknownTypeException {
 93  4488 return findPType(t, Collections.singletonList(p1));
 94    }
 95   
 96  2824 public static ParameterizableType findPTypeByType(final Type t, final List/*<Type>*/ parameters) throws UnknownTypeException {
 97  2824 return findPType(t, parameters);
 98    }
 99   
 100  7514 private static ParameterizableType findPType(final Object type, final List/*<Object>*/ parameters) throws UnknownTypeException {
 101  7514 final List/* <Object>*/ request = new ArrayList(2);
 102  7514 request.add(type);
 103  7514 request.addAll(parameters);
 104  7514 if (isKnown(request)) {
 105  7381 return (ParameterizableType) knownType(request);
 106    } else {
 107  133 final ParameterizableType t = findPTypeInternal(type, parameters);
 108  133 makeKnown(request, t);
 109  133 return t;
 110    }
 111    }
 112   
 113  17512 private static Type findType(final Object type) throws UnknownTypeException {
 114  17512 if (isKnown(type)) {
 115  17365 return knownType(type);
 116    } else {
 117  147 final Type t = findTypeInternal(type);
 118  147 makeKnown(type, t);
 119  147 return t;
 120    }
 121    }
 122   
 123  147 private static Type findTypeInternal(final Object t) throws UnknownTypeException {
 124  147 if (t instanceof Type) {
 125  42 return (Type) t;
 126  105 } else if (t instanceof String) {
 127  22 return findTypeInternal((String) t);
 128  83 } else if (t instanceof Class) {
 129  83 return findTypeInternal((Class) t);
 130    } else {
 131  0 throw new UnknownTypeException("Don't know how to resolve type " + t + " (" + t.getClass() + ")");
 132    }
 133    }
 134   
 135  22 private static Type findTypeInternal(final String name) throws UnknownTypeException {
 136  22 if ("any".equals(name)) {
 137  0 return AnyType.getInstance();
 138  22 } else if ("array".equals(name)) {
 139  3 return ArrayType.getInstance();
 140    } else {
 141  19 try {
 142  19 return PrimitiveType.valueOf(name);
 143    } catch (final UnknownTypeException ex) {
 144  6 try {
 145  6 return ClassType.valueOf(name);
 146    } catch (final UnknownTypeException ex2) {
 147  3 try {
 148  3 return ParameterizableType.valueOf(name);
 149    } catch (final UnknownTypeException ex3) {
 150  0 throw new UnknownTypeException(name + " is not a known type");
 151    }
 152    }
 153    }
 154    }
 155    }
 156   
 157  83 private static Type findTypeInternal(final Class c) throws UnknownTypeException {
 158  83 if (c.isPrimitive()) {
 159  4 return new PrimitiveType(c);
 160  79 } else if (c.isArray()) {
 161  5 return new ParameterizableType(ArrayType.getInstance(),
 162    Arrays.asList(new Type[]{findTypeByClass(c.getComponentType())}));
 163    } else {
 164  74 return new ClassType(c);
 165    }
 166    }
 167   
 168  133 private static ParameterizableType findPTypeInternal(final Object t, final List/*<Object>*/ parameters) throws UnknownTypeException {
 169  133 final List/*<Type>*/ l = new ArrayList(parameters.size());
 170  133 for (final Iterator i = parameters.iterator(); i.hasNext();) {
 171  133 l.add(findType(i.next()));
 172    }
 173  133 return new ParameterizableType(findType(t), l);
 174    }
 175    }