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

📄 rtproperties.java

📁 Open DMT GPS server source code
💻 JAVA
📖 第 1 页 / 共 4 页
字号:
    public long getLong(String key, long dft)    {        Object val = this._getProperty(key, null, null, true);        if (val == null) {            return dft;        } else        if (val instanceof Number) {            return ((Number)val).longValue();        } else {            return StringTools.parseLong(val.toString(), dft);        }    }    public long[] getLongArray(String key, long dft[])    {        String val[] = this.getStringArray(key, null);        if (val == null) {            return dft;        } else {            long n[] = new long[val.length];            for (int i = 0; i < val.length; i++) {                n[i] = StringTools.parseLong(val[i], 0L);            }            return n;        }    }    public void setLong(String key, long value)    {        this.setProperty(key, value);    }    public void setLongArray(String key, long value[])    {        this.setProperty(key, value);    }    public void setProperty(String key, long value)    {        this.setProperty(key, new Long(value));    }    // ------------------------------------------------------------------------    public int getInt(String key)    {        return this.getInt(key, 0);    }    public int getInt(String key[], int dft)    {        return this.getInt(this.getFirstDefinedKey(key), dft);    }    public int getInt(String key, int dft)    {        Object val = this._getProperty(key, null, null, true);        if (val == null) {            return dft;        } else        if (val instanceof Number) {            return ((Number)val).intValue();        } else {            return StringTools.parseInt(val.toString(), dft);        }    }    public int[] getIntArray(String key, int dft[])    {        String val[] = this.getStringArray(key, null);        if (val == null) {            return dft;        } else {            int n[] = new int[val.length];            for (int i = 0; i < val.length; i++) {                n[i] = StringTools.parseInt(val[i], 0);            }            return n;        }    }    public void setInt(String key, int value)    {        this.setProperty(key, value);    }    public void setIntArray(String key, int value[])    {        this.setProperty(key, value);    }    public void setProperty(String key, int value)    {        this.setProperty(key, new Integer(value));    }    // ------------------------------------------------------------------------    public boolean getBoolean(String key)    {        boolean dft = false;        return this._getBoolean_dft(key, dft, true);    }    public boolean getBoolean(String key[], boolean dft)    {        return this.getBoolean(this.getFirstDefinedKey(key), dft);    }    public boolean getBoolean(String key, boolean dft)    {        return this._getBoolean_dft(key, dft, DEFAULT_TRUE_IF_BOOLEAN_STRING_EMPTY);    }    private boolean _getBoolean_dft(String key, boolean dft, boolean dftTrueIfEmpty)    {        Object val = this._getProperty(key, null, null, true);        if (val == null) {            return dft;        } else        if (val instanceof Boolean) {            return ((Boolean)val).booleanValue();        } else        if (val.toString().equals("")) {            return dftTrueIfEmpty? true : dft;        } else {            return StringTools.parseBoolean(val.toString(), dft);        }    }    public void setBoolean(String key, boolean value)    {        this.setProperty(key, value);    }    public void setBooleanArray(String key, boolean value[])    {        this.setProperty(key, value);    }    public void setProperty(String key, boolean value)    {        this.setProperty(key, new Boolean(value));    }    // ------------------------------------------------------------------------    public void printProperties()    {        this.printProperties(null, null);    }    public void printProperties(RTProperties exclProps)    {        this.printProperties(exclProps, null);    }    public void printProperties(Collection<?> orderBy)    {        this.printProperties(null, orderBy);    }    public void printProperties(RTProperties exclProps, Collection<?> orderBy)    {        Print.logInfo(this.toString(exclProps, orderBy, true));    }    // ------------------------------------------------------------------------    public boolean equals(Object other)    {        if (other instanceof RTProperties) {            // We need to perform our own 'equals' checking here:            // Two RTProperties are equal if they contain the same properties irrespective of ordering.            // [All property values are compared as Strings]            RTProperties rtp = (RTProperties)other;            Map M1 = this.getProperties();            Map M2 = rtp.getProperties();            if (M1.size() == M2.size()) {                for (Iterator i = M1.keySet().iterator(); i.hasNext();) {                    Object key = i.next();                    if (M2.containsKey(key)) {                        Object m1Val = M1.get(key);                        Object m2Val = M2.get(key);                        String m1ValStr = (m1Val != null)? m1Val.toString() : null;                        String m2ValStr = (m2Val != null)? m2Val.toString() : null;                        if (m1Val == m2Val) {                            continue; // they are the same object (or both null)                        } else                        if ((m1ValStr != null) && m1ValStr.equals(m2ValStr)) {                            continue; // the values are equals                        } else {                            //Print.logInfo("Values not equal: " + m1ValStr + " <==> " + m2ValStr);                            return false; // values are not equal                        }                    } else {                        //Print.logInfo("Key doesn't exist in M2");                        return false; // key doesn't exist in M2                    }                }                return true; // all key/vals matched            } else {                //Print.logInfo("Sizes don't match");                return false;            }        } else {            return false;        }    }    // ------------------------------------------------------------------------        public void saveProperties(File cfgFile)        throws IOException    {        /* property maps */        Map propMap = this.getProperties();        /* encode properties */        StringBuffer strProps = new StringBuffer();        for (Iterator i = propMap.keySet().iterator(); i.hasNext();) {            Object keyObj = i.next();            Object valObj = propMap.get(keyObj);            strProps.append(keyObj.toString());            strProps.append(KeyValSeparatorChar);            if (valObj != null) {                strProps.append(valObj.toString());            }            strProps.append("\n");        }                /* save to file */        FileTools.writeFile(strProps.toString().getBytes(), cfgFile);    }    // ------------------------------------------------------------------------    public String toString()    {        return this.toString(null, null, false);    }    public String toString(RTProperties exclProps)    {        return this.toString(exclProps, null, false);    }    public String toString(Collection<?> orderBy)    {        return this.toString(null, orderBy, false);    }    public String toString(RTProperties exclProps, Collection<?> orderBy)    {        return this.toString(null, orderBy, false);    }    public String toString(RTProperties exclProps, Collection<?> orderBy, boolean inclNewline)    {        StringBuffer sb = new StringBuffer();        /* append name */        String n = this.getName();        if (!n.equals("")) {            sb.append(n).append(NameSeparatorChar).append(" ");        }        /* property maps */        Map<Object,Object> propMap = this.getProperties();        Map<Object,Object> exclMap = (exclProps != null)? exclProps.getProperties() : null;        /* order by */        Set<Object> orderSet = null;        if (orderBy != null) {            orderSet = new OrderedSet<Object>(orderBy, true);            orderSet.addAll(propMap.keySet());            // 'orderSet' now contains the union of keys from 'orderBy' and 'propMap.keySet()'        } else {            orderSet = propMap.keySet();        }        /* encode properties */        for (Iterator<?> i = orderSet.iterator(); i.hasNext();) {            Object keyObj = i.next(); // possible this key doesn't exist in 'propMap' if 'orderBy' used.            if (!KEY_NAME.equals(keyObj) && propMap.containsKey(keyObj)) {                Object valObj = propMap.get(keyObj); // key guaranteed here to be in 'propMap'                if ((exclMap == null) || !RTProperties.compareMapValues(valObj, exclMap.get(keyObj))) {                    sb.append(keyObj.toString()).append(KeyValSeparatorChar);                    String v = (valObj != null)? valObj.toString() : "";                    if ((v.indexOf(" ") >= 0) || (v.indexOf("\t") >= 0) || (v.indexOf("\"") >= 0)) {                        sb.append(StringTools.quoteString(v));                    } else {                        sb.append(v);                    }                    if (inclNewline) {                        sb.append("\n");                    } else                    if (i.hasNext()) {                        sb.append(" ");                    }                } else {                    //Print.logDebug("Key hasn't changed: " + key);                }            }        }        return sb.toString().trim();    }    private static boolean compareMapValues(Object value, Object target)    {        if ((value == null) && (target == null)) {            return true;        } else        if ((value == null) || (target == null)) {            return false;        } else        if (value.equals(target)) {            return true;        } else {            return value.toString().equals(target.toString());        }    }    // ------------------------------------------------------------------------    /**    *** OrderedProperties class    **/    public static class OrderedProperties        extends Properties    {        private OrderedMap<Object,Object> orderedMap = null;        public OrderedProperties() {            super();            this.orderedMap = new OrderedMap<Object,Object>();        }        public Object put(Object key, Object value) {            Object rtn = super.put(key, value);            this.orderedMap.put(key, value);            return rtn;        }        public OrderedMap<Object,Object> getOrderedMap() {            return this.orderedMap;        }    }    // ------------------------------------------------------------------------        private static boolean STRING_PARSE_PROPS = true;        private static boolean isEOL(byte b)     { return ((b == '\n') || (b == '\r')); }    private static boolean isEOL(char b)     { return ((b == '\n') || (b == '\r')); }    private static boolean isCOMMENT(byte b) { return ((b == '#')  || (b == '!') ); }    private static boolean isCOMMENT(char b) { return ((b == '#')  || (b == '!') ); }    private static boolean isSEP(byte b)     { return ((b == '=')  || (b == ':') ); }    private static boolean isSEP(char b)     { return ((b == '=')  || (b == ':') ); }    public static Properties loadProperties(Properties props, InputStream in)        throws IOException    {        byte data[] = FileTools.readStream(in);        if (STRING_PARSE_PROPS) {            String dataStr = StringTools.toStringValue(data);            String ds[] = StringTools.split(dataStr,'\n');            for (int i = 0; i < ds.length; i++) {                String d = ds[i].trim();                if (d.equals("") || isCOMMENT(d.charAt(0))) { continue; }                int p = d.indexOf("=");                if (p < 0) { p = d.indexOf(":"); }                String key = (p >= 0)? d.substring(0,p) : d;                String val = (p >= 0)? d.substring(p+1) : "";                if (!key.equals("")) {                    Print.logInfo("S)Prop: " + key + " ==> " + val);                    props.setProperty(key, val);                }            }        } else {            // This may not be safe for non-"ISO-8859-1" character sets            for (int s = 0; s < data.length;) {                // skip to start of next key                while ((s < data.length) && Character.isWhitespace(data[s])) { s++; }                if ((s >= data.length) || isCOMMENT(data[s])) {                    while ((s < data.length) && !isEOL(data[s])) { s++; }                    continue;                }                // find separator/eol                int e, sep = -1;                for (e = s; (e < data.length) && !isEOL(data[e]); e++) {                    if ((sep < 0) && isSEP(data[e])) { sep = e; }                }                // parse key/value                String key = "";                String val = "";                if (sep >= 0) {                    key = StringTools.toStringValue(data, s, sep - s).trim();                    // TODO: decode Unicode value                    val = StringTools.toStringValue(data, sep + 1, e - sep).trim();                } else {                    key = StringTools.toStringValue(data, s, e - s).trim();                    val = "";                }                if (!key.equals("")) {                    Print.logInfo("B)Prop: " + key + " ==> " + val);                    props.setProperty(key, val);                }                // start at nexe character                s = e + 1;            }        }        return props;    }        // ------------------------------------------------------------------------}

⌨️ 快捷键说明

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