java 快捷操作Properties 辅助类

简介: 我们在操作Properties 时候一般都是按照键值来操作,但是如果我们在操作一个复杂 的Properties 时候这样先进行很多操作就显得累赘。比如我们想获取Spring Boot 的 Properties 时候。里面很多复杂的键。我们就需要考虑找一个工具类来实现。package com.yoke.util;import java.util.ArrayList;i

我们在操作Properties 时候一般都是按照键值来操作,但是如果我们在操作一个复杂 的Properties 时候这样先进行很多操作就显得累赘。比如我们想获取Spring Boot 的 Properties 时候。里面很多复杂的键。我们就需要考虑找一个工具类来实现。

package com.yoke.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.Properties;
import java.util.StringTokenizer;

/**
 *
 */
public class PropertiesParser {
    Properties props = null;

    public PropertiesParser(Properties props) {
        this.props = props;
    }

    public Properties getUnderlyingProperties() {
        return this.props;
    }

    public String getStringProperty(String name) {
        return this.getStringProperty(name, (String)null);
    }

    public String getStringProperty(String name, String def) {
        String val = this.props.getProperty(name, def);
        if(val == null) {
            return def;
        } else {
            val = val.trim();
            return val.length() == 0?def:val;
        }
    }

    public String[] getStringArrayProperty(String name) {
        return this.getStringArrayProperty(name, (String[])null);
    }

    public String[] getStringArrayProperty(String name, String[] def) {
        String vals = this.getStringProperty(name);
        if(vals == null) {
            return def;
        } else {
            StringTokenizer stok = new StringTokenizer(vals, ",");
            ArrayList strs = new ArrayList();

            try {
                while(stok.hasMoreTokens()) {
                    strs.add(stok.nextToken().trim());
                }

                return (String[])((String[])strs.toArray(new String[strs.size()]));
            } catch (Exception var7) {
                return def;
            }
        }
    }

    public boolean getBooleanProperty(String name) {
        return this.getBooleanProperty(name, false);
    }

    public boolean getBooleanProperty(String name, boolean def) {
        String val = this.getStringProperty(name);
        return val == null?def:Boolean.valueOf(val).booleanValue();
    }

