defaultconverterlookup.java

来自「xstream是一个把java object序列化成xml文件的开源库,轻便好用」· Java 代码 · 共 56 行

JAVA
56
字号
package com.thoughtworks.xstream.core;import com.thoughtworks.xstream.alias.ClassMapper;import com.thoughtworks.xstream.converters.ConversionException;import com.thoughtworks.xstream.converters.Converter;import com.thoughtworks.xstream.converters.ConverterLookup;import com.thoughtworks.xstream.converters.basic.NullConverter;import com.thoughtworks.xstream.core.util.PrioritizedList;import com.thoughtworks.xstream.XStream;import java.util.Collections;import java.util.HashMap;import java.util.Iterator;import java.util.Map;public class DefaultConverterLookup implements ConverterLookup {    private final PrioritizedList converters = new PrioritizedList();    private final Converter nullConverter = new NullConverter();    private final Map typeToConverterMap = Collections.synchronizedMap(new HashMap());    private final ClassMapper classMapper;    public DefaultConverterLookup(ClassMapper classMapper) {        this.classMapper = classMapper;    }    /**     * @deprecated As of 1.1.1 you can register Converters with priorities, making the need for a default converter redundant.     */    public Converter defaultConverter() {        return (Converter) converters.firstOfLowestPriority();    }    public Converter lookupConverterForType(Class type) {        if (type == null) {            return nullConverter;        }        Converter cachedConverter = (Converter) typeToConverterMap.get(type);        if (cachedConverter != null) return cachedConverter;        Class mapType = classMapper.defaultImplementationOf(type);        Iterator iterator = converters.iterator();        while (iterator.hasNext()) {            Converter converter = (Converter) iterator.next();            if (converter.canConvert(mapType)) {                typeToConverterMap.put(type, converter);                return converter;            }        }        throw new ConversionException("No converter specified for " + type);    }    public void registerConverter(Converter converter, int priority) {        converters.add(converter, priority);    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?