A container class to hold the parameters

Source: Internet
Author: User
Tags empty integer key modify tostring

Import java.util.*;

/** * Provides a container for holding parameters that provide parameter reading capability for methods with multiple parameters, multiple optional parameters, and variable parameters. * Using classes such as map directly is not convenient for data type processing. This class provides read methods for commonly used data types. Usage example:<br> * port = args.get ("port", 8080);<br> * The port value placed in the map can be "8443" or New Integer (8443) or any other type, as long as ToString () * Can parse out an integer. * @author Sunlen * @version 1.0 */public Class args{/** a constant null parameter table. Used to pass null arguments, the list of parameters is locked and cannot be modified. * * public static final Args EMPTY = new Args (). Lock ();

The/** parameter list is locked and the locked argument list is read-only. * * Boolean locked;

The parameters saved by the/** map. * * Map args;

/** creates an empty argument list.    * * Public Args () {This (new HashMap ()); }

/** * Create parameters with map.     * @param theargs The Map object that holds the parameters. */Public Args (Map Theargs) {if (theargs==null) {throw new NullPointerException ("argument        Is null ");    } args = Theargs; }

/** * method to get string parameters.     * @param the name of the key parameter.     * @param def parameter value.        */Public String Get (string key,string def) {try {return Args.get (key). ToString ();        catch (Exception ex) {return def; }    }

/** * method for obtaining integer parameters.     * @param the name of the key parameter.     * @param def parameter value. */public int get (String key,int def) {try {return Integer.parseint (Args.get (key). Tostrin        g ());        catch (Exception ex) {return def; }    }

/** * method for obtaining long integer parameters.     * @param the name of the key parameter.     * @param def parameter value. /Public long Get (String Key,long def) {try {return Long.parselong (Args.get (key). Tostrin        g ());        catch (Exception ex) {return def; }    }

/** * method for obtaining floating-point number parameters.     * @param the name of the key parameter.     * @param def parameter value. /public float to (String key,float def) {try {return float.parsefloat (Args.get (key). ToS        Tring ());        catch (Exception ex) {return def; }    }

/** * Method for obtaining Boolean parameters.     Only "true" resolves to true and other resolves to false.     * @param the name of the key parameter.     * @param def parameter value. */Public Boolean get (String Key,boolean def) {try {return ("true". Equals (Args.get (key))        ;        catch (Exception ex) {return def; }    }

   /**     * Gets the original object type.      * @param the name of the key parameter.      * @param def parameter default value.      */    public Object get (String key,object def)     {         try        {             Object obj = args.get (key);             if (obj==null)             {                 return def;            }             return obj;       }        catch (Exception ex)         {            return def;       }    }

/** * Adds a parameter to the current parameter table.     * @param the name of the key parameter.     * @param value parameter values.     * @return The parameter table after the new parameter has been added (the object instance has not changed).     * @exception unsupportedoperationexception If the argument list is locked. */Public Args Set (String key,object value) {if (locked) {throw new Unsupportedoperationex        Ception ("Args have locked,can modify");        } args.put (Key,value);    return this; }

/** * Adds a parameter to the current parameter table.     * @param the name of the key parameter.     * @param value parameter values.     * @return The parameter table after the new parameter has been added (the object instance has not changed).     * @exception unsupportedoperationexception If the argument list is locked. */Public Args Set (String key,int value) {if (locked) {throw new Unsupportedoperationexcep        tion ("Args have locked,can modify");        } args.put (Key,new Integer (value));    return this; }

   /**     * Adds a parameter to the current parameter table.      * @param the name of the key parameter.      * @param value parameter values.      * @return The parameter table after the new parameter has been added (the object instance has not changed).      * @exception unsupportedoperationexception If the argument list is locked.      */    Public Args Set (String key,boolean value)     {         if (locked)         {             throw new Unsupportedoperationexception (                                               "Args have locked,can modify");       }         Args.put (key,new Boolean (VALue));        return this;   }

/** * Adds a parameter to the current parameter table.     * @param the name of the key parameter.     * @param value parameter values.     * @return The parameter table after the new parameter has been added (the object instance has not changed).     * @exception unsupportedoperationexception If the argument list is locked. */Public Args Set (String Key,long value) {if (locked) {throw new Unsupportedoperationexce        Ption ("Args have locked,can modify");        } args.put (Key,new Long (value));    return this; }

/** * Adds a parameter to the current parameter table.     * @param the name of the key parameter.     * @param value parameter values.     * @return The parameter table after the new parameter has been added (the object instance has not changed).     * @exception unsupportedoperationexception If the argument list is locked. */Public Args Set (String key,float value) {if (locked) {throw new Unsupportedoperationexc        Eption ("Args have locked,can modify");        } args.put (Key,new Float (value));    return this; }

/** * Adds a parameter to the current parameter table.     * @param the name of the key parameter.     * @param value parameter values.     * @return The parameter table after the new parameter has been added (the object instance has not changed).     * @exception unsupportedoperationexception If the argument list is locked. */Public Args Set (String key,double value) {if (locked) {throw new Unsupportedoperationex        Ception ("Args have locked,can modify");        } args.put (Key,new Double (value));    return this; }

/** * Lock parameter list. When locked, the argument list becomes read-only, and any call to the set () method throws a * Unsupportedoperationexception exception.     The parameter table that has been locked again calls this method without any effect.     * @return A locked argument list (the object instance does not change).        */Public Args Lock () {locked = true;    return this; }

A string description of the/** * parameter list.     * @return all parameter values.    */Public String toString () {return args.tostring (); }}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.