A container class for saving parameters

xiaoxiao2021-03-06  20

Import java.util. *;

/ ** * Provides a container for saving parameters, providing parameter read capabilities for those with more parameters, optional parameters, and parameter variable. * Direct use of MAP and other classes are not convenient for data type. This class provides a read method for common data types. Method:
* port = args.get ("port", 8080);
* Put value placed in MAP can be "8443" or new integer (8443) or any other type, as long as totring ) * Ability to resolve an integer. * @Author sunlen * @version 1.0 * / public class args {/ ** A constant empty parameter table. Used to pass empty parameters, the parameter list has been locked and cannot be modified. * / Public static final args Empty = new args (). LOCK ();

/ ** The parameter list has been locked, and the list after the lock will read. * / Boolean Locked;

/ ** Map saved parameters. * / Map args;

/ ** Create a list of empty parameters. * / Public args () {this (new hashmap ());}

/ ** * Create a parameter with MAP. * @Param THEARGS Saves the MAP object of the parameter. * / Public args (map theargs) {if (THEARGS == null) {throw new nullpointEREXCEPTION ("argument is null";} args = THEARGS;

/ ** * Method for obtaining string parameters. * @Param Key parameter name. * @Param DEF parameter value. * / Public string get (String key, string def) {try {return args.get (key) .tostring ();} catch (exception ex) {return def;}}

/ ** * Method for acquiring integer parameters. * @Param Key parameter name. * @Param DEF parameter value. * / Public int GET (INT DEF) {Try {Return INTEGER.PARSEINT (Args.get (key) .tostring ());} catch (exception ex) {returnif;}}

/ ** * Method for acquiring long integer parameters. * @Param Key parameter name. * @Param DEF parameter value. * / Public long position (string key, long def) {Try {return long.parselong ());} catch (exception ex) {returnif;}}}}}}}}}}}

/ ** * Method for acquiring floating point number parameters. * @Param Key parameter name. * @Param DEF parameter value. * / Public float get (string key, float def) {Try {return float.parsefloat (args.get (key) .tostring ());} catch (exception ex) {return def;}} / ** * get Boolean Method of parameters. Only "True" is parsed into TRUE other parsing into false. * @Param Key parameter name. * @Param DEF parameter value. * / Public boolean get (String Key, Boolean DEF) {TRE {Return ("true" .Equals (args.get (key)));} catCH (Exception ex) {returnif;}}}}

/ ** * acquire the original object type. * @Param Key parameter name. * @Param DEF parameter default. * / 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; }

/ ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (String Key, Object Value) {if (locked) {throw new unsupportedOperationException ("args have locked, can modify);} args.put (key, value); return this;}

/ ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (string key, int value) {if (locked) {throw new unsupportedOperationException ("Args Have Locked, Can Modify);} args.put (key, new integer (value); returnid;} / ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (String Key, Boolean Value) {if (locked) {throw new unsupportedOperationException ("args have locked, can modify);} args.put (key, new boolean); return this;}

/ ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (String Key, long value) {if (locked) {throw new unsupportedOperationException ("args have locked, can modify);} args.put (key, new long (value); returnid;}

/ ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (String Key, Float Value) {if (locked) {throw new unsupportedOperationException ("Args Have Locked, Can Modify);} args.put (key, new float (value); returnid;} / ** * Add a parameter to the current parameter table. * @Param Key parameter name. * @Param Value parameter value. * @Return has added a parameter table after new parameters (the object instance is not changed). * @Exception UnsupportedOperationException If the parameter list has been locked. * / Public args set (String Key, Double Value) {if (locked) {throw new unsupportedOperationException ("args have locked, can modify);} args.put (key, new double (value); Return this;}

/ ** * Lock the parameter list. After the lock is locked, the parameter list will become read-only, and any call to the set () method will throw * unsupportedOperationException exception. There is no impact on the method that has been locked again. * @Return List the parameter list (the object instance is not changed). * / Public args lock () {locked = true; return this;}

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

Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.

[

Click here to favor this article]

Published on March 21, 2005 9:33 AM

HREF = "http://blog.9cbs.net/sunlen/services/pingback.aspx" Rel = "pingback" />

comment

SSPhoenix published

2005-03-21 10:29 AM

Can't Modify?

Only lock, did not unlock?

Can you give an example of using?

SUNLEN published

2005-03-21 11:23 AM

If you want to unlock, add a unlock method, the code is as follows:

Public args unlock ()

{

Locked = false;

Return this;

}

The method is as follows:

Args args = new args (); // Initialization class args = args.set ("maxnum", 99); // Set the value of MaxNum is 99

INT MAXNUM = args.get ("maxnum", 100); // Get the value of MaxNum, if the acquisition fails, set it to 100

Args = arggs.lock (); // Locking container

Args = arggs.set ("MaxNum", 999);

MaxNum = args.get ("maxnum", 100);

转载请注明原文地址:https://www.9cbs.com/read-43933.html

New Post(0)