Browse Source
All simple type discovery is now done through a SimpletypeHolder instance that carries a few default types considered to be simple but is configurable to carry additional ones. The mapping subsystem uses an instance of that class to do simple type decisions and thus allows customizing what is to be considered a simple type by simply dependency injecting a value object.pull/2/merge
7 changed files with 229 additions and 112 deletions
@ -1,90 +0,0 @@
@@ -1,90 +0,0 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* Licensed 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. |
||||
*/ |
||||
|
||||
package org.springframework.data.mapping; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Date; |
||||
import java.util.Locale; |
||||
import java.util.Set; |
||||
import java.util.concurrent.ConcurrentHashMap; |
||||
|
||||
/** |
||||
* Helper class to set and retrieve bean values. |
||||
* |
||||
* @author Jon Brisbin <jbrisbin@vmware.com> |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public abstract class MappingBeanHelper { |
||||
|
||||
private static final Set<Class<?>> simpleTypes = Collections.newSetFromMap(new ConcurrentHashMap<Class<?>, Boolean>()); |
||||
|
||||
static { |
||||
simpleTypes.add(boolean.class); |
||||
simpleTypes.add(boolean[].class); |
||||
simpleTypes.add(long.class); |
||||
simpleTypes.add(long[].class); |
||||
simpleTypes.add(short.class); |
||||
simpleTypes.add(short[].class); |
||||
simpleTypes.add(int.class); |
||||
simpleTypes.add(int[].class); |
||||
simpleTypes.add(byte.class); |
||||
simpleTypes.add(byte[].class); |
||||
simpleTypes.add(float.class); |
||||
simpleTypes.add(float[].class); |
||||
simpleTypes.add(double.class); |
||||
simpleTypes.add(double[].class); |
||||
simpleTypes.add(char.class); |
||||
simpleTypes.add(char[].class); |
||||
simpleTypes.add(Boolean.class); |
||||
simpleTypes.add(Long.class); |
||||
simpleTypes.add(Short.class); |
||||
simpleTypes.add(Integer.class); |
||||
simpleTypes.add(Byte.class); |
||||
simpleTypes.add(Float.class); |
||||
simpleTypes.add(Double.class); |
||||
simpleTypes.add(Character.class); |
||||
simpleTypes.add(String.class); |
||||
simpleTypes.add(Date.class); |
||||
simpleTypes.add(Locale.class); |
||||
simpleTypes.add(Class.class); |
||||
simpleTypes.add(Number.class); |
||||
} |
||||
|
||||
/** |
||||
* Returns the set of types considered to be simple. |
||||
* |
||||
* @return |
||||
*/ |
||||
public static Set<Class<?>> getSimpleTypes() { |
||||
return simpleTypes; |
||||
} |
||||
|
||||
/** |
||||
* Returns whether the given type is considered a simple one. |
||||
* |
||||
* @param type |
||||
* @return |
||||
*/ |
||||
public static boolean isSimpleType(Class<?> type) { |
||||
for (Class<?> clazz : simpleTypes) { |
||||
if (type == clazz || clazz.isAssignableFrom(type)) { |
||||
return true; |
||||
} |
||||
} |
||||
return type.isEnum(); |
||||
} |
||||
} |
||||
@ -0,0 +1,125 @@
@@ -0,0 +1,125 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* Licensed 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. |
||||
*/ |
||||
package org.springframework.data.mapping; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.Date; |
||||
import java.util.HashSet; |
||||
import java.util.Locale; |
||||
import java.util.Set; |
||||
|
||||
import org.springframework.util.Assert; |
||||
|
||||
/** |
||||
* Simple container to hold a set of types to be considered simple types. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class SimpleTypeHolder { |
||||
|
||||
private static final Set<Class<?>> DEFAULTS = new HashSet<Class<?>>(); |
||||
|
||||
static { |
||||
DEFAULTS.add(boolean.class); |
||||
DEFAULTS.add(boolean[].class); |
||||
DEFAULTS.add(long.class); |
||||
DEFAULTS.add(long[].class); |
||||
DEFAULTS.add(short.class); |
||||
DEFAULTS.add(short[].class); |
||||
DEFAULTS.add(int.class); |
||||
DEFAULTS.add(int[].class); |
||||
DEFAULTS.add(byte.class); |
||||
DEFAULTS.add(byte[].class); |
||||
DEFAULTS.add(float.class); |
||||
DEFAULTS.add(float[].class); |
||||
DEFAULTS.add(double.class); |
||||
DEFAULTS.add(double[].class); |
||||
DEFAULTS.add(char.class); |
||||
DEFAULTS.add(char[].class); |
||||
DEFAULTS.add(Boolean.class); |
||||
DEFAULTS.add(Long.class); |
||||
DEFAULTS.add(Short.class); |
||||
DEFAULTS.add(Integer.class); |
||||
DEFAULTS.add(Byte.class); |
||||
DEFAULTS.add(Float.class); |
||||
DEFAULTS.add(Double.class); |
||||
DEFAULTS.add(Character.class); |
||||
DEFAULTS.add(String.class); |
||||
DEFAULTS.add(Date.class); |
||||
DEFAULTS.add(Locale.class); |
||||
DEFAULTS.add(Class.class); |
||||
DEFAULTS.add(Number.class); |
||||
} |
||||
|
||||
private final Set<Class<?>> simpleTypes; |
||||
|
||||
/** |
||||
* Creates a new {@link SimpleTypeHolder} containing the default types. |
||||
* |
||||
* @see #SimpleTypeHolder(Set, boolean) |
||||
*/ |
||||
@SuppressWarnings("unchecked") |
||||
public SimpleTypeHolder() { |
||||
this(Collections.EMPTY_SET, true); |
||||
} |
||||
|
||||
/** |
||||
* Creates a new {@link SimpleTypeHolder} to carry the given custom simple types. Registration of default simple types |
||||
* can be deactivated by passing {@literal false} for {@code registerDefaults}. |
||||
* |
||||
* @param customSimpleTypes |
||||
* @param registerDefaults |
||||
*/ |
||||
public SimpleTypeHolder(Set<? extends Class<?>> customSimpleTypes, boolean registerDefaults) { |
||||
|
||||
Assert.notNull(customSimpleTypes); |
||||
this.simpleTypes = new HashSet<Class<?>>(customSimpleTypes); |
||||
|
||||
if (registerDefaults) { |
||||
this.simpleTypes.addAll(DEFAULTS); |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* Copy constructor to create a new {@link SimpleTypeHolder} that carries the given additional custom simple types. |
||||
* |
||||
* @param customSimpleTypes must not be {@literal null} |
||||
* @param source must not be {@literal null} |
||||
*/ |
||||
public SimpleTypeHolder(Set<? extends Class<?>> customSimpleTypes, SimpleTypeHolder source) { |
||||
|
||||
Assert.notNull(customSimpleTypes); |
||||
Assert.notNull(source); |
||||
|
||||
this.simpleTypes = new HashSet<Class<?>>(customSimpleTypes); |
||||
this.simpleTypes.addAll(source.simpleTypes); |
||||
} |
||||
|
||||
/** |
||||
* Returns whether the given type is considered a simple one. |
||||
* |
||||
* @param type |
||||
* @return |
||||
*/ |
||||
public boolean isSimpleType(Class<?> type) { |
||||
for (Class<?> clazz : simpleTypes) { |
||||
if (type == clazz || clazz.isAssignableFrom(type)) { |
||||
return true; |
||||
} |
||||
} |
||||
return type.isEnum(); |
||||
} |
||||
} |
||||
@ -0,0 +1,79 @@
@@ -0,0 +1,79 @@
|
||||
/* |
||||
* Copyright (c) 2011 by the original author(s). |
||||
* |
||||
* Licensed 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. |
||||
*/ |
||||
package org.springframework.data.mapping; |
||||
|
||||
import static org.hamcrest.CoreMatchers.*; |
||||
import static org.junit.Assert.*; |
||||
|
||||
import java.util.Collections; |
||||
import java.util.HashSet; |
||||
|
||||
import org.junit.Test; |
||||
|
||||
/** |
||||
* Unit tests for {@link SimpleTypeHolder}. |
||||
* |
||||
* @author Oliver Gierke |
||||
*/ |
||||
public class SimpleTypeHolderUnitTests { |
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void rejectsNullCustomTypes() { |
||||
new SimpleTypeHolder(null, false); |
||||
} |
||||
|
||||
@Test(expected = IllegalArgumentException.class) |
||||
public void rejectsNullOriginal() { |
||||
new SimpleTypeHolder(new HashSet<Class<?>>(), null); |
||||
} |
||||
|
||||
@Test |
||||
public void addsDefaultTypes() { |
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(); |
||||
|
||||
assertThat(holder.isSimpleType(String.class), is(true)); |
||||
} |
||||
|
||||
@Test |
||||
public void doesNotAddDefaultConvertersIfConfigured() { |
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(new HashSet<Class<?>>(), false); |
||||
|
||||
assertThat(holder.isSimpleType(String.class), is(false)); |
||||
} |
||||
|
||||
@Test |
||||
public void addsCustomTypesToSimpleOnes() { |
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true); |
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true)); |
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false)); |
||||
} |
||||
|
||||
@Test |
||||
public void createsHolderFromAnotherOneCorrectly() { |
||||
|
||||
SimpleTypeHolder holder = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolder.class), true); |
||||
SimpleTypeHolder second = new SimpleTypeHolder(Collections.singleton(SimpleTypeHolderUnitTests.class), holder); |
||||
|
||||
assertThat(holder.isSimpleType(SimpleTypeHolder.class), is(true)); |
||||
assertThat(holder.isSimpleType(SimpleTypeHolderUnitTests.class), is(false)); |
||||
assertThat(second.isSimpleType(SimpleTypeHolder.class), is(true)); |
||||
assertThat(second.isSimpleType(SimpleTypeHolderUnitTests.class), is(true)); |
||||
} |
||||
} |
||||
Loading…
Reference in new issue