Package: AbstractType

AbstractType

nameinstructionbranchcomplexitylinemethod
AbstractType()
M: 3 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
equals(Object)
M: 16 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
hashCode()
M: 4 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 1 C: 0
0%
M: 1 C: 0
0%
isAssignableFrom(Class, Class)
M: 130 C: 0
0%
M: 56 C: 0
0%
M: 29 C: 0
0%
M: 26 C: 0
0%
M: 1 C: 0
0%
static {...}
M: 28 C: 0
0%
M: 0 C: 0
100%
M: 1 C: 0
0%
M: 11 C: 0
0%
M: 1 C: 0
0%
wrapPrimitive(Class)
M: 11 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2015 Obeo.
3: * All rights reserved. This program and the accompanying materials
4: * are made available under the terms of the Eclipse Public License v1.0
5: * which accompanies this distribution, and is available at
6: * http://www.eclipse.org/legal/epl-v10.html
7: *
8: * Contributors:
9: * Obeo - initial API and implementation
10: *******************************************************************************/
11: /*******************************************************************************
12: * Copyright (c) 2015 Obeo.
13: * All rights reserved. This program and the accompanying materials
14: * are made available under the terms of the Eclipse Public License v1.0
15: * which accompanies this distribution, and is available at
16: * http://www.eclipse.org/legal/epl-v10.html
17: *
18: * Contributors:
19: * Obeo - initial API and implementation
20: *******************************************************************************/
21: package org.eclipse.acceleo.query.validation.type;
22:
23: import com.google.common.collect.ImmutableMap;
24:
25: /**
26: * Abstract implementation of {@link IType}.
27: *
28: * @author <a href="mailto:romain.guider@obeo.fr">Romain Guider</a>
29: */
30: public abstract class AbstractType implements IType {
31:         /** Associates primitive java classes to their wrapper. */
32:         private static final ImmutableMap<Class<?>, Class<?>> PRIMITIVE_WRAPPERS;
33:
34:         static {
35:                 //@formatter:off
36:                 PRIMITIVE_WRAPPERS = ImmutableMap.<Class<?>, Class<?>> builder()
37:                                 .put(byte.class, Byte.class)
38:                                 .put(short.class, Short.class)
39:                                 .put(char.class, Character.class)
40:                                 .put(int.class, Integer.class)
41:                                 .put(long.class, Long.class)
42:                                 .put(float.class, Float.class)
43:                                 .put(double.class, Double.class)
44:                                 .put(boolean.class, Boolean.class)
45:                                 .build();
46:                 //@formatter:on
47:         }
48:
49:         /**
50:          * Returns the primitve wrapper for the given class if any, or the class itself if it isn't a primitive.
51:          *
52:          * @param source
53:          * The class to check for a primitive wrapper.
54:          * @return The primitve wrapper for the given class if any, or the class itself if it isn't a primitive.
55:          * @since 4.1
56:          */
57:         protected Class<?> wrapPrimitive(Class<?> source) {
58:                 Class<?> wrapper = PRIMITIVE_WRAPPERS.get(source);
59:•                if (wrapper != null) {
60:                         return wrapper;
61:                 }
62:                 return source;
63:         }
64:
65:         /**
66:          * AQL allows for widening conversions (int to long for example) but not narrowing ones (long to int) and
67:          * considers auto-boxing. This extends the basic java {@link Class#isAssignableFrom(Class)
68:          * "Class.isAssignableFrom"} with these rules.
69:          *
70:          * @param toType
71:          * The type into which we're trying to convert another.
72:          * @param fromType
73:          * The source type from which to convert.
74:          * @return <code>true</code> if <code>toType</code> can be assigned from <code>fromType</code> when
75:          * considering auto-boxing and primitive widening rules.
76:          * @since 4.1
77:          */
78:         protected boolean isAssignableFrom(Class<?> toType, Class<?> fromType) {
79:                 Class<?> wrappedToType = wrapPrimitive(toType);
80:                 Class<?> wrappedFromType = wrapPrimitive(fromType);
81:
82:                 boolean result = false;
83:•                if (toType == null || fromType == null) {
84:                         result = false;
85:•                } else if (wrappedToType.isAssignableFrom(wrappedFromType)) {
86:                         result = true;
87:                 } else {
88:                         // Widening conversions
89:                         // - byte to short, int, long, float, or double
90:                         // - short to int, long, float, or double
91:                         // - char to int, long, float, or double
92:                         // - int to long, float, or double
93:                         // - long to float or double
94:                         // - float to double
95:                         // CHECKSTYLE:OFF not much to do here since we're testing against all conversions
96:•                        if (wrappedFromType == Byte.class) {
97:•                                result = wrappedToType == Short.class || wrappedToType == Integer.class
98:•                                                || wrappedToType == Long.class || wrappedToType == Float.class
99:•                                                || wrappedToType == Double.class;
100:•                        } else if (wrappedFromType == Short.class) {
101:•                                result = wrappedToType == Integer.class || wrappedToType == Long.class
102:•                                                || wrappedToType == Float.class || wrappedToType == Double.class;
103:•                        } else if (wrappedFromType == Character.class) {
104:•                                result = wrappedToType == Integer.class || wrappedToType == Long.class
105:•                                                || wrappedToType == Float.class || wrappedToType == Double.class;
106:•                        } else if (wrappedFromType == Integer.class) {
107:•                                result = wrappedToType == Long.class || wrappedToType == Float.class
108:•                                                || wrappedToType == Double.class;
109:•                        } else if (wrappedFromType == Long.class) {
110:•                                result = wrappedToType == Float.class || wrappedToType == Double.class;
111:•                        } else if (wrappedFromType == Float.class) {
112:•                                result = wrappedToType == Double.class;
113:                         }
114:                 }
115:                 // CHECKSTYLE:ON
116:                 return result;
117:         }
118:
119:         /**
120:          * {@inheritDoc}
121:          *
122:          * @see java.lang.Object#hashCode()
123:          */
124:         @Override
125:         public int hashCode() {
126:                 return getType().hashCode();
127:         }
128:
129:         /**
130:          * {@inheritDoc}
131:          *
132:          * @see java.lang.Object#equals(java.lang.Object)
133:          */
134:         @Override
135:         public boolean equals(Object obj) {
136:•                return getClass() == obj.getClass() && ((AbstractType)obj).getType().equals(getType());
137:         }
138: }