java 快捷操作Properties 辅助类

我们在操作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 复杂的键来进行分组管理。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值