Class BagObject

java.lang.Object
us.irdev.bedrock.bag.Bag
us.irdev.bedrock.bag.BagObject
All Implemented Interfaces:
Selectable<BagObject>

public class BagObject extends Bag implements Selectable<BagObject>
A collection of text-based values store in key/value pairs (maintained in a sorted array).
  • Constructor Details

    • BagObject

      public BagObject()
      Create a new BagObject with a default underlying storage size.
    • BagObject

      public BagObject(int size)
      Create a new BagObject with hint for the underlying storage size.
      Parameters:
      size - The expected number of elements in the BagObject, treated as a hint to optimize memory allocation. If additional elements are stored, the BagObject will revert to normal allocation behavior.
    • BagObject

      public BagObject(BagObject bagObject)
      Create a new BagObject as deep copy of another BagObject
  • Method Details

    • getCount

      public int getCount()
      Return the number of elements stored in the BagObject.
      Returns:
      the count of elements in the underlying store. This is distinct from the capacity of the underlying store.
    • getObject

      public Object getObject(String key)
      Return an object stored at the requested key value. The key may be a simple name, or it may be a path (with keys separated by "/") to create a hierarchical "bedrock-of-bags" that is indexed recursively.

      Using a binary search of the underlying store, finds where the first component of the path should be and returns it.

      Specified by:
      getObject in class Bag
      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      Returns:
      The indexed element (if found), or null
    • put

      public BagObject put(String key, Object object)
      Store an object at the requested key value. The key may be a simple name, or it may be a path (with keys separated by "/") to create a hierarchical "bedrock-of-bags" that is indexed recursively.

      Using a binary search of the underlying store, finds where the first component of the path should be. If it does not already exist, it is created (recursively in the case of a path), and the underlying store is shifted to make a space for it. The shift might cause the underlying store to be resized if there is insufficient room.

      Note that null values for the element are NOT stored at the leaf of the tree denoted by a path, as returning null from getObject would be indistinguishable from a call to getObject with an unknown key. This check is performed before the tree traversal, so the underlying store will NOT contain the path after an attempt to add a null value.

      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      object - The element to store.
      Returns:
      The BagObject, so that operations can be chained together.
    • open

      public static BagObject open(String key, Object object)
      Create a new BagObject and "put" the object using its key value. The key may be a simple name, or it may be a path (with keys separated by "/") to create a hierarchical "bedrock-of-bags" that is indexed recursively.
      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      object - The element to store.
      Returns:
      The newly created BagObject.
    • add

      public BagObject add(String key, Object object)
      Add an object to a BagArray stored at the requested key. The key may be a simple name, or it may be a path (with keys separated by "/") to create a hierarchical "bedrock-of-bags" that is indexed recursively. If the key does not already exist a non-null value will be stored as a bare value, just as if "put" had been called. If it does exist, and is not already an array or the stored value is null, then a new array will be created to store any existing values and the requested element.

      Using a binary search of the underlying store, finds where the first component of the path should be. If it does not already exist, it is created (recursively in the case of a path), and the underlying store is shifted to make a space for it. The shift might cause the underlying store to be resized if there is insufficient room.

      Note that null values for the BagArray ARE stored per the design decision for arrays.

      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      object - The element to store.
      Returns:
      The BagObject, so that operations can be chained together.
    • remove

      public BagObject remove(String key)
      Remove an object stored at the requested key. The key may be a simple name, or it may be a path (with keys separated by "/") to create a hierarchical "bedrock-of-bags" that is indexed recursively.

      Using a binary search of the underlying store, finds where the element mapped to the key should be, and removes it. If the element doesn't exist, nothing happens. If the element is removed, the underlying store is shifted to close the space where it was. removing elements will never cause the underlying store to shrink.

      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      Returns:
      The BagObject, so that operations can be chained together.
    • has

      public boolean has(String key)
      Return whether or not the requested key or path is present in the BagObject or hierarchical "bedrock-of-bags"
      Parameters:
      key - A string value used to index the element, using "/" as separators, for example: "com/brettonw/bedrock/key".
      Returns:
      A boolean value, true if the key is present in the underlying store. Note that null values are not stored (design decision), so this equivalent to checking for null.
    • keys

      public String[] keys()
      Returns an array of the keys contained in the underlying container. it does not enumerate the container and all of its children.
      Returns:
      The keys in the underlying map as an array of Strings.
    • toString

      public String toString(String format)
      Specified by:
      toString in class Bag
      Parameters:
      format -
      Returns:
    • select

      public BagObject select(SelectKey selectKey)
      Specified by:
      select in interface Selectable<BagObject>
      Parameters:
      selectKey -
      Returns:
    • merge

      public static BagObject merge(BagObject... bagObjects)