parameterlist.java

来自「jpeg2000编解码」· Java 代码 · 共 484 行 · 第 1/2 页

JAVA
484
字号
     * @param pname The parameter name.     *     * @return the value of the parameter as a string, or null if there is no     * parameter with the name 'pname'.     * */    public String getParameter(String pname) {        String pval;        pval = (String) get(pname);        if (pval == null && defaults != null) { // if parameter is not there            // Look in defaults            pval = defaults.getProperty(pname);        }        return pval;    }    /**     * Returns the value of the named parameter as a boolean. The value "on"     * is interpreted as 'true', while the value "off" is interpreted as     * 'false'. If the parameter has another value then an     * StringFormatException is thrown. If the parameter 'pname' is not in the     * parameter list, an IllegalArgumentException is thrown.     *     * @param pname The parameter name.     *     * @return the value of the parameter as a boolean.     *     * @exception StringFormatException If the parameter has a value which is     * neither "on" nor "off".     *     * @exception IllegalArgumentException If there is no parameter with the     * name 'pname' in the parameter list.     * */    public boolean getBooleanParameter(String pname) {        String s = (String) getParameter(pname);        if (s == null) {            throw new IllegalArgumentException("No parameter with name "+                                               pname);        }        else if (s.equals("on")) {            return true;        }        else if (s.equals("off")) {            return false;        }        else {            throw new StringFormatException("Parameter \""+pname+                                            "\" is not boolean: " + s);        }    }    /**     * Returns the value of the named parameter as an int. If the parameter     * has a non-numeric value a NumberFormatException is thrown. If the     * parameter has a multiple word value than the first word is returned as     * an int, others are ignored. If the parameter 'pname' is not in the     * parameter list, an IllegalArgumentException is thrown.     *     * @param pname The parameter name.     *     * @return the value of the parameter as an int.     *     * @exception NumberFormatException If the parameter has a non-numeric     * value.     *     * @exception IllegalArgumentException If there is no parameter with the     * name 'pname' in the parameter list.     * */    public int getIntParameter(String pname) {         String s = (String) getParameter(pname);        if (s == null) {            throw new IllegalArgumentException("No parameter with name "+                                               pname);        }        else {            try {                return Integer.parseInt(s);            }            catch (NumberFormatException e) {                throw new NumberFormatException("Parameter \""+pname+                                                "\" is not integer: "                                                + e.getMessage());            }        }   }    /**     * Returns the value of the named parameter as a float. If the parameter     * has a non-numeric value a NumberFormatException is thrown. If the     * parameter has a multiple word value than the first word is returned as     * an int, others are ignored. If the parameter 'pname' is not in the     * parameter list, an IllegalArgumentException is thrown.     *     * @param pname The parameter name.     *     * @exception NumberFormatException If the parameter has a non-numeric     * value.     *     * @exception IllegalArgumentException If there is no parameter with the     * name 'pname' in the parameter list.     *     * @return the value of the parameter as a float.     * */    public float getFloatParameter(String pname) {        String s = (String) getParameter(pname);        if (s == null) {            throw new IllegalArgumentException("No parameter with name "+                                               pname);        }        else {            try {                // Unfortunately there is no method to convert from a string                // directly to a float                return (new Float(s)).floatValue();            }            catch (NumberFormatException e) {                throw new NumberFormatException("Parameter \""+pname+                                                "\" is not floating-point: "                                                + e.getMessage());            }        }    }    /**     * Checks if the parameters which name starts with the prefix 'prfx' in     * the parameter list are all in the list of valid parameter names     * 'plist'. If there is a parameter that is not in 'plist' an     * IllegalArgumentException is thrown with an explanation message. The     * default parameters are also included in the check.     *     * @param prfx The prefix of parameters to check.     *     * @param plist The list of valid parameter names for the 'prfx'     * prefix. If null it is considered that no names are valid.     *     * @exception IllegalArgumentException If there's a parameter name     * starting with 'prfx' which is not in the valid list of parameter names.     * */    public void checkList(char prfx, String plist[]) {        Enumeration args;        String val;        int i;        boolean isvalid;        args = propertyNames();        while (args.hasMoreElements()) {            val = (String) args.nextElement();            if (val.length() > 0 && val.charAt(0) == prfx) {                isvalid = false;                if (plist != null) {                    for (i=plist.length-1; i>=0; i--) {                        if (val.equals(plist[i])) {                            isvalid = true;                            break;                        }                    }                }                if (!isvalid) { // Did not find valid flag                    throw new IllegalArgumentException("Option '"+val+"' is "+                                                       "not a valid one.");                }            }        }    }    /**     * Checks if the parameters which names do not start with any of the     * prefixes in 'prfxs' in this ParameterList are all in the list of valid     * parameter names 'plist'. If there is a parameter that is not in 'plist'     * an IllegalArgumentException is thrown with an explanation message. The     * default parameters are also included in the check.     *     * @param prfxs The prefixes of parameters to ignore.     *     * @param plist The list of valid parameter names. If null it is     * considered that no names are valid.     *     * @exception IllegalArgumentException If there's a parameter name not     * starting with 'prfx' which is not in the valid list of parameter names.     * */    public void checkList(char prfxs[], String plist[]) {        Enumeration args;        String val,strprfxs;        int i;        boolean isvalid;        args = propertyNames();        strprfxs = new String(prfxs);        while (args.hasMoreElements()) {            val = (String) args.nextElement();            if (val.length() > 0 && strprfxs.indexOf(val.charAt(0)) == -1) {                isvalid = false;                if (plist != null) {                    for (i=plist.length-1; i>=0; i--) {                        if (val.equals(plist[i])) {                            isvalid = true;                            break;                        }                    }                }                if (!isvalid) {                    throw new IllegalArgumentException("Option '"+val+"' is "+                                                       "not a valid one.");                }            }        }    }    /**     * Converts the usage information to a list of parameter names in a single     * array. The usage information appears in a 2D array of String. The first     * dimensions contains the different options, the second dimension     * contains the name of the option (first element), the synopsis and the     * explanation. This method takes the names of the different options in     * 'pinfo' and returns them in a single array of String.     *     * @param pinfo The list of options and their usage info (see above).     *     * @return An array with the names of the options in pinfo. If pinfo is     * null, null is returned.     * */    public static String[] toNameArray(String pinfo[][]) {        String pnames[];        if (pinfo == null) {            return null;        }        pnames = new String[pinfo.length];        for (int i=pinfo.length-1; i>=0; i--) {            pnames[i] = pinfo[i][0];        }        return pnames;    }}

⌨️ 快捷键说明

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