
net.ymate.platform.commons.lang.BlurObject Maven / Gradle / Ivy
The newest version!
/*
* Copyright 2007-2019 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 net.ymate.platform.commons.lang;
import net.ymate.platform.commons.ReentrantLockHelper;
import net.ymate.platform.commons.annotation.Converter;
import net.ymate.platform.commons.util.ClassUtils;
import net.ymate.platform.commons.util.RuntimeUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.Serializable;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Blob;
import java.sql.Clob;
import java.sql.SQLException;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.*;
import java.util.concurrent.ConcurrentHashMap;
/**
* 模糊对象,任意数据类型间转换
*
* @author 刘镇 ([email protected]) on 2010-4-16 下午11:51:39
*/
@SuppressWarnings("rawtypes")
public class BlurObject implements Serializable {
private static final Log LOG = LogFactory.getLog(BlurObject.class);
private static final long serialVersionUID = 4141840934670622411L;
private static final Map, Map, IConverter>>> CONVERTERS = new ConcurrentHashMap<>();
static {
try {
ClassUtils.ExtensionLoader extensionLoader = ClassUtils.getExtensionLoader(IConverter.class, true);
for (Class converter : extensionLoader.getExtensionClasses()) {
Converter converterAnn = converter.getAnnotation(Converter.class);
if (converterAnn != null) {
IConverter converterInst = converter.newInstance();
for (Class> from : converterAnn.from()) {
if (!from.equals(converterAnn.to())) {
registerConverter(from, converterAnn.to(), converterInst);
}
}
}
}
} catch (Exception e) {
if (LOG.isWarnEnabled()) {
LOG.warn(StringUtils.EMPTY, RuntimeUtils.unwrapThrow(e));
}
}
}
/**
* 注册类型转换器
*
* @param fromClass 原类型
* @param toClass 目标类型
* @param converter 类型转换器实例
* @throws Exception 可能产生的任何异常
*/
public static void registerConverter(Class> fromClass, Class> toClass, IConverter> converter) throws Exception {
ReentrantLockHelper.putIfAbsentAsync(CONVERTERS, toClass, () -> new HashMap<>(16)).put(fromClass, converter);
}
@SuppressWarnings("unchecked")
public static T convertTo(Object attr, Class targetType) {
T object = null;
if (attr != null && !CONVERTERS.isEmpty()) {
Map, IConverter>> map = CONVERTERS.get(targetType);
if (map != null && !map.isEmpty()) {
IConverter> converter = map.get(attr.getClass());
if (converter != null) {
object = (T) converter.convert(attr);
}
}
}
if (object == null) {
try {
object = targetType.cast(attr);
} catch (ClassCastException ignored) {
}
}
return object;
}
/**
* 当前存储对象值
*/
private final Object attr;
public static BlurObject bind(Object o) {
return new BlurObject(o);
}
public BlurObject(Object o) {
attr = o;
}
/**
* @return 输出对象
*/
public Object toObjectValue() {
return attr;
}
/**
* @return 输出模糊对象
*/
public BlurObject toBlurObjectValue() {
if (attr instanceof BlurObject) {
return (BlurObject) attr;
}
return this;
}
/**
* @return 输出为映射
*/
public Map, ?> toMapValue() {
if (attr == null) {
return null;
}
if (attr instanceof Map) {
return (Map, ?>) attr;
}
return Collections.emptyMap();
}
/**
* @return 输出为列表
*/
public List> toListValue() {
if (attr == null) {
return null;
}
if (attr instanceof List) {
return (List>) attr;
}
if (attr instanceof Collection) {
return new ArrayList<>((Collection>) attr);
}
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy