Package org.qdl_lang.variables
Class QDLStem
- java.lang.Object
-
- org.qdl_lang.variables.QDLStem
-
- All Implemented Interfaces:
Serializable
,Map<QDLKey,QDLValue>
- Direct Known Subclasses:
StemVariable
public class QDLStem extends Object implements Map<QDLKey,QDLValue>, Serializable
Created by Jeff Gaynor
on 6/27/22 at 6:33 AM- See Also:
- Serialized Form
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
QDLStem.ARGetkeys
static class
QDLStem.KeyRankMap
A map that is keyed by an integer and has lists of indices as values.protected static class
QDLStem.OrderedIndexEntry
static class
QDLStem.SizeOf
class
QDLStem.ValueIterator
-
Field Summary
Fields Modifier and Type Field Description static String
STEM_ENTRY_CONNECTOR
static String
STEM_INDEX_MARKER
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
addAll(QDLStem qdlStem)
void
addList(List list)
Adds a list of objects to this stem, giving them indices appropriate indices This is mostly a convenience for people writing in java to create lists programatically.protected QDLStem.KeyRankMap
allKeys2()
QDLStem
almostUnique()
This almost returns all the unique elements.void
clear()
QDLStem
clone()
Make a shallow copy of this stem variable.QDLStem
commonKeys(QDLStem arg2)
boolean
containsKey(Object k)
boolean
containsValue(Object value)
protected QDLStem
convertKeyByRank(List<List> list)
Gets a list of lists, e.g.QDLStem
dim()
Set<Map.Entry<QDLKey,QDLValue>>
entrySet()
QDLStem
excludeKeys(QDLStem keyList)
QDLStem
fromJSON(net.sf.json.JSON json)
QDLStem
fromJSON(net.sf.json.JSONArray array, boolean convert, int type)
QDLStem
fromJSON(net.sf.json.JSONObject jsonObject)
QDLStem
fromJSON(net.sf.json.JSONObject jsonObject, boolean convertVars, int type)
Populate this from a JSON object.QDLValue
get(Long key)
QDLValue
get(Object key)
QDLValue
get(String key)
IndexList
get(IndexList indexList, boolean strictMatching)
QDLValue
get(StemMultiIndex w)
Note that theQDLValue
get(StemPath<StemPathEntry> stemPath)
QDLValue
get(QDLKey key)
Convenience setter to put an object.Boolean
getBoolean(Long key)
Used in unit tests only.Boolean
getBoolean(String key)
QDLValue
getByMultiIndex(String index)
This will take a stem with embedded .BigDecimal
getDecimal(Long key)
QDLValue
getDefaultValue()
If this is set, then any get with no key will return this value.Long
getLong(Long key)
Long
getLong(String key)
QDLList<? extends QDLValue>
getQDLList()
QDLMap
getQDLMap()
Long
getRank()
QDLStem
getStem(Long key)
QDLStem
getStem(String key)
String
getString(Long key)
String
getString(String key)
boolean
hasDefaultValue()
QDLStem
hasKey(QDLStem keyList)
Modern successor to the deprecatedhasKeys(QDLStem)
.QDLStem
hasKeys(QDLStem keyList)
Takes a stem and returns a boolean list conformable to the argument.boolean
hasValue(QDLValue x)
QDLStem
includeKeys(QDLStem keyList)
QDLStem
indicesByRank()
Returns a flat list of all indices at a given rank.QDLStem
indicesByRank(Long rank)
Return the indices of a given rank for this stem.protected void
indicesByRank(QDLStem v, List list, QDLStem.KeyRankMap keyRankMap)
String
inputForm()
String
inputForm(int indentFactor)
String
inputForm(int indentFactor, String currentIndent)
boolean
isEmpty()
boolean
isList()
static boolean
isLongIndex(String key)
Does a regex on the index to see if it is really a long.protected boolean
isVar(String var)
QDLStem
keysByAxis(Long axis)
Get by axis.StemKeys
keySet()
return an enumeration (set) of ALL keys in order, starting with the list indices then the keys.void
listAdd(QDLValue value)
Add the single object to the list in this stem.void
listAppend(QDLStem stem)
Append the list elements of the stem to this list.void
listCopy(long startIndex, long length, QDLStem target, long insertIndex)
Copies the elements from this list to the target list.void
listInsertAt(long startIndex, long length, QDLStem target, long insertIndex)
Insert the currentQDLStem
listSubset(long startIndex)
Insert the whole argument in to the current stem, re-adjusting indices.QDLStem
listSubset(long startIndex, long length)
QDLStem
mask(QDLStem stem2)
protected void
myPut(QDLValue index, QDLValue value)
IndexList
newGet(IndexList indexList, boolean strictMatching)
Strict matching is used at the last resolution of the stem.QDLStem
newInstance()
QDLStem
newInstance(Long count, QDLValue[] fillList)
QDLValue
put(int index, Object value)
QDLValue
put(int index, QDLValue value)
QDLValue
put(String key, QDLValue value)
QDLValue
put(QDLKey qdlKey, Object object)
Convenience method that converts second argument to aQDLValue
as needed.QDLValue
put(QDLKey qdlKey, QDLValue qdlValue)
void
putAll(Map<? extends QDLKey,? extends QDLValue> map)
This does not add the list elements because that causes issues with the contract for maps.QDLValue
remove(Long key)
QDLValue
remove(Object kk)
QDLValue
remove(String key)
boolean
remove(IndexList indexList)
void
remove(StemMultiIndex w)
boolean
removeAllByValue(QDLValue c, boolean reorderLists)
boolean
removeAllByValue(QDLValue c, boolean reorderLists, boolean listProcessed)
boolean
removeAllByValues(Collection<QDLValue> c, boolean reorderLists)
Removes every value in the collection from everywhere in the stem.void
renameKeys(QDLStem newKeys, boolean overWriteKeys)
void
set(IndexList indexList, QDLValue value)
void
set(StemMultiIndex w, QDLValue value)
void
setDefaultValue(Object defaultValue)
Do conversion toQDLValue
.void
setDefaultValue(QDLValue defaultValue)
void
setQDLList(QDLList qdlList)
void
setQDLMap(QDLMap qdlMap)
int
size()
As per usual JavaMap.size()
contract, return axis 0 countlong
size(int axis)
Return count down to a given axis.net.sf.json.JSON
toJSON()
Converts this to a JSON object.net.sf.json.JSON
toJSON(boolean escapeNames, int codecType)
String
toString()
protected String
toString(boolean showDefaultValues)
Whether or not to show default values.String
toString(int indentFactor)
String
toString(int indentFactor, String currentIndent)
Used as a utility, this prints with an indent relative to a current indent.QDLStem
union(QDLStem... stemVariables)
This will return a new stem consisting of this stem and the union of all the stem arguments.QDLSet<QDLValue>
values()
Be aware that this creates an actual set so it reads every item.QDLSet
valueSet()
Iterator
valuesIterator()
A specific iterator for the values of this stem.-
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface java.util.Map
compute, computeIfAbsent, computeIfPresent, equals, forEach, getOrDefault, hashCode, merge, putIfAbsent, remove, replace, replace, replaceAll
-
-
-
-
Field Detail
-
STEM_INDEX_MARKER
public static final String STEM_INDEX_MARKER
- See Also:
- Constant Field Values
-
STEM_ENTRY_CONNECTOR
public static String STEM_ENTRY_CONNECTOR
-
-
Constructor Detail
-
QDLStem
public QDLStem()
-
QDLStem
public QDLStem(QDLList list)
Initialize a QDL stem with a list- Parameters:
list
-
-
QDLStem
public QDLStem(Long count, QDLValue[] fillList)
Initialize a QDL stem with count elements form the fillList. This returns a simple list. SeeQDLList(long, QDLValue[])
for the contract.- Parameters:
count
-fillList
-
-
-
Method Detail
-
newInstance
public QDLStem newInstance()
-
getQDLMap
public QDLMap getQDLMap()
-
setQDLMap
public void setQDLMap(QDLMap qdlMap)
-
setQDLList
public void setQDLList(QDLList qdlList)
-
getDecimal
public BigDecimal getDecimal(Long key)
-
addList
public void addList(List list)
Adds a list of objects to this stem, giving them indices appropriate indices This is mostly a convenience for people writing in java to create lists programatically. Note there is no parameter for this list since that will blow up if there are mixed entries.- Parameters:
list
-
-
listAppend
public void listAppend(QDLStem stem)
Append the list elements of the stem to this list. This integrates the lists but does not affect the maps.- Parameters:
stem
-
-
listAdd
public void listAdd(QDLValue value)
Add the single object to the list in this stem. This allows you to add a stem as a list value, unlikelistAppend(QDLStem)
which appends the elements of the argument's list to the current object's list.- Parameters:
value
-
-
listCopy
public void listCopy(long startIndex, long length, QDLStem target, long insertIndex)
Copies the elements from this list to the target list. Note that this will over-write any elements already in the target. If you need to insert elements, use thelistInsertAt(long, long, QDLStem, long)
method.- Parameters:
startIndex
- first index in the sourcelength
- how many elements to take from the sourcetarget
- that target to get the copyinsertIndex
- where in the target to start copying.
-
listInsertAt
public void listInsertAt(long startIndex, long length, QDLStem target, long insertIndex)
Insert the current- Parameters:
startIndex
-length
-target
-insertIndex
-
-
listSubset
public QDLStem listSubset(long startIndex)
Insert the whole argument in to the current stem, re-adjusting indices.- Parameters:
startIndex
-
-
listSubset
public QDLStem listSubset(long startIndex, long length)
-
isList
public boolean isList()
-
getDefaultValue
public QDLValue getDefaultValue()
If this is set, then any get with no key will return this value. Since the basic unit of QDL is the stem, this gives us a way of basically turning a scalar in to a stem without having to do complicated size and key matching.
Note thatcontainsKey(Object)
still works as usual, so you can ask if a key exists.- Returns:
-
setDefaultValue
public void setDefaultValue(QDLValue defaultValue)
-
setDefaultValue
public void setDefaultValue(Object defaultValue)
Do conversion toQDLValue
.- Parameters:
defaultValue
-
-
hasDefaultValue
public boolean hasDefaultValue()
-
size
public int size()
As per usual JavaMap.size()
contract, return axis 0 count
-
size
public long size(int axis)
Return count down to a given axis. This acceptsProcessStemAxisRestriction.ALL_AXES
as well, which gives the count of every entry, i.e., the cardinality of the stem.- Parameters:
axis
-- Returns:
-
containsKey
public boolean containsKey(Object k)
- Specified by:
containsKey
in interfaceMap<QDLKey,QDLValue>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValue
in interfaceMap<QDLKey,QDLValue>
-
clone
public QDLStem clone()
Make a shallow copy of this stem variable.
-
get
public QDLValue get(QDLKey key)
Convenience setter to put an object. If it is not aQDLValue
it will be converted to one.- Parameters:
key
-
-
addAll
public void addAll(QDLStem qdlStem)
-
keySet
public StemKeys keySet()
return an enumeration (set) of ALL keys in order, starting with the list indices then the keys.
-
values
public QDLSet<QDLValue> values()
Be aware that this creates an actual set so it reads every item. If you need to iterate over the elements (so single pass, not potentially multiple passes) consider usingvaluesIterator()
while gets the iterators and manages them.
-
isLongIndex
public static boolean isLongIndex(String key)
Does a regex on the index to see if it is really a long. Note that this still is needed since a user can set a.'2' := 3 and this should turn it into a list entry otherwise we get both string and list entries with "the same" key.The logic of doing this with a regex vs. one of Java's parse methods (e.g.
Long.parseLong(String)
is that the latter would throw an exception and exception handling in Java slows dramatically as the stack size increases. It is better for performance to use a regex.- Parameters:
key
-- Returns:
-
isVar
protected boolean isVar(String var)
-
valueSet
public QDLSet valueSet()
-
almostUnique
public QDLStem almostUnique()
This almost returns all the unique elements. The issue is that if there are deeply nested stems, then do not entirely get made unique before getting added to the result, hence the simplest fix is that in that case is to call this twice. Someday this can be fixed with a careful rewrite of the recursion in stem lists.- Returns:
-
dim
public QDLStem dim()
-
getRank
public Long getRank()
-
get
public QDLValue get(StemPath<StemPathEntry> stemPath)
-
union
public QDLStem union(QDLStem... stemVariables)
This will return a new stem consisting of this stem and the union of all the stem arguments. The effect is to overwrite the current stem values with the argument values. so e.g.{'a':1}~{'a':2,'b':3}~{'b':4,'c':5} {a:2, b:4, c:5}
- Parameters:
stemVariables
-- Returns:
-
valuesIterator
public Iterator valuesIterator()
A specific iterator for the values of this stem. This should be used when traversing all values, such as inWhileLoop
s.- Returns:
-
hasValue
public boolean hasValue(QDLValue x)
-
renameKeys
public void renameKeys(QDLStem newKeys, boolean overWriteKeys)
-
hasKeys
public QDLStem hasKeys(QDLStem keyList)
Takes a stem and returns a boolean list conformable to the argument.- Parameters:
keyList
-- Returns:
-
hasKey
public QDLStem hasKey(QDLStem keyList)
Modern successor to the deprecatedhasKeys(QDLStem)
. This returns a left conformable stem as it should.- Parameters:
keyList
-- Returns:
-
newGet
public IndexList newGet(IndexList indexList, boolean strictMatching)
Strict matching is used at the last resolution of the stem. It means that left over scalars are flagged as errors since there is no stem waiting to resolve them.- Parameters:
indexList
-strictMatching
-- Returns:
-
remove
public boolean remove(IndexList indexList)
-
get
public QDLValue get(StemMultiIndex w)
Note that the- Parameters:
w
-- Returns:
-
set
public void set(StemMultiIndex w, QDLValue value)
-
remove
public void remove(StemMultiIndex w)
-
allKeys2
protected QDLStem.KeyRankMap allKeys2()
-
keysByAxis
public QDLStem keysByAxis(Long axis)
Get by axis. So if axis is 0, top level keys only, axis == 1 yields rank 2 keys- Parameters:
axis
-- Returns:
-
indicesByRank
public QDLStem indicesByRank(Long rank)
Return the indices of a given rank for this stem.- Parameters:
rank
-- Returns:
-
convertKeyByRank
protected QDLStem convertKeyByRank(List<List> list)
Gets a list of lists, e.g.[[foo, 0], [foo, 1], [foo, 2], [foo, tyu]]
and returns a stem of these.- Parameters:
list
-- Returns:
-
indicesByRank
protected void indicesByRank(QDLStem v, List list, QDLStem.KeyRankMap keyRankMap)
-
indicesByRank
public QDLStem indicesByRank()
Returns a flat list of all indices at a given rank. Each element is a stem. Special case is rank of 0 returns the same as rank == 1, except the elements are scalars, not stems with a single element.- Returns:
-
toJSON
public net.sf.json.JSON toJSON()
Converts this to a JSON object. Names of stem components are decoded. So if you have a stem, a., with component $23foo, then a.$23foo yields{"#foo":...}
I.e. the $23 is treated as an escaped name and converted back. If you do not want stem names escaped when converting to JSON, then usetoJSON(boolean, int)
with the argument being false. In that case the outputted JSON would be{"$23foo":...}
- Returns:
-
toJSON
public net.sf.json.JSON toJSON(boolean escapeNames, int codecType)
-
fromJSON
public QDLStem fromJSON(net.sf.json.JSON json)
-
fromJSON
public QDLStem fromJSON(net.sf.json.JSONObject jsonObject)
-
fromJSON
public QDLStem fromJSON(net.sf.json.JSONObject jsonObject, boolean convertVars, int type)
Populate this from a JSON object. Note that JSON arrays are turned in to stem lists.- Parameters:
jsonObject
- return this object, populated
-
fromJSON
public QDLStem fromJSON(net.sf.json.JSONArray array, boolean convert, int type)
-
toString
public String toString(int indentFactor, String currentIndent)
Used as a utility, this prints with an indent relative to a current indent.- Parameters:
indentFactor
-currentIndent
-- Returns:
-
toString
public String toString(int indentFactor)
-
toString
protected String toString(boolean showDefaultValues)
Whether or not to show default values. This shows the top level, but not any embedded stems, or the output becomes huge and unreadable fast.- Parameters:
showDefaultValues
-- Returns:
-
inputForm
public String inputForm()
-
inputForm
public String inputForm(int indentFactor)
-
removeAllByValues
public boolean removeAllByValues(Collection<QDLValue> c, boolean reorderLists)
Removes every value in the collection from everywhere in the stem. Optionally, lists will be reordered too. Since this a bit more- Parameters:
c
-reorderLists
-- Returns:
-
removeAllByValue
public boolean removeAllByValue(QDLValue c, boolean reorderLists)
-
removeAllByValue
public boolean removeAllByValue(QDLValue c, boolean reorderLists, boolean listProcessed)
-
getByMultiIndex
public QDLValue getByMultiIndex(String index)
This will take a stem with embedded . and return the object at that index.
E.g.
myStem.getByMultiIndex("a.b.c");
would return myStem.a.b.c;
This just calls
get(StemMultiIndex)
.Note
In this call, you pass in the exact index you want, unlike
get(StemMultiIndex)
which accepts the name of the stem as the first argument- Parameters:
index
-- Returns:
-
put
public QDLValue put(QDLKey qdlKey, Object object)
Convenience method that converts second argument to aQDLValue
as needed.- Parameters:
qdlKey
-object
-- Returns:
-
putAll
public void putAll(Map<? extends QDLKey,? extends QDLValue> map)
This does not add the list elements because that causes issues with the contract for maps. If you are adding all the values of a stem to this one, useaddAll(QDLStem)
.
-
-