    public byte getByteProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Byte.parseByte(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public byte getByteProperty(String name, byte def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Byte.parseByte(val);
            } catch (NumberFormatException var5) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public char getCharProperty(String name) {
        return this.getCharProperty(name, '\u0000');
    }

    public char getCharProperty(String name, char def) {
        String param = this.getStringProperty(name);
        return param == null?def:param.charAt(0);
    }

    public double getDoubleProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Double.parseDouble(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public double getDoubleProperty(String name, double def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Double.parseDouble(val);
            } catch (NumberFormatException var6) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public float getFloatProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Float.parseFloat(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public float getFloatProperty(String name, float def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Float.parseFloat(val);
            } catch (NumberFormatException var5) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public int getIntProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Integer.parseInt(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public int getIntProperty(String name, int def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Integer.parseInt(val);
            } catch (NumberFormatException var5) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public int[] getIntArrayProperty(String name) throws NumberFormatException {
        return this.getIntArrayProperty(name, (int[])null);
    }

    public int[] getIntArrayProperty(String name, int[] def) throws NumberFormatException {
        String vals = this.getStringProperty(name);
        if(vals == null) {
            return def;
        } else {
            StringTokenizer stok = new StringTokenizer(vals, ",");
            ArrayList ints = new ArrayList();

            try {
                while(stok.hasMoreTokens()) {
                    try {
                        ints.add(new Integer(stok.nextToken().trim()));
                    } catch (NumberFormatException var8) {
                        throw new NumberFormatException(" \'" + vals + "\'");
                    }
                }

                int[] e = new int[ints.size()];

                for(int i = 0; i < ints.size(); ++i) {
                    e[i] = ((Integer)ints.get(i)).intValue();
                }

                return e;
            } catch (Exception var9) {
                return def;
            }
        }
    }

    public long getLongProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Long.parseLong(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public long getLongProperty(String name, long def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Long.parseLong(val);
            } catch (NumberFormatException var6) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public short getShortProperty(String name) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            throw new NumberFormatException(" null string");
        } else {
            try {
                return Short.parseShort(val);
            } catch (NumberFormatException var4) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public short getShortProperty(String name, short def) throws NumberFormatException {
        String val = this.getStringProperty(name);
        if(val == null) {
            return def;
        } else {
            try {
                return Short.parseShort(val);
            } catch (NumberFormatException var5) {
                throw new NumberFormatException(" \'" + val + "\'");
            }
        }
    }

    public String[] getPropertyGroups(String prefix) {
        Enumeration keys = this.props.propertyNames();
        HashSet groups = new HashSet(10);
        if(!prefix.endsWith(".")) {
            prefix = prefix + ".";
        }

        while(keys.hasMoreElements()) {
            String key = (String)keys.nextElement();
            if(key.startsWith(prefix)) {
                String groupName = key.substring(prefix.length(), key.indexOf(46, prefix.length()));
                groups.add(groupName);
            }
        }

        return (String[])((String[])groups.toArray(new String[groups.size()]));
    }

    public Properties getPropertyGroup(String prefix) {
        return this.getPropertyGroup(prefix, false, (String[])null);
    }

    public Properties getPropertyGroup(String prefix, boolean stripPrefix) {
        return this.getPropertyGroup(prefix, stripPrefix, (String[])null);
    }

    public Properties getPropertyGroup(String prefix, boolean stripPrefix, String[] excludedPrefixes) {
        Enumeration keys = this.props.propertyNames();
        Properties group = new Properties();
        if(!prefix.endsWith(".")) {
            prefix = prefix + ".";
        }

        while(true) {
            String key;
            do {
                if(!keys.hasMoreElements()) {
                    return group;
                }

                key = (String)keys.nextElement();
            } while(!key.startsWith(prefix));

            boolean exclude = false;
            if(excludedPrefixes != null) {
                for(int value = 0; value < excludedPrefixes.length && !exclude; ++value) {
                    exclude = key.startsWith(excludedPrefixes[value]);
                }
            }

            if(!exclude) {
                String var9 = this.getStringProperty(key, "");
                if(stripPrefix) {
                    group.put(key.substring(prefix.length()), var9);
                } else {
                    group.put(key, var9);
                }
            }
        }
    }
}

这个类来源于jsoup ,主要是对类似于SpringBoot 复杂的键来进行分组管理。

相关文章
|
1月前
|
存储 Java 索引
用Java语言实现一个自定义的ArrayList类
自定义MyArrayList类模拟Java ArrayList核心功能,支持泛型、动态扩容(1.5倍)、增删改查及越界检查,底层用Object数组实现,适合学习动态数组原理。
87 4
|
1月前
|
IDE JavaScript Java
在Java 11中,如何处理被弃用的类或接口?
在Java 11中,如何处理被弃用的类或接口?
165 5
|
1月前
|
JSON 网络协议 安全
【Java】(10)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
143 1
|
1月前
|
Java Go 开发工具
【Java】(8)正则表达式的使用与常用类分享
正则表达式定义了字符串的模式。正则表达式并不仅限于某一种语言,但是在每种语言中有细微的差别。
203 1
|
1月前
|
存储 Java 程序员
【Java】(6)全方面带你了解Java里的日期与时间内容,介绍 Calendar、GregorianCalendar、Date类
java.util 包提供了 Date 类来封装当前的日期和时间。Date 类提供两个构造函数来实例化 Date 对象。第一个构造函数使用当前日期和时间来初始化对象。Date( )第二个构造函数接收一个参数,该参数是从1970年1月1日起的毫秒数。
148 1
|
1月前
|
JSON 网络协议 安全
【Java基础】(1)进程与线程的关系、Tread类;讲解基本线程安全、网络编程内容;JSON序列化与反序列化
几乎所有的操作系统都支持进程的概念,进程是处于运行过程中的程序,并且具有一定的独立功能,进程是系统进行资源分配和调度的一个独立单位一般而言,进程包含如下三个特征。独立性动态性并发性。
161 1
|
1月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
265 5
|
2月前
|
数据采集 存储 弹性计算
高并发Java爬虫的瓶颈分析与动态线程优化方案
高并发Java爬虫的瓶颈分析与动态线程优化方案
Java 数据库 Spring
136 0
|
2月前
|
算法 Java
Java多线程编程:实现线程间数据共享机制
以上就是Java中几种主要处理多线程序列化资源以及协调各自独立运行但需相互配合以完成任务threads 的技术手段与策略。正确应用上述技术将大大增强你程序稳定性与效率同时也降低bug出现率因此深刻理解每项技术背后理论至关重要.
220 16