public class SimpleArgumentsBundle extends java.lang.Object implements ArgumentsBundle
ArgumentsBundle
, which stores all arguments simply as a String->String key-value
map and parses them into required types when accessed by respective getter methods (meaning it also fails lazily).
Does not cache parsing results, so if the same arguments are requested multiple times, it may be a good idea to query
them once and store the result in a variable.ArgumentsBundle.DimensionUnits, ArgumentsBundle.EdgeAffinity
Modifier and Type | Field and Description |
---|---|
protected static java.util.regex.Pattern |
DIMEN_VALUE_PATTERN |
protected java.util.Map<java.lang.String,java.lang.String> |
mArguments |
protected android.util.DisplayMetrics |
mMetrics |
UNITS_DP, UNITS_IN, UNITS_MM, UNITS_NULL, UNITS_NUMBER, UNITS_PERCENT, UNITS_PT, UNITS_PX, UNITS_SP
Constructor and Description |
---|
SimpleArgumentsBundle(java.util.Map<java.lang.String,java.lang.String> arguments,
android.util.DisplayMetrics metrics)
Create a new simple arguments bundle implementation from provided key->value map.
|
Modifier and Type | Method and Description |
---|---|
boolean |
equals(java.lang.Object o) |
boolean |
getBoolean(java.lang.String key,
boolean defaultValue)
Get boolean argument.
|
int |
getColor(java.lang.String key,
int defaultValue)
Get argument value as color integer with fallback to default value if argument is missing.
|
float |
getDimensionPixelExact(java.lang.String key,
float defaultValue)
Get dimension argument value as pixels with possible fallback to default value if argument is missing or invalid.
|
int |
getDimensionPixelOffset(java.lang.String key,
int defaultValue)
Get dimension argument value as pixels with possible fallback to default value if argument is missing or invalid.
|
static float |
getDimensionPixelRaw(float value,
int units,
android.util.DisplayMetrics metrics)
Convert complex dimension value of provided units into pixels.
|
int |
getDimensionPixelSize(java.lang.String key,
int defaultValue)
Get dimension argument value as pixels with possible fallback to default value if argument is missing or invalid.
|
int |
getDimensionUnits(java.lang.String key)
Get the units of a dimension argument.
|
float |
getDimensionValue(java.lang.String key,
float defaultValue)
Get raw numeric value from dimension argument disregarding units and NOT performing any conversion to pixels.
|
android.util.DisplayMetrics |
getDisplayMetrics()
Get display metrics associated with this arguments bundle.
|
int |
getEdgeAffinity(java.lang.String key,
int defaultValue)
Get argument as an
ArgumentsBundle.EdgeAffinity constant, which can be either Gravity.TOP , Gravity.LEFT ,
Gravity.RIGHT , or Gravity.BOTTOM , with fallback to default value if argument is missing or
invalid. |
float |
getFloat(java.lang.String key,
float defaultValue)
Get argument value as float with fallback to default value if argument is missing.
|
int |
getGravity(java.lang.String key,
int defaultValue)
Get argument value as a gravity value (a combination of
Gravity constants) with fallback to default value
if argument is missing. |
int |
getInt(java.lang.String key,
int defaultValue)
Get argument value as integer with fallback to default value if argument is missing.
|
java.lang.String |
getString(java.lang.String key)
Get argument value as a string.
|
java.lang.String |
getString(java.lang.String key,
java.lang.String defaultValue)
Get argument value as a string with fallback to default value if argument is missing.
|
boolean |
hasArgument(java.lang.String key)
Test if there's an argument with given key in the bundle, even if the value is
null . |
int |
hashCode() |
protected java.lang.String |
resolveArgument(java.lang.String key)
Resolves argument value from the bundle.
|
protected java.util.Map<java.lang.String,java.lang.String> mArguments
protected android.util.DisplayMetrics mMetrics
protected static java.util.regex.Pattern DIMEN_VALUE_PATTERN
public SimpleArgumentsBundle(@NonNull java.util.Map<java.lang.String,java.lang.String> arguments, @NonNull android.util.DisplayMetrics metrics)
arguments
- A collection that maps arguments to values. In this implementation both the key and the value
are raw strings, parsed into required data types as requested from the map. The values must be
already provided as parsable literal values — this implementation cannot resolve variables
or calculate expressions.metrics
- Display metrics associated with this arguments bundle, required so that dimension values (dp, sp
etc) can be properly resolved.public android.util.DisplayMetrics getDisplayMetrics()
getDisplayMetrics
in interface ArgumentsBundle
public boolean hasArgument(java.lang.String key)
ArgumentsBundle
null
.hasArgument
in interface ArgumentsBundle
key
- argument keyprotected java.lang.String resolveArgument(java.lang.String key)
getXxx()
methods — subclasses should override this
method if additional processing is required (e.g. lazy variable dereference, expression evaluation etc).key
- key of the argument whose value to resolvepublic java.lang.String getString(java.lang.String key)
resolveArgument(String)
getString
in interface ArgumentsBundle
key
- argument keypublic java.lang.String getString(java.lang.String key, @Nullable java.lang.String defaultValue)
getString
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valuepublic int getInt(java.lang.String key, int defaultValue)
ArgumentsBundle
getInt
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valuepublic float getFloat(java.lang.String key, float defaultValue)
ArgumentsBundle
getFloat
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valuepublic boolean getBoolean(java.lang.String key, boolean defaultValue)
ArgumentsBundle
arg
are identical to arg=true
.getBoolean
in interface ArgumentsBundle
key
- argument keydefaultValue
- value if argument is not presentpublic int getColor(java.lang.String key, int defaultValue)
ArgumentsBundle
getColor
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valuepublic int getGravity(java.lang.String key, int defaultValue)
Gravity
constants) with fallback to default value
if argument is missing. Does a quick and rough parsing of the raw string for containing constant words like
top
or center_vertical
getGravity
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valueArgumentsBundle.getEdgeAffinity(String, int)
public int getEdgeAffinity(java.lang.String key, int defaultValue)
ArgumentsBundle
ArgumentsBundle.EdgeAffinity
constant, which can be either Gravity.TOP
, Gravity.LEFT
,
Gravity.RIGHT
, or Gravity.BOTTOM
, with fallback to default value if argument is missing or
invalid.getEdgeAffinity
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback valueArgumentsBundle.getGravity(String, int)
public int getDimensionUnits(java.lang.String key)
getDimensionUnits
in interface ArgumentsBundle
key
- argument keyArgumentsBundle.UNITS_NULL
if the argument is null or missingpublic float getDimensionValue(java.lang.String key, float defaultValue)
getDimensionValue
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback value in pixelsgetDimensionPixelRaw(float, int, DisplayMetrics)
public float getDimensionPixelExact(java.lang.String key, float defaultValue)
ArgumentsBundle.getDimensionPixelSize(String, int)
and ArgumentsBundle.getDimensionPixelSize(String, int)
, this method
doesn't perform any rounding. Note: this method requires that DisplayMetrics
object is injected in this config.getDimensionPixelExact
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback value in pixelsgetDimensionPixelRaw(float, int, DisplayMetrics)
public int getDimensionPixelOffset(java.lang.String key, int defaultValue)
ArgumentsBundle.getDimensionPixelSize(String, int)
, this method is expected to round the raw value down to
the closest integer. Note: this method requires that DisplayMetrics
object is injected in this config.getDimensionPixelOffset
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback value in pixelsgetDimensionPixelRaw(float, int, DisplayMetrics)
public int getDimensionPixelSize(java.lang.String key, int defaultValue)
ArgumentsBundle.getDimensionPixelOffset(String, int)
}, this method is expected to round the raw value up or
down to the closest integer by common rules, and must ensure the result is at least 1px if original value is
not 0. Note: this method requires that DisplayMetrics
object is injected in this config.getDimensionPixelSize
in interface ArgumentsBundle
key
- argument keydefaultValue
- fallback value in pixelsgetDimensionPixelRaw(float, int, DisplayMetrics)
public static float getDimensionPixelRaw(float value, int units, android.util.DisplayMetrics metrics)
value
- raw dimension value, e.g. 24f
units
- dimension units, one of ArgumentsBundle.UNITS_PX
, ArgumentsBundle.UNITS_DP
, ArgumentsBundle.UNITS_SP
, ArgumentsBundle.UNITS_PT
, ArgumentsBundle.UNITS_IN
, ArgumentsBundle.UNITS_MM
, ArgumentsBundle.UNITS_NUMBER
, ArgumentsBundle.UNITS_NULL
, or
ArgumentsBundle.UNITS_PERCENT
metrics
- display metrics to convert complex dimension types that depend on density (dp, sp etc) into
pixels, can be null if type is one of ArgumentsBundle.UNITS_PX
, ArgumentsBundle.UNITS_PERCENT
, ArgumentsBundle.UNITS_NUMBER
, or ArgumentsBundle.UNITS_NULL
public boolean equals(java.lang.Object o)
equals
in class java.lang.Object
public int hashCode()
hashCode
in class java.lang.Object