test
This commit is contained in:
@@ -1,67 +0,0 @@
|
||||
/*
|
||||
* Copyright 2004-2008 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.binding.format.registry;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.math.BigInteger;
|
||||
|
||||
import org.springframework.binding.convert.service.DefaultConversionService;
|
||||
import org.springframework.binding.format.FormatterRegistry;
|
||||
import org.springframework.binding.format.formatters.BooleanFormatter;
|
||||
import org.springframework.binding.format.formatters.DateFormatter;
|
||||
import org.springframework.binding.format.formatters.IntegerFormatter;
|
||||
import org.springframework.binding.format.formatters.NumberFormatter;
|
||||
|
||||
public class DefaultFormatterRegistry extends GenericFormatterRegistry {
|
||||
|
||||
/**
|
||||
* A singleton shared instance. Should never be modified.
|
||||
*/
|
||||
private static DefaultFormatterRegistry SHARED_INSTANCE;
|
||||
|
||||
/**
|
||||
* Creates a new formatter registry.
|
||||
*/
|
||||
public DefaultFormatterRegistry() {
|
||||
registerDefaultFormatters();
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers the default formatters. Subclasses may override.
|
||||
*/
|
||||
protected void registerDefaultFormatters() {
|
||||
registerFormatter(new IntegerFormatter(Integer.class));
|
||||
registerFormatter(new IntegerFormatter(BigInteger.class));
|
||||
registerFormatter(new IntegerFormatter(Long.class));
|
||||
registerFormatter(new IntegerFormatter(Short.class));
|
||||
registerFormatter(new NumberFormatter(Float.class));
|
||||
registerFormatter(new NumberFormatter(Double.class));
|
||||
registerFormatter(new NumberFormatter(BigDecimal.class));
|
||||
registerFormatter(new NumberFormatter(Byte.class));
|
||||
registerFormatter(new BooleanFormatter());
|
||||
registerFormatter(new DateFormatter());
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the shared {@link DefaultConversionService} instance.
|
||||
*/
|
||||
public synchronized static FormatterRegistry getSharedInstance() {
|
||||
if (SHARED_INSTANCE == null) {
|
||||
SHARED_INSTANCE = new DefaultFormatterRegistry();
|
||||
}
|
||||
return SHARED_INSTANCE;
|
||||
}
|
||||
}
|
||||
@@ -1,97 +0,0 @@
|
||||
/*
|
||||
* Copyright 2004-2008 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.binding.format.registry;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.springframework.binding.format.Formatter;
|
||||
import org.springframework.binding.format.FormatterRegistry;
|
||||
import org.springframework.util.Assert;
|
||||
|
||||
/**
|
||||
* A general-purpose {@link FormatterRegistry} implementation allows formatters to be registered programatically.
|
||||
*
|
||||
* @see #registerFormatter(Formatter)
|
||||
* @see #registerFormatter(String, Formatter)
|
||||
*
|
||||
* @author Keith Donald
|
||||
*/
|
||||
public class GenericFormatterRegistry implements FormatterRegistry {
|
||||
|
||||
private Map formattersById = new HashMap();
|
||||
|
||||
private Map formattersByClass = new HashMap();
|
||||
|
||||
public Formatter getFormatter(Class clazz) {
|
||||
Assert.notNull(clazz, "The formatted class argument is required");
|
||||
clazz = convertToWrapperClassIfNecessary(clazz);
|
||||
return (Formatter) formattersByClass.get(clazz);
|
||||
}
|
||||
|
||||
public Formatter getFormatter(Class clazz, String id) {
|
||||
Assert.notNull(clazz, "The formatted class argument is required");
|
||||
Assert.hasText(id, "The id of the custom formatter is required");
|
||||
Formatter formatter = (Formatter) formattersById.get(id);
|
||||
if (formatter != null && !formatter.getObjectType().equals(clazz)) {
|
||||
throw new IllegalArgumentException(
|
||||
"Provided class argument does not match registered Formatter objectType [" + clazz.getName()
|
||||
+ "]; unable to return custom Formatter instance " + formatter);
|
||||
}
|
||||
return formatter;
|
||||
}
|
||||
|
||||
// impl
|
||||
|
||||
public void registerFormatter(Formatter formatter) {
|
||||
Assert.notNull(formatter, "The formatter to register is required");
|
||||
formattersByClass.put(formatter.getObjectType(), formatter);
|
||||
}
|
||||
|
||||
public void registerFormatter(String id, Formatter formatter) {
|
||||
Assert.hasText(id, "The id of the custom formatter is required");
|
||||
Assert.notNull(formatter, "The formatter to register is required");
|
||||
formattersById.put(id, formatter);
|
||||
}
|
||||
|
||||
// helpers
|
||||
|
||||
private Class convertToWrapperClassIfNecessary(Class targetType) {
|
||||
if (targetType.isPrimitive()) {
|
||||
if (targetType.equals(int.class)) {
|
||||
return Integer.class;
|
||||
} else if (targetType.equals(short.class)) {
|
||||
return Short.class;
|
||||
} else if (targetType.equals(long.class)) {
|
||||
return Long.class;
|
||||
} else if (targetType.equals(float.class)) {
|
||||
return Float.class;
|
||||
} else if (targetType.equals(double.class)) {
|
||||
return Double.class;
|
||||
} else if (targetType.equals(byte.class)) {
|
||||
return Byte.class;
|
||||
} else if (targetType.equals(boolean.class)) {
|
||||
return Boolean.class;
|
||||
} else if (targetType.equals(char.class)) {
|
||||
return Character.class;
|
||||
} else {
|
||||
throw new IllegalStateException("Should never happen - primitive type is not a primitive?");
|
||||
}
|
||||
} else {
|
||||
return targetType;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
<html>
|
||||
<body>
|
||||
<p>
|
||||
A system for registering Formatters for lookup by UI artifacts.
|
||||
</p>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user