Skip to content

Commit

Permalink
Additional tests for DI
Browse files Browse the repository at this point in the history
  • Loading branch information
rchodava committed Jul 18, 2016
1 parent dc5db81 commit 947266a
Show file tree
Hide file tree
Showing 3 changed files with 246 additions and 131 deletions.
133 changes: 2 additions & 131 deletions core/src/main/java/foundation/stack/datamill/configuration/Wiring.java
Original file line number Diff line number Diff line change
@@ -1,27 +1,8 @@
package foundation.stack.datamill.configuration;

/*
* Portions of this code were copied from the Apache commons-lang project, notably the source of class
* org.apache.commons.lang3.ClassUtils. That code is under the following terms:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import com.google.common.collect.HashMultimap;
import com.google.common.collect.Multimap;
import foundation.stack.datamill.configuration.impl.Classes;
import foundation.stack.datamill.reflection.impl.TypeSwitch;
import foundation.stack.datamill.values.Value;
import rx.functions.Action1;
Expand Down Expand Up @@ -84,30 +65,6 @@
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class Wiring {
private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>();
static {
primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
primitiveWrapperMap.put(Byte.TYPE, Byte.class);
primitiveWrapperMap.put(Character.TYPE, Character.class);
primitiveWrapperMap.put(Short.TYPE, Short.class);
primitiveWrapperMap.put(Integer.TYPE, Integer.class);
primitiveWrapperMap.put(Long.TYPE, Long.class);
primitiveWrapperMap.put(Double.TYPE, Double.class);
primitiveWrapperMap.put(Float.TYPE, Float.class);
primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
}

private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<>();
static {
for (final Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperMap.entrySet()) {
final Class<?> primitiveClass = entry.getKey();
final Class<?> wrapperClass = entry.getValue();
if (!primitiveClass.equals(wrapperClass)) {
wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
}
}
}

private static final TypeSwitch<Value, Void, Object> valueCast = new TypeSwitch<Value, Void, Object>() {
@Override
protected Object caseBoolean(Value value, Void __) {
Expand Down Expand Up @@ -170,92 +127,6 @@ protected Object defaultCase(Value value, Void __) {
}
};

private static Class<?> primitiveToWrapper(final Class<?> clazz) {
Class<?> convertedClass = clazz;
if (clazz != null && clazz.isPrimitive()) {
convertedClass = primitiveWrapperMap.get(clazz);
}

return convertedClass;
}

private static Class<?> wrapperToPrimitive(final Class<?> clazz) {
return wrapperPrimitiveMap.get(clazz);
}

private static boolean isAssignable(Class<?> clazz, final Class<?> toClass) {
if (toClass == null) {
return false;
}

if (clazz == null) {
return !toClass.isPrimitive();
}

if (clazz.isPrimitive() && !toClass.isPrimitive()) {
clazz = primitiveToWrapper(clazz);
if (clazz == null) {
return false;
}
}
if (toClass.isPrimitive() && !clazz.isPrimitive()) {
clazz = wrapperToPrimitive(clazz);
if (clazz == null) {
return false;
}
}

if (clazz.equals(toClass)) {
return true;
}
if (clazz.isPrimitive()) {
if (toClass.isPrimitive() == false) {
return false;
}
if (Integer.TYPE.equals(clazz)) {
return Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Long.TYPE.equals(clazz)) {
return Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Boolean.TYPE.equals(clazz)) {
return false;
}
if (Double.TYPE.equals(clazz)) {
return false;
}
if (Float.TYPE.equals(clazz)) {
return Double.TYPE.equals(toClass);
}
if (Character.TYPE.equals(clazz)) {
return Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Short.TYPE.equals(clazz)) {
return Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Byte.TYPE.equals(clazz)) {
return Short.TYPE.equals(toClass)
|| Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
// should never get here
return false;
}

return toClass.isAssignableFrom(clazz);
}

private final Multimap<Class<?>, Object> members = HashMultimap.create();
private final Map<String, Object> named = new HashMap<>();

Expand Down Expand Up @@ -500,7 +371,7 @@ private Object getValueForNamedParameter(Parameter parameter, Named name) {

private Object castValueToTypeIfPossible(Value value, Class<?> type) {
Object castedValue = valueCast.doSwitch(type, value, null);
if (castedValue != null && isAssignable(type, castedValue.getClass())) {
if (castedValue != null && Classes.isAssignable(type, castedValue.getClass())) {
return castedValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package foundation.stack.datamill.configuration.impl;

/*
* Portions of this code were copied from the Apache commons-lang project, notably the source of class
* org.apache.commons.lang3.ClassUtils. The code is under the following terms:
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import java.util.HashMap;
import java.util.Map;

/**
* @author Ravi Chodavarapu (rchodava@gmail.com)
*/
public class Classes {
private static final Map<Class<?>, Class<?>> primitiveWrapperMap = new HashMap<>();

static {
primitiveWrapperMap.put(Boolean.TYPE, Boolean.class);
primitiveWrapperMap.put(Byte.TYPE, Byte.class);
primitiveWrapperMap.put(Character.TYPE, Character.class);
primitiveWrapperMap.put(Short.TYPE, Short.class);
primitiveWrapperMap.put(Integer.TYPE, Integer.class);
primitiveWrapperMap.put(Long.TYPE, Long.class);
primitiveWrapperMap.put(Double.TYPE, Double.class);
primitiveWrapperMap.put(Float.TYPE, Float.class);
primitiveWrapperMap.put(Void.TYPE, Void.TYPE);
}

private static final Map<Class<?>, Class<?>> wrapperPrimitiveMap = new HashMap<>();

static {
for (final Map.Entry<Class<?>, Class<?>> entry : primitiveWrapperMap.entrySet()) {
final Class<?> primitiveClass = entry.getKey();
final Class<?> wrapperClass = entry.getValue();
if (!primitiveClass.equals(wrapperClass)) {
wrapperPrimitiveMap.put(wrapperClass, primitiveClass);
}
}
}

private static Class<?> primitiveToWrapper(final Class<?> clazz) {
Class<?> convertedClass = clazz;
if (clazz != null && clazz.isPrimitive()) {
convertedClass = primitiveWrapperMap.get(clazz);
}

return convertedClass;
}

private static Class<?> wrapperToPrimitive(final Class<?> clazz) {
return wrapperPrimitiveMap.get(clazz);
}

public static boolean isAssignable(Class<?> clazz, final Class<?> toClass) {
if (toClass == null) {
return false;
}

if (clazz == null) {
return !toClass.isPrimitive();
}

if (clazz.isPrimitive() && !toClass.isPrimitive()) {
clazz = primitiveToWrapper(clazz);
if (clazz == null) {
return false;
}
}
if (toClass.isPrimitive() && !clazz.isPrimitive()) {
clazz = wrapperToPrimitive(clazz);
if (clazz == null) {
return false;
}
}

if (clazz.equals(toClass)) {
return true;
}
if (clazz.isPrimitive()) {
if (!toClass.isPrimitive()) {
return false;
}
if (Integer.TYPE.equals(clazz)) {
return Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Long.TYPE.equals(clazz)) {
return Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Boolean.TYPE.equals(clazz)) {
return false;
}
if (Double.TYPE.equals(clazz)) {
return false;
}
if (Float.TYPE.equals(clazz)) {
return Double.TYPE.equals(toClass);
}
if (Character.TYPE.equals(clazz)) {
return Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Short.TYPE.equals(clazz)) {
return Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
if (Byte.TYPE.equals(clazz)) {
return Short.TYPE.equals(toClass)
|| Integer.TYPE.equals(toClass)
|| Long.TYPE.equals(toClass)
|| Float.TYPE.equals(toClass)
|| Double.TYPE.equals(toClass);
}
// should never get here
return false;
}

return toClass.isAssignableFrom(clazz);
}
}

0 comments on commit 947266a

Please sign in to comment.