Package ortus.boxlang.runtime.types
Class Struct
java.lang.Object
ortus.boxlang.runtime.types.Struct
- All Implemented Interfaces:
Serializable
,Map<Key,
,Object> IReferenceable
,IStruct
,IType
,IListenable
- Direct Known Subclasses:
BaseScope
,ImmutableStruct
This type provides the core map class for Boxlang. Structs are highly versatile and are used for organizing and managing related data.
Types of Structs in BoxLang:
* Basic Structs: These are the basic structures where each key is associated with a single value. Keys are case-insensitive and can be strings or symbols.
* Nested Structs: Structs can contain other structs as values, allowing for a hierarchical organization of data.
* Case-Sensitive Structs: By default, BoxLang structs are case-insensitive. However, you can create case-sensitive structs if needed.
* Ordered Structs: This implementation of a Struct maintains keys in the order they were added.
* Sorted Structs: This implementation of a Struct maintains keys in specified sorted order.
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface ortus.boxlang.runtime.types.IStruct
IStruct.TYPES
-
Field Summary
FieldsModifier and TypeFieldDescriptionMetadata objectstatic final IStruct
An immutable singleton empty structprotected static final int
In general, a common approach is to choose an initial capacity that is a power of two.static final Comparator
<Key> A pre-made comparator to use with a sorted struct to sort longest keys first, and shortest lastThe wrapped map used in the implementationFields inherited from interface ortus.boxlang.runtime.types.meta.IListenable
ALL_KEYS
-
Constructor Summary
ConstructorsConstructorDescriptionStruct()
Create a default structStruct
(Comparator<Key> comparator) Create a sorted struct with the passed objectConstruct a struct from the keys/values in your map.Struct
(Map<Key, Object> map, IStruct.TYPES type) Construct a struct from a map.Struct
(IStruct.TYPES type) ConstructorStruct
(IStruct.TYPES type, Map<? extends Object, ? extends Object> map) Construct a struct of a specific type from an existing map -
Method Summary
Modifier and TypeMethodDescriptionvoid
Copies all of the mappings from the specified map to this map (optional operation).assign
(IBoxContext context, Key key, Object value) Assign a value to a keyasString()
Represent as string, or throw exception if not possiblevoid
clear()
Removes all of the mappings from this map (optional operation).boolean
containsKey
(Object key) Returnstrue
if this map contains a mapping for the specifiedKey
boolean
containsKey
(String key) Returnstrue
if this map maps one or more keys using a String keyboolean
containsKey
(Key key) Returnstrue
if this map contains a mapping for the specifiedKey
boolean
containsValue
(Object value) Returnstrue
if this map maps has the specified valuedereference
(IBoxContext context, Key key, Boolean safe) Dereference this object by a key and return the value, or throw exceptiondereferenceAndInvoke
(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method) using positional argumentsdereferenceAndInvoke
(IBoxContext context, Key name, Map<Key, Object> namedArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method)entrySet()
Returns aSet
view of the mappings contained in this map.boolean
Verifies equality with the following rules: - Same object - Super classstatic IStruct
Static helper to create a struct from an existing mapstatic IStruct
fromMap
(IStruct.TYPES type, Map<Object, Object> map) Static helper to construct a struct of a specific type and an existing mapReturns the value to which the specified Key is mappedReturns the value to which the specified Key is mappedGet the BoxMetadata object for this structgetKeys()
Get an array list of all the keys in the structGet an array list of all the keys in the structgetOrDefault
(String key, Object defaultValue) Get key, with default value if not foundgetOrDefault
(Key key, Object defaultValue) Get key, with default value if not foundReturns the value of the key safely, nulls will be wrapped in a NullValue still.getType()
Get the type of structGet the wrapped map used in the implementationint
hashCode()
Struct HashcodeReturns a boolean as to whether the struct instance is case sensitiveboolean
isEmpty()
Returnstrue
if this map contains no key-value mappings.Returns a boolean as to whether this is a soft-referenced structkeySet()
Returns aSet
view of the keys contained in this map.static IStruct
Create a linked struct from a list of values.static IStruct
Create a struct from a list of values.Set a value in the struct by a string key, which we auto-convert to a Key objectSet a value in the struct by a Key objectvoid
Copies all of the mappings from the specified map to this map (optional operation).putIfAbsent
(String key, Object value) Put a value in the struct if the key doesn't existputIfAbsent
(Key key, Object value) Put a value in the struct if the key doesn't existvoid
registerChangeListener
(Key key, IChangeListener listener) void
registerChangeListener
(IChangeListener listener) -------------------------------------------------------------------------- IListenable Interface Methods --------------------------------------------------------------------------Remove a value from the struct by a Key objectRemove a value from the struct by a Key objectRemove a value from the struct by a Key objectvoid
removeChangeListener
(Key key) int
size()
Returns the number of key-value mappings in this map.static IStruct
sortedOf
(Comparator<Key> comparator, Object... values) Create a sorted struct from a list of values and the passed comparator.static IStruct
Create a sorted struct from an existing map.Helper to make the struct immutabletoString()
Convert the struct to a human-readable string, usually great for debugging Remember structs have no order except their internal hash codeConvert the struct to a human-readable string, usually great for debugging Remember structs have no order except their internal hash codestatic Object
unWrapNull
(Object value) Unwrap null values from the NullValue classvalues()
Returns aCollection
view of the values contained in this map.wrapAssignment
(Object value) Wraps the assignment valueWrap null values in an instance of the NullValue classMethods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface ortus.boxlang.runtime.types.IStruct
getAs, getAsArray, getAsAttempt, getAsBoolean, getAsBoxInterface, getAsClassRunnable, getAsDateTime, getAsDouble, getAsFunction, getAsInteger, getAsKey, getAsLong, getAsNumber, getAsOptional, getAsQuery, getAsStream, getAsString, getAsStruct, getAsXML
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, forEach, getOrDefault, merge, remove, replace, replace, replaceAll
-
Field Details
-
KEY_LENGTH_LONGEST_FIRST_COMPARATOR
A pre-made comparator to use with a sorted struct to sort longest keys first, and shortest last -
EMPTY
An immutable singleton empty struct -
$bx
Metadata object -
wrapped
The wrapped map used in the implementation -
INITIAL_CAPACITY
protected static final int INITIAL_CAPACITYIn general, a common approach is to choose an initial capacity that is a power of two. For example, 16, 32, 64, etc. This is because ConcurrentHashMap uses power-of-two-sized hash tables, and using a power-of-two capacity can lead to better distribution of elements in the table.- See Also:
-
-
Constructor Details
-
Struct
Constructor- Parameters:
type
- The type of struct to create: DEFAULT, LINKED, SORTED- Throws:
BoxRuntimeException
- If an invalid type is specified: DEFAULT, LINKED, SORTED
-
Struct
public Struct()Create a default struct -
Struct
Create a sorted struct with the passed object- Parameters:
comparator
- The comparator to use
-
Struct
Construct a struct from a map. This wraps the original map. Use theStruct( Type type, Map<? extends Object, ? extends Object> map )
method and supply an explicit type to have this struct created with a copy of all the keys/values in your map.- Parameters:
map
- The map to create the struct fromtype
- The type of struct to create: DEFAULT, LINKED, SORTED
-
Struct
Construct a struct from the keys/values in your map.- Parameters:
map
- The map to create the struct from
-
Struct
Construct a struct of a specific type from an existing map- Parameters:
type
- The type of struct to create: DEFAULT, LINKED, SORTEDmap
- The map to create the struct from
-
-
Method Details
-
fromMap
Static helper to create a struct from an existing map- Parameters:
map
- The map to create the struct from- Returns:
- The struct created from the map
-
fromMap
Static helper to construct a struct of a specific type and an existing map- Parameters:
type
- The type of struct to create: DEFAULT, LINKED, SORTEDmap
- The map to create the struct from- Returns:
- The struct created from the map with the specified type
-
of
Create a struct from a list of values. The values must be in pairs, key, value, key, value, etc.- Parameters:
values
- The values to create the struct from- Returns:
- The struct
-
linkedOf
Create a linked struct from a list of values. The values must be in pairs, key, value, key, value, etc.- Parameters:
values
- The values to create the struct from- Returns:
- The linked struct
-
sortedOf
Create a sorted struct from a list of values and the passed comparator. The values must be in pairs, key, value, key, value, etc.- Parameters:
comparator
- The comparator to usevalues
- The values to create the struct from- Returns:
- The sorted struct
-
sortedOf
Create a sorted struct from an existing map.- Parameters:
comparator
- The comparator to usemap
- An existing map to create the sorted struct from- Returns:
- The sorted struct
-
size
public int size()Returns the number of key-value mappings in this map. If the map contains more thanInteger.MAX_VALUE
elements, returnsInteger.MAX_VALUE
. -
isEmpty
public boolean isEmpty()Returnstrue
if this map contains no key-value mappings. -
containsKey
Returnstrue
if this map contains a mapping for the specifiedKey
- Specified by:
containsKey
in interfaceIStruct
- Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified
-
containsKey
Returnstrue
if this map contains a mapping for the specifiedKey
- Specified by:
containsKey
in interfaceMap<Key,
Object> - Parameters:
key
- key whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified
-
containsKey
Returnstrue
if this map maps one or more keys using a String key- Specified by:
containsKey
in interfaceIStruct
- Parameters:
key
- The string key to look for. Automatically converted to Key object- Returns:
true
if this map contains a mapping for the specified
-
containsValue
Returnstrue
if this map maps has the specified value- Specified by:
containsValue
in interfaceMap<Key,
Object> - Parameters:
value
- value whose presence in this map is to be tested- Returns:
true
if this map contains a mapping for the specified value
-
get
Returns the value to which the specified Key is mapped -
get
Returns the value to which the specified Key is mapped -
getOrDefault
Get key, with default value if not found- Specified by:
getOrDefault
in interfaceIStruct
- Parameters:
key
- The key to look fordefaultValue
- The default value to return if the key is not found- Returns:
- The value of the key
-
getOrDefault
Get key, with default value if not found- Specified by:
getOrDefault
in interfaceIStruct
- Parameters:
key
- The key to look fordefaultValue
- The default value to return if the key is not found- Returns:
- The value of the key
-
getRaw
Returns the value of the key safely, nulls will be wrapped in a NullValue still. -
put
Set a value in the struct by a Key object -
put
Set a value in the struct by a string key, which we auto-convert to a Key object -
putIfAbsent
Put a value in the struct if the key doesn't exist- Specified by:
putIfAbsent
in interfaceIStruct
- Specified by:
putIfAbsent
in interfaceMap<Key,
Object> - Parameters:
key
- The key to setvalue
- The value to set- Returns:
- The previous value of the key, or null if not found
-
putIfAbsent
Put a value in the struct if the key doesn't exist- Specified by:
putIfAbsent
in interfaceIStruct
- Parameters:
key
- The String key to setvalue
- The value to set- Returns:
- The previous value of the key, or null if not found
-
remove
Remove a value from the struct by a Key object -
remove
Remove a value from the struct by a Key object -
remove
Remove a value from the struct by a Key object -
putAll
Copies all of the mappings from the specified map to this map (optional operation). It expects the specific key and object generics. -
addAll
Copies all of the mappings from the specified map to this map (optional operation). This method will automatically convert the keys to Key objects -
clear
public void clear()Removes all of the mappings from this map (optional operation). -
keySet
Returns aSet
view of the keys contained in this map. -
values
Returns aCollection
view of the values contained in this map. -
entrySet
Returns aSet
view of the mappings contained in this map. -
equals
Verifies equality with the following rules: - Same object - Super class -
hashCode
public int hashCode()Struct Hashcode -
toString
Convert the struct to a human-readable string, usually great for debugging Remember structs have no order except their internal hash code -
toStringWithCase
Convert the struct to a human-readable string, usually great for debugging Remember structs have no order except their internal hash code- Specified by:
toStringWithCase
in interfaceIStruct
- Returns:
- The string representation of the struct using the format {key=value, key=value}
-
asString
Represent as string, or throw exception if not possible -
getBoxMeta
Get the BoxMetadata object for this struct- Specified by:
getBoxMeta
in interfaceIType
- Returns:
- The object for this struct
-
getType
Get the type of struct -
isCaseSensitive
Returns a boolean as to whether the struct instance is case sensitive- Specified by:
isCaseSensitive
in interfaceIStruct
-
isSoftReferenced
Returns a boolean as to whether this is a soft-referenced struct- Specified by:
isSoftReferenced
in interfaceIStruct
-
toImmutable
Helper to make the struct immutable -
assign
Assign a value to a key- Specified by:
assign
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofkey
- The key to assignvalue
- The value to assign- Returns:
- The value that was assigned
-
dereference
Dereference this object by a key and return the value, or throw exception- Specified by:
dereference
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofkey
- The key to dereferencesafe
- Whether to throw an exception if the key is not found- Returns:
- The requested object
-
dereferenceAndInvoke
public Object dereferenceAndInvoke(IBoxContext context, Key name, Object[] positionalArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method) using positional arguments- Specified by:
dereferenceAndInvoke
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The key to dereferencepositionalArguments
- The positional arguments to pass to the invokablesafe
- Whether to throw an exception if the key is not found- Returns:
- The requested object
-
dereferenceAndInvoke
public Object dereferenceAndInvoke(IBoxContext context, Key name, Map<Key, Object> namedArguments, Boolean safe) Dereference this object by a key and invoke the result as an invokable (UDF, java method)- Specified by:
dereferenceAndInvoke
in interfaceIReferenceable
- Parameters:
context
- The context we're executing inside ofname
- The name of the key to dereference, which becomes the method namenamedArguments
- The arguments to pass to the invokablesafe
- If true, return null if the method is not found, otherwise throw an exception- Returns:
- The requested return value or null
-
getFunctionContextThisClassForInvoke
-
getFunctionContextThisInterfaceForInvoke
-
wrapNull
Wrap null values in an instance of the NullValue class- Parameters:
value
- The value to wrap- Returns:
- The wrapped value
-
wrapAssignment
Wraps the assignment value- Parameters:
value
- The object to wrap ( or not )
-
getKeys
Get an array list of all the keys in the struct -
getKeysAsStrings
Get an array list of all the keys in the struct- Specified by:
getKeysAsStrings
in interfaceIStruct
- Returns:
- An array list of all the keys in the struct
-
unWrapNull
Unwrap null values from the NullValue class- Parameters:
value
- The value to unwrap- Returns:
- The unwrapped value which can be null
-
getWrapped
Get the wrapped map used in the implementation- Specified by:
getWrapped
in interfaceIStruct
-
registerChangeListener
-------------------------------------------------------------------------- IListenable Interface Methods --------------------------------------------------------------------------- Specified by:
registerChangeListener
in interfaceIListenable
-
registerChangeListener
- Specified by:
registerChangeListener
in interfaceIListenable
-
removeChangeListener
- Specified by:
removeChangeListener
in interfaceIListenable
-