Class ArgumentParser
- java.lang.Object
-
- com.github.javinator9889.utils.ArgumentParser
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Cloneable
public class ArgumentParser extends java.lang.Object implements java.io.Serializable, java.lang.Cloneable
ArgumentParser
provides a fully compatible class with plenty of objects. It is designed for using it as an access platform to methods' arguments and params, taking advantage oflambda expressions
of Java 8 and above.It is not thread safe as all the operations are not done atomically, so there is no guarantee that all the data stored at
HashMap
is saved in the order expected and with the expected values if appending from multiple threads at the same time.- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_CAPACITY
Default capacity of the HashMap object containing values - based on an analysis of typical consumption of applications.static float
DEFAULT_LOAD_FACTOR
Default load factor for HashMap - when there is no capacity specified, a better performance is obtained by setting it up to 0.75 as it is a good approximation tolog(2)
, so its capacity increases when the 75% of theHashTable
is filled.private java.util.HashMap<java.lang.String,java.lang.Object>
mArguments
HashMap
containing the params specified by the user that is working with this class.static float
OPTIMUM_LOAD_FACTOR_BASED_ON_SIZE
Optimum load factor used when aninitial capacity
is provided - if we know how much objects (keys) are going to be stored inside the hash table, there is no need to increase its capacity when, following the latest explanation, the 75% of theHashTable
is filled, as we will consume resources for increasing a table that will not be completely filled (if we are going to store 16 objects, there is no need to increase the table capacity when there are 12 objects stored for possibly saving 20 ones when we are going to only store 16 ones).
-
Constructor Summary
Constructors Modifier Constructor Description ArgumentParser()
Default constructor, using thedefault capacity (=8)
and thedefault load factor(=0.75)
for generating theHashMap
.ArgumentParser(int initialCapacity)
Generates a new instance ofthis
by using the providedinitialCapacity
.private
ArgumentParser(int initialCapacity, float loadFactor)
Private constructor designed for generating optimizedHashMap
based on user requirements.ArgumentParser(ArgumentParser from)
Duplicates an existingArgumentParser
copying the existing params at thefrom
object.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all params.ArgumentParser
clone()
Creates and returns a copy of this object.boolean
containsParam(java.lang.String paramName)
Returns true if this object has the named value.boolean
equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one.java.lang.Object
get(java.lang.String paramName)
Gets a value.java.lang.String
getAsString(java.lang.String paramName)
Obtains theString
representation of the value contained atparamName
.boolean
getBoolean(java.lang.String paramName)
Obtains the param value stored under the given param name.byte
getByte(java.lang.String paramName)
Obtains the param value stored under the given param name.byte[]
getBytes(java.lang.String paramName)
Obtains the param value stored under the given param name.double
getDouble(java.lang.String paramName)
Obtains the param value stored under the given param name.float
getFloat(java.lang.String paramName)
Obtains the param value stored under the given param name.int
getInt(java.lang.String paramName)
Obtains the param value stored under the given param name.java.util.List<?>
getList(java.lang.String paramName)
Obtains the param value stored under the given param name.long
getLong(java.lang.String paramName)
Obtains the param value stored under the given param name.short
getShort(java.lang.String paramName)
Obtains the param value stored under the given param name.java.lang.String
getString(java.lang.String paramName)
Obtains the param value stored under the given param name.int
hashCode()
Returns a hash code value for the object.boolean
isEmpty()
Indicates whether this collection is empty.java.util.Set<java.lang.String>
paramsNamesSet()
Returns a set of all params names'.java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>>
paramsValuesSet()
Returns a set of all of the params names' and params values'.void
putAllParams(ArgumentParser params)
Adds all values from other ArgumentParser to the currentarguments
.void
putNullParam(java.lang.String paramName)
Adds a value to the param set.void
putParam(java.lang.String paramName, byte[] paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Boolean paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Byte paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Double paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Float paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Integer paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Long paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Object paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.Short paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.lang.String paramValue)
Adds a value to the param set.void
putParam(java.lang.String paramName, java.util.List<?> paramValue)
Adds a value to the param set.void
remove(java.lang.String paramName)
Remove a single value.int
size()
Returns the number of params.java.lang.String
toString()
Returns a string containing a concise, human-readable description of this object.
-
-
-
Field Detail
-
DEFAULT_CAPACITY
public static final int DEFAULT_CAPACITY
Default capacity of the HashMap object containing values - based on an analysis of typical consumption of applications.- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTOR
Default load factor for HashMap - when there is no capacity specified, a better performance is obtained by setting it up to 0.75 as it is a good approximation tolog(2)
, so its capacity increases when the 75% of theHashTable
is filled.- See Also:
- Constant Field Values
-
OPTIMUM_LOAD_FACTOR_BASED_ON_SIZE
public static final float OPTIMUM_LOAD_FACTOR_BASED_ON_SIZE
Optimum load factor used when aninitial capacity
is provided - if we know how much objects (keys) are going to be stored inside the hash table, there is no need to increase its capacity when, following the latest explanation, the 75% of theHashTable
is filled, as we will consume resources for increasing a table that will not be completely filled (if we are going to store 16 objects, there is no need to increase the table capacity when there are 12 objects stored for possibly saving 20 ones when we are going to only store 16 ones).- See Also:
- Constant Field Values
-
mArguments
private java.util.HashMap<java.lang.String,java.lang.Object> mArguments
HashMap
containing the params specified by the user that is working with this class.
-
-
Constructor Detail
-
ArgumentParser
public ArgumentParser()
Default constructor, using thedefault capacity (=8)
and thedefault load factor(=0.75)
for generating theHashMap
.It has the same behaviour as calling
ArgumentParser(int, float)
withArgumentParser(DEFAULT_CAPACITY, DEFAULT_LOAD_FACTOR)
orArgumentParser(8, 0.75F)
.
-
ArgumentParser
public ArgumentParser(int initialCapacity)
Generates a new instance ofthis
by using the providedinitialCapacity
. When calling this constructor, you must ensure that theinitialCapacity
you provided is as exact as possible, for avoiding an abusive use of resources (because when saving one more object than the specified one, the hole table must be rewritten completely and duplicated, which consumes lots of resources and it is not as fast as expected onHashMap
). If you do not know exactly the initial capacity, is better to useArgumentParser()
with no arguments.It has the same behaviour as calling
ArgumentParser(int, float)
withArgumentParser(initialCapacity, OPTIMUM_LOAD_FACTOR_BASED_ON_SIZE)
orArgumentParser(initialCapacity, 1.0F)
.- Parameters:
initialCapacity
- the amount of params that will be stored. It must be as exact as possible.
-
ArgumentParser
public ArgumentParser(@NotNull ArgumentParser from)
Duplicates an existingArgumentParser
copying the existing params at thefrom
object.- Parameters:
from
-ArgumentParser
from which values will be copied.
-
ArgumentParser
private ArgumentParser(int initialCapacity, float loadFactor)
Private constructor designed for generating optimizedHashMap
based on user requirements. Its visibility isprivate
because some users do not know which values must be atloadFactor
so the performance can be dramatically decreased.- Parameters:
initialCapacity
- the amount of params that will be stored. It must be as exact as possible, or using thedefault capacity (=8)
instead.loadFactor
- factor that will determine whether the hash table must be resized. It must be1.0F
when the initial capacity is exact, elsethe default load factor (=0.75)
.
-
-
Method Detail
-
equals
public boolean equals(java.lang.Object obj)
Indicates whether some other object is "equal to" this one.The
equals
method implements an equivalence relation on non-null object references:- It is reflexive: for any non-null
reference value
x
,x.equals(x)
should returntrue
. - It is symmetric: for any non-null
reference values
x
andy
,x.equals(y)
should returntrue
if and only ify.equals(x)
returnstrue
. - It is transitive: for any non-null
reference values
x
,y
, andz
, ifx.equals(y)
returnstrue
andy.equals(z)
returnstrue
, thenx.equals(z)
should returntrue
. - It is consistent: for any non-null
reference values
x
andy
, multiple invocations ofx.equals(y)
consistently returntrue
or consistently returnfalse
, provided no information used inequals
comparisons on the objects is modified. - For any non-null reference value
x
,x.equals(null)
should returnfalse
.
The
equals
method for classObject
implements the most discriminating possible equivalence relation on objects; that is, for any non-null reference valuesx
andy
, this method returnstrue
if and only ifx
andy
refer to the same object (x == y
has the valuetrue
).Note that it is generally necessary to override the
hashCode
method whenever this method is overridden, so as to maintain the general contract for thehashCode
method, which states that equal objects must have equal hash codes.- Overrides:
equals
in classjava.lang.Object
- Parameters:
obj
- the reference object with which to compare.- Returns:
true
if this object is the same as the obj argument;false
otherwise.- See Also:
hashCode()
,HashMap
- It is reflexive: for any non-null
reference value
-
hashCode
public int hashCode()
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided byHashMap
.The general contract of
hashCode
is:- Whenever it is invoked on the same object more
than once during
an execution of a Java application, the
hashCode
method must consistently return the same integer, provided no information used inequals
comparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
equals(Object)
method, then calling thehashCode
method on each of the two objects must produce the same integer result. - It is not required that if two objects
are unequal
according to the
Object.equals(java.lang.Object)
method, then calling thehashCode
method on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
As much as is reasonably practical, the hashCode method defined by class
Object
does return distinct integers for distinct objects. (The hashCode may or may not be implemented as some function of an object's memory address at some point in time.)- Overrides:
hashCode
in classjava.lang.Object
- Returns:
- a hash code value for this object.
- See Also:
Object.equals(java.lang.Object)
,System.identityHashCode(java.lang.Object)
- Whenever it is invoked on the same object more
than once during
an execution of a Java application, the
-
toString
public java.lang.String toString()
Returns a string containing a concise, human-readable description of this object.- Overrides:
toString
in classjava.lang.Object
- Returns:
- a printable representation of this object.
-
putAllParams
public void putAllParams(@NotNull ArgumentParser params)
Adds all values from other ArgumentParser to the currentarguments
.- Parameters:
params
- theArgumentParser
from which copying values.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.String paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Byte paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Short paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Integer paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Long paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Float paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Double paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Boolean paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull byte[] paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.lang.Object paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putParam
public void putParam(@NotNull java.lang.String paramName, @NotNull java.util.List<?> paramValue)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.paramValue
- the data of the param.
-
putNullParam
public void putNullParam(@NotNull java.lang.String paramName)
Adds a value to the param set.- Parameters:
paramName
- name of the param to store.
-
size
public int size()
Returns the number of params.- Returns:
- the number of parameters.
-
isEmpty
public boolean isEmpty()
Indicates whether this collection is empty.- Returns:
- true if size == 0
-
remove
public void remove(@NotNull java.lang.String paramName)
Remove a single value.- Parameters:
paramName
- the name of the value to remove
-
clear
public void clear()
Removes all params.
-
containsParam
public boolean containsParam(@NotNull java.lang.String paramName)
Returns true if this object has the named value.- Parameters:
paramName
- the value to check for- Returns:
true
if the value is present,false
otherwise
-
get
public java.lang.Object get(@NotNull java.lang.String paramName)
Gets a value. Valid value types areString
,Boolean
,Number
,byte[]
andObject
implementations.- Parameters:
paramName
- the value to get.- Returns:
- the data for the value, or
null
if the value is missing or ifnull
was previously added with the givenkey
.
-
getString
public java.lang.String getString(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
String
if present,null
if not.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anString
.
-
getInt
public int getInt(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
int
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anInteger
or it isnull
.
-
getByte
public byte getByte(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
byte
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anByte
or it isnull
.
-
getShort
public short getShort(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
short
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anShort
or it isnull
.
-
getLong
public long getLong(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
long
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anLong
or it isnull
.
-
getFloat
public float getFloat(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
float
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anFloat
or it isnull
.
-
getDouble
public double getDouble(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
double
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anDouble
or it isnull
.
-
getBoolean
public boolean getBoolean(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
boolean
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anBoolean
or it isnull
.
-
getBytes
public byte[] getBytes(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
byte
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anByte
or it isnull
.
-
getList
public java.util.List<?> getList(@NotNull java.lang.String paramName)
Obtains the param value stored under the given param name.- Parameters:
paramName
- param name from which obtaining the value.- Returns:
List
if present.- Throws:
InvalidClassTypeException
- whether the object contained atparamName
is not anList
or it isnull
.
-
getAsString
public java.lang.String getAsString(@NotNull java.lang.String paramName)
Obtains theString
representation of the value contained atparamName
.- Parameters:
paramName
- name of the param from which value is obtained.- Returns:
String
representation of the value.
-
paramsValuesSet
public java.util.Set<java.util.Map.Entry<java.lang.String,java.lang.Object>> paramsValuesSet()
Returns a set of all of the params names' and params values'.- Returns:
- a set of all of the params names' and params values'.
-
paramsNamesSet
public java.util.Set<java.lang.String> paramsNamesSet()
Returns a set of all params names'.- Returns:
- a set of all params names'.
-
clone
public final ArgumentParser clone()
Creates and returns a copy of this object. The precise meaning of "copy" may depend on the class of the object. The general intent is that, for any objectx
, the expression:x.clone() != x
x.clone().getClass() == x.getClass()
true
, but these are not absolute requirements. While it is typically the case that:x.clone().equals(x)
true
, this is not an absolute requirement.By convention, the returned object should be obtained by calling
super.clone
. If a class and all of its superclasses (exceptObject
) obey this convention, it will be the case thatx.clone().getClass() == x.getClass()
.By convention, the object returned by this method should be independent of this object (which is being cloned). To achieve this independence, it may be necessary to modify one or more fields of the object returned by
super.clone
before returning it. Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned bysuper.clone
need to be modified.The method
clone
for classObject
performs a specific cloning operation. First, if the class of this object does not implement the interfaceCloneable
, then aCloneNotSupportedException
is thrown. Note that all arrays are considered to implement the interfaceCloneable
and that the return type of theclone
method of an array typeT[]
isT[]
where T is any reference or primitive type. Otherwise, this method creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation.The class
Object
does not itself implement the interfaceCloneable
, so calling theclone
method on an object whose class isObject
will result in throwing an exception at run time.- Overrides:
clone
in classjava.lang.Object
- Returns:
- a clone of this instance.
- Throws:
java.lang.IllegalStateException
- if there was an error calling Object.clone() so it throws an CloneNotSupportedException.- See Also:
Cloneable
-
-