basictypeconverter.java

来自「JXPath」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
        }
        else if (object instanceof NodeSet) {
            return convert(((NodeSet) object).getValues(), toType);
        }
        else if (object instanceof Pointer) {
            return convert(((Pointer) object).getValue(), toType);
        }
        else if (toType == String.class) {
            return object.toString();
        }
        else if (object instanceof Boolean) {
            if (toType == boolean.class) {
                return object;
            }
            boolean value = ((Boolean) object).booleanValue();
            return allocateNumber(toType, value ? 1 : 0);
        }
        else if (object instanceof Number) {
            double value = ((Number) object).doubleValue();
            if (toType == boolean.class || toType == Boolean.class) {
                return value == 0.0 ? Boolean.FALSE : Boolean.TRUE;
            }
            if (toType.isPrimitive()
                || Number.class.isAssignableFrom(toType)) {
                return allocateNumber(toType, value);
            }
        }
        else if (object instanceof Character) {
            if (toType == char.class) {
                return object;
            }
        }
        else if (object instanceof String) {
            Object value = convertStringToPrimitive(object, toType);
            if (value != null) {
                return value;
            }
        }
        
        Converter converter = ConvertUtils.lookup(toType);
        if (converter != null) {
            return converter.convert(toType, object);
        }

        throw new RuntimeException(
            "Cannot convert " + object.getClass() + " to " + toType);
    }

    protected Object convertNullToPrimitive(Class toType) {
        if (toType == boolean.class) {
            return Boolean.FALSE;
        }
        if (toType == char.class) {
            return new Character('\0');
        }
        if (toType == byte.class) {
            return new Byte((byte) 0);
        }
        if (toType == short.class) {
            return new Short((short) 0);
        }
        if (toType == int.class) {
            return new Integer(0);
        }
        if (toType == long.class) {
            return new Long(0L);
        }
        if (toType == float.class) {
            return new Float(0.0f);
        }
        if (toType == double.class) {
            return new Double(0.0);
        }
        return null;
    }

    protected Object convertStringToPrimitive(Object object, Class toType) {
        if (toType == boolean.class || toType == Boolean.class) {
            return Boolean.valueOf((String) object);
        }
        if (toType == char.class || toType == Character.class) {
            return new Character(((String) object).charAt(0));
        }
        if (toType == byte.class || toType == Byte.class) {
            return new Byte((String) object);
        }
        if (toType == short.class || toType == Short.class) {
            return new Short((String) object);
        }
        if (toType == int.class || toType == Integer.class) {
            return new Integer((String) object);
        }
        if (toType == long.class || toType == Long.class) {
            return new Long((String) object);
        }
        if (toType == float.class || toType == Float.class) {
            return new Float((String) object);
        }
        if (toType == double.class || toType == Double.class) {
            return new Double((String) object);
        }
        return null;
    }
    
    protected Number allocateNumber(Class type, double value) {
        if (type == Byte.class || type == byte.class) {
            return new Byte((byte) value);
        }
        if (type == Short.class || type == short.class) {
            return new Short((short) value);
        }
        if (type == Integer.class || type == int.class) {
            return new Integer((int) value);
        }
        if (type == Long.class || type == long.class) {
            return new Long((long) value);
        }
        if (type == Float.class || type == float.class) {
            return new Float((float) value);
        }
        if (type == Double.class || type == double.class) {
            return new Double(value);
        }
        return null;
    }

    protected boolean canCreateCollection(Class type) {
        if (!type.isInterface()
            && ((type.getModifiers() & Modifier.ABSTRACT) == 0)) {
            return true;
        }

        if (type == List.class) {
            return true;
        }

        if (type == Set.class) {
            return true;
        }
        return false;
    }

    protected Collection allocateCollection(Class type) {
        if (!type.isInterface()
            && ((type.getModifiers() & Modifier.ABSTRACT) == 0)) {
            try {
                return (Collection) type.newInstance();
            }
            catch (Exception ex) {
                throw new JXPathException(
                    "Cannot create collection of type: " + type,
                    ex);
            }
        }

        if (type == List.class) {
            return new ArrayList();
        }
        if (type == Set.class) {
            return new HashSet();
        }
        throw new RuntimeException("Cannot create collection of type: " + type);
    }
    
    protected Collection unmodifiableCollection(Collection collection) {
        if (collection instanceof List) {
            return Collections.unmodifiableList((List) collection);
        }
        else if (collection instanceof Set) {
            return Collections.unmodifiableSet((Set) collection);
        }
        // Cannot wrap it into a proper unmodifiable collection, 
        // so we just return the original collection itself
        return collection;
    }
    
    static final class ValueNodeSet implements NodeSet {
        private List values;
        private List pointers;

        public ValueNodeSet(List values) {
           this.values = values;
        }
        
        public List getValues() {
            return Collections.unmodifiableList(values);
        }
        
        public List getNodes() {
            return Collections.unmodifiableList(values);
        }
        
        public List getPointers() {
            if (pointers == null) {
                pointers = new ArrayList();
                for (int i = 0; i < values.size(); i++) {
                    pointers.add(new ValuePointer(values.get(i)));
                }
                pointers = Collections.unmodifiableList(pointers);
            }
            return pointers;
        }
    }
    
    static final class ValuePointer implements Pointer {
        private Object bean;

        public ValuePointer(Object object) {
            this.bean = object;
        }
        
        public Object getValue() {
            return bean;
        }
        
        public Object getNode() {
            return bean;
        }
        
        public Object getRootNode() {
            return bean;
        }        
        
        public void setValue(Object value) {
            throw new UnsupportedOperationException();
        }
        
        public Object clone() {
            return this;
        }
        
        public int compareTo(Object object) {
            return 0;
        }
        
        public String asPath() {
            if (bean == null) {
                return "null()";
            }
            else if (bean instanceof Number) {
                String string = bean.toString();
                if (string.endsWith(".0")) {
                    string = string.substring(0, string.length() - 2);
                }
                return string;
            }
            else if (bean instanceof Boolean) {
                return ((Boolean) bean).booleanValue() ? "true()" : "false()";
            }
            else if (bean instanceof String) {
                return "'" + bean + "'";
            }
            return "{object of type " + bean.getClass().getName() + "}";
        }
    }
}

⌨️ 快捷键说明

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