ResourcesHolder

Created by Anggrayudi on 11/03/2016.

Since version 0.0.5 the uses of this class is diverted. You don't need to avoid Java reflection by storing the internal resources you just retrieved to ResourcesHolder and send them via Broadcast, because this version does not use Java reflection anymore. Instead, you can use this class to store any objects you might need in other classes (like passing objects from activity to service) and send them via Broadcast.

Obsolete documentation (v0.0.4 and lower) A class that holds internal resources which are retrieved by InternalAccessor . We need to hold the resources we just retrieved, because InternalAccessor uses reflection, and Java reflection may slow down user's machine performance. Using reflection every time you reflect the same value is not a good practice. So, use this class instead.

Also, you can use this class to holds non-internal resources, according to your needs.

An example to use this class is:

    ResourcesHolder holder = new ResourcesHolder()
             .putString("my_string_key", InternalAccessor.getString("accept"))
             .putResourceId("my_res_id_key", InternalAccessor.getResourceId("drawable", "loading_tile_android");
    // Retrieve the values from another place
    holder.getString("my_string_key");
    holder.getResourceId("my_res_id_key");
    // If you plan to send 'holder' to another class via BroadcastReceiver or LocalBroadcastManager
    Intent intent = new Intent(ResourcesHolder.ACTION_SEND_RESOURCES_HOLDER);
    intent.putExtra("holder", holder);
    // send via LocalBroadcastManager
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
    // send via BroadcastReceiver
    sendBroadcast(intent);
    // Instead, you can do this in simple way
    holder.sendBroadcast(this, "holder");
    // or with this
    holder.sendViaLocalBroadcastManager(this, "holder");

open class ResourcesHolder : Parcelable

Constructors

ResourcesHolder
Link copied to clipboard
open fun ResourcesHolder()

Functions

clear
Link copied to clipboard

Clear all values that is saved in this class.

open fun clear()
containsKey
Link copied to clipboard
open fun containsKey(key: String): Boolean
describeContents
Link copied to clipboard
abstract fun describeContents(): Int
open fun describeContents(): Int
getAsBoolean
Link copied to clipboard
open fun getAsBoolean(key: String): Boolean
getAsBooleanArray
Link copied to clipboard
open fun getAsBooleanArray(key: String): Array<Boolean>
getAsByte
Link copied to clipboard
open fun getAsByte(key: String): Byte
getAsByteArray
Link copied to clipboard
open fun getAsByteArray(key: String): Array<Byte>
getAsChar
Link copied to clipboard
open fun getAsChar(key: String): Char
getAsCharArray
Link copied to clipboard
open fun getAsCharArray(key: String): Array<Char>
getAsDouble
Link copied to clipboard
open fun getAsDouble(key: String): Double
getAsDoubleArray
Link copied to clipboard
open fun getAsDoubleArray(key: String): Array<Double>
getAsFloat
Link copied to clipboard

Some resources use this data type, they are dimension and fraction. You also can get any float here.

open fun getAsFloat(key: String): Float
getAsFloatArray
Link copied to clipboard
open fun getAsFloatArray(key: String): Array<Float>
getAsIntArray
Link copied to clipboard
open fun getAsIntArray(key: String): Array<Int>
getAsInteger
Link copied to clipboard

Some resources use this data type, they are color, resourceId and integerResource. You also can get any integer here.

open fun getAsInteger(key: String): Int
getAsLong
Link copied to clipboard
open fun getAsLong(key: String): Long
getAsLongArray
Link copied to clipboard
open fun getAsLongArray(key: String): Array<Long>
getAsObject
Link copied to clipboard

Get any values that is stored as Object, i.e. for non-primitive data types. They can be an array or an instance from a class.

open fun getAsObject(key: String): Any
getAsShort
Link copied to clipboard
open fun getAsShort(key: String): Short
getAsShortArray
Link copied to clipboard
open fun getAsShortArray(key: String): Array<Short>
getAsString
Link copied to clipboard
open fun getAsString(key: String): String
getAsStringArray
Link copied to clipboard
open fun getAsStringArray(key: String): Array<String>
keySet
Link copied to clipboard

Returns a set of all of the keys

open fun keySet(): Set<String>
printAll
Link copied to clipboard

Print all values that is saved via put*(Key, Value) method.

open fun printAll()
put
Link copied to clipboard
open fun put(key: String, value: Boolean): ResourcesHolder
open fun put(key: String, value: Byte): ResourcesHolder
open fun put(key: String, value: Char): ResourcesHolder
open fun put(key: String, value: Double): ResourcesHolder
open fun put(key: String, value: Float): ResourcesHolder
open fun put(key: String, value: Int): ResourcesHolder
open fun put(key: String, value: String): ResourcesHolder
open fun put(key: String, value: Long): ResourcesHolder
open fun put(key: String, value: Short): ResourcesHolder

An additional method to holds an Object, for non-primitive data types. For example Drawable, XmlResourceParser, etc. To retrieve it back to the original object, cast it to its origin class. You also can store an array here. An example of using this method is:

 List<String> strs = new ArrayList<>();
 strs.add("FOO");
 strs.add("BXY");
 Person[] persons = 
{
new Person(), null, new Person()
}
;
 XmlResourceParser parser = getResources().getXml(R.xml.preferences);
 ResourcesHolder holder = new ResourcesHolder()
     .put("arrayInt", new int[]
{
9, 8, 5
}
)
     .put("arrayStringList", strs)
     .put("arrayPerson", persons)
     .put("my_parser", parser);
 // Then, retrieve them back to the original form
 int[] ints = holder.getAsIntArray("arrayInt");
 //noinspection unchecked
 strs = (List<String>) holder.getAsObject("arrayStringList");
 // cast them to the original class
 persons = (Person[]) holder.getAsObject("arrayPerson");
 parser = (XmlResourceParser) holder.getAsObject("my_parser");

open fun put(key: String, object: Any): ResourcesHolder
remove
Link copied to clipboard

Remove a value by key.

open fun remove(key: String)
sendBroadcast
Link copied to clipboard

This method equals with sendBroadcast . Notice that the Intent uses ACTION_SEND_RESOURCES_HOLDER action. So that, register your BroadcastReceiver with that action to make it able to receive the extra from Intent.

See sendViaLocalBroadcastManager

open fun sendBroadcast(context: Context, key: String)
sendViaLocalBroadcastManager
Link copied to clipboard

Send ResourcesHolder instance to another class via LocalBroadcastManager. Make sure that the BroadcastReceiver is registered with ACTION_SEND_RESOURCES_HOLDER action.

open fun sendViaLocalBroadcastManager(context: Context, key: String)
size
Link copied to clipboard
open fun size(): Int
sort
Link copied to clipboard

Sort ResourcesHolder by keys.

open fun sort(descending: Boolean)
valueSet
Link copied to clipboard

Returns a set of all of the keys and values

open fun valueSet(): Set<Map.Entry<String, Any>>
writeToParcel
Link copied to clipboard
abstract fun writeToParcel(p: Parcel, p1: Int)
open fun writeToParcel(dest: Parcel, flags: Int)

Properties

ACTION_SEND_RESOURCES_HOLDER
Link copied to clipboard
val ACTION_SEND_RESOURCES_HOLDER: String
CREATOR
Link copied to clipboard
val CREATOR: Parcelable.Creator<ResourcesHolder>