org.springframework.beans.propertyeditors.CustomMapEditor Maven / Gradle / Ivy
/*
* Copyright 2002-2017 the original author or authors.
*
* 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.beans.propertyeditors;
import java.beans.PropertyEditorSupport;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;
import org.springframework.util.ReflectionUtils;
/**
* Property editor for Maps, converting any source Map
* to a given target Map type.
*
* @author Juergen Hoeller
* @since 2.0.1
* @see java.util.Map
* @see java.util.SortedMap
*/
public class CustomMapEditor extends PropertyEditorSupport {
@SuppressWarnings("rawtypes")
private final Class mapType;
private final boolean nullAsEmptyMap;
/**
* Create a new CustomMapEditor for the given target type,
* keeping an incoming {@code null} as-is.
* @param mapType the target type, which needs to be a
* sub-interface of Map or a concrete Map class
* @see java.util.Map
* @see java.util.HashMap
* @see java.util.TreeMap
* @see java.util.LinkedHashMap
*/
@SuppressWarnings("rawtypes")
public CustomMapEditor(Class mapType) {
this(mapType, false);
}
/**
* Create a new CustomMapEditor for the given target type.
* If the incoming value is of the given type, it will be used as-is.
* If it is a different Map type or an array, it will be converted
* to a default implementation of the given Map type.
* If the value is anything else, a target Map with that single
* value will be created.
*
The default Map implementations are: TreeMap for SortedMap,
* and LinkedHashMap for Map.
* @param mapType the target type, which needs to be a
* sub-interface of Map or a concrete Map class
* @param nullAsEmptyMap ap whether to convert an incoming {@code null}
* value to an empty Map (of the appropriate type)
* @see java.util.Map
* @see java.util.TreeMap
* @see java.util.LinkedHashMap
*/
@SuppressWarnings("rawtypes")
public CustomMapEditor(Class mapType, boolean nullAsEmptyMap) {
Assert.notNull(mapType, "Map type is required");
if (!Map.class.isAssignableFrom(mapType)) {
throw new IllegalArgumentException(
"Map type [" + mapType.getName() + "] does not implement [java.util.Map]");
}
this.mapType = mapType;
this.nullAsEmptyMap = nullAsEmptyMap;
}
/**
* Convert the given text value to a Map with a single element.
*/
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue(text);
}
/**
* Convert the given value to a Map of the target type.
*/
@Override
public void setValue(@Nullable Object value) {
if (value == null && this.nullAsEmptyMap) {
super.setValue(createMap(this.mapType, 0));
}
else if (value == null || (this.mapType.isInstance(value) && !alwaysCreateNewMap())) {
// Use the source value as-is, as it matches the target type.
super.setValue(value);
}
else if (value instanceof Map) {
// Convert Map elements.
Map source = (Map) value;
Map