⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 converters.java

📁 java实现浏览器等本地桌面的功能
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * $Id: Converters.java,v 1.2 2005/10/10 17:01:10 rbair Exp $ * * Copyright 2004 Sun Microsystems, Inc., 4150 Network Circle, * Santa Clara, California 95054, U.S.A. All rights reserved. * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. *  * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU * Lesser General Public License for more details. *  * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */package org.jdesktop.binding.metadata;import java.text.DateFormat;import java.text.SimpleDateFormat;import java.util.Date;import java.util.HashMap;import java.util.Map;import java.util.Set;/** * Class containing the static converter registry and a set of static Converter * classes for the common Java data types: * <ul> * <li>java.lang.Boolean</li> * <li>java.lang.String</li> * <li>java.lang.Integer</li> * <li>java.lang.Long</li> * <li>java.lang.Short</li> * <li>java.lang.Float</li> * <li>java.lang.Double</li> * <li>java.lang.Date</li> *  * </ul> * Converter instances are retrieved from the registry using the class * as the key.  Example usage: * <pre><code>    Converter converter = Converters.get(Integer.class); *     try { *         Integer value = (Integer)converter.decode("99", null); *     } *     catch (ConversionException e) { *         // conversion error! *     } * </code></pre> * <p> * Converters can also be added or replaced in the registry: * <pre><code>    Converters.put(Foo.class, new FooConverter()); * </code></pre> * </p> * * @author Amy Fowler * @version 1.0 */public class Converters {    private static Map map;    static {        map = new HashMap();        map.put(Boolean.class, "org.jdesktop.binding.metadata.Converters$BooleanConverter");        map.put(Date.class, "org.jdesktop.binding.metadata.Converters$DateConverter");        map.put(Double.class, "org.jdesktop.binding.metadata.Converters$DoubleConverter");        map.put(Float.class, "org.jdesktop.binding.metadata.Converters$FloatConverter");        map.put(Integer.class, "org.jdesktop.binding.metadata.Converters$IntegerConverter");//        map.put(LinkModel.class, "org.jdesktop.binding.Converters$LinkConverter");        map.put(Long.class, "org.jdesktop.binding.metadata.Converters$LongConverter");        map.put(Short.class, "org.jdesktop.binding.metadata.Converters$ShortConverter");        map.put(String.class, "org.jdesktop.binding.metadata.Converters$StringConverter");    }    /**     * Retrieves the converter for the class.     *     * @param klass class used as key for converter lookup     * @return Converter instance registered for specified class, or null if     *         no converter is currently registered for that class     */    public static Converter get(Class klass) {	Object obj = map.get(klass);	Converter converter = null;	if (obj != null) {	    if (obj instanceof String) {		try {		    Class cls = Class.forName((String)obj);		    converter = (Converter)cls.newInstance();		    map.put(klass, converter);		} catch (Exception ex) {		    converter = null;		}	    } else {		converter = (Converter)obj;	    }	}        return converter;    }    /**     * Registers the specified converter for the specified class, overriding     * any prior converter mapping for that class if it existed.     * @param klass class used as key for converter lookup     * @param converter Converter instance to be registered for the class     */    public static void put(Class klass, Converter converter) {        map.put(klass, converter);    }    /**     * Return all the types which currently have supported type converters.     *      * @return an non-null array of supported types     */    public static Class[] getTypes() {	Set keys = map.keySet();	return (Class[])keys.toArray(new Class[0]);    }    protected Converters() {    } //prevent instantiation    /**     * Converter for java.lang.String which passes the value     * unchanged.  The <code>format</code> parameter is ignored.     */    static class StringConverter implements Converter {        public String encode(Object value, Object format)            throws ConversionException{            if (value != null && value instanceof String) {                return (String)value;            }            throw new ConversionException(value, String.class);        }        public Object decode(String value, Object format)            throws ConversionException {            try {                return value.toString();            }            catch (Exception e) {                throw new ConversionException(value, String.class, e);            }        }    }    /**     * Converter for java.lang.Boolean.     * Conversion from String to Boolean will return Boolean.TRUE if the     * string value is equal to &quot;true&quot; using a case-insensitive     * compare and will return Boolean.FALSE for all other string values.     * The <code>format</code> parameter is ignored.     */    static class BooleanConverter implements Converter {        public String encode(Object value, Object format)            throws ConversionException {            try {                Boolean boolValue = (Boolean) value;                return boolValue.toString();            }            catch (Exception e) {                throw new ConversionException(value, Boolean.class, e);            }        }        public Object decode(String value, Object format)            throws ConversionException {            try {                // this returns Boolean.FALSE for any string that is not                // equivelent to "true" using a case-insensitive compare                return Boolean.valueOf(value);            }            catch (Exception e) {                throw new ConversionException(value, Boolean.class, e);            }        }    }    /**     * Converter for java.lang.Integer.     * The <code>format</code> parameter may either be an Integer object     * representing the radix, or null.  If <code>format</code> is null,     * a default radix of 10 is used.     */    static class IntegerConverter implements Converter {        public String encode(Object value, Object format)            throws ConversionException {            try {                int intValue = ((Integer)value).intValue();                int radix = (format == null? 10 : ((Integer)format).intValue());                return Integer.toString(intValue, radix);            }            catch (Exception e) {                throw new ConversionException(value, Integer.class, e);            }         }         public Object decode(String value, Object format)             throws ConversionException {             try {                 int radix = (format == null? 10 : ((Integer)format).intValue());                 return Integer.valueOf(value, radix);             }             catch (Exception e) {                throw new ConversionException(value, Integer.class, e);             }         }     }     /**      * Converter for java.lang.Long.      * The <code>format</code> parameter may either be an Integer object      * representing the radix or null.  If <code>format</code> is null,      * a default radix of 10 is used.      */     static class LongConverter implements Converter {         public String encode(Object value, Object format)             throws ConversionException {             try {                 long longValue = ((Long)value).longValue();                 int radix = (format == null? 10 : ((Integer)format).intValue());                 return Long.toString(longValue, radix);             }             catch (Exception e) {                 throw new ConversionException(value, Long.class, e);             }

⌨️ 快捷键说明

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