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 classQDLStem.ARGetkeysstatic classQDLStem.KeyRankMapA map that is keyed by an integer and has lists of indices as values.protected static classQDLStem.OrderedIndexEntrystatic classQDLStem.SizeOfclassQDLStem.ValueIterator
-
Field Summary
Fields Modifier and Type Field Description static StringSTEM_ENTRY_CONNECTORstatic StringSTEM_INDEX_MARKER
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidaddAll(QDLStem qdlStem)voidaddList(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.KeyRankMapallKeys2()QDLStemalmostUnique()This almost returns all the unique elements.voidclear()QDLStemclone()Make a shallow copy of this stem variable.QDLStemcommonKeys(QDLStem arg2)booleancontainsKey(Object k)booleancontainsValue(Object value)protected QDLStemconvertKeyByRank(List<List> list)Gets a list of lists, e.g.QDLStemdim()Set<Map.Entry<QDLKey,QDLValue>>entrySet()QDLStemexcludeKeys(QDLStem keyList)QDLStemfromJSON(net.sf.json.JSON json)QDLStemfromJSON(net.sf.json.JSONArray array, boolean convert, int type)QDLStemfromJSON(net.sf.json.JSONObject jsonObject)QDLStemfromJSON(net.sf.json.JSONObject jsonObject, boolean convertVars, int type)Populate this from a JSON object.QDLValueget(Long key)QDLValueget(Object key)QDLValueget(String key)IndexListget(IndexList indexList, boolean strictMatching)QDLValueget(StemMultiIndex w)Note that theQDLValueget(StemPath<StemPathEntry> stemPath)QDLValueget(QDLKey key)Convenience setter to put an object.BooleangetBoolean(Long key)Used in unit tests only.BooleangetBoolean(String key)QDLValuegetByMultiIndex(String index)This will take a stem with embedded .BigDecimalgetDecimal(Long key)QDLValuegetDefaultValue()If this is set, then any get with no key will return this value.LonggetLong(Long key)LonggetLong(String key)QDLList<? extends QDLValue>getQDLList()QDLMapgetQDLMap()LonggetRank()QDLStemgetStem(Long key)QDLStemgetStem(String key)StringgetString(Long key)StringgetString(String key)booleanhasDefaultValue()QDLStemhasKey(QDLStem keyList)Modern successor to the deprecatedhasKeys(QDLStem).QDLStemhasKeys(QDLStem keyList)Takes a stem and returns a boolean list conformable to the argument.booleanhasValue(QDLValue x)QDLStemincludeKeys(QDLStem keyList)QDLStemindicesByRank()Returns a flat list of all indices at a given rank.QDLStemindicesByRank(Long rank)Return the indices of a given rank for this stem.protected voidindicesByRank(QDLStem v, List list, QDLStem.KeyRankMap keyRankMap)StringinputForm()StringinputForm(int indentFactor)StringinputForm(int indentFactor, String currentIndent)booleanisEmpty()booleanisList()static booleanisLongIndex(String key)Does a regex on the index to see if it is really a long.protected booleanisVar(String var)QDLStemkeysByAxis(Long axis)Get by axis.StemKeyskeySet()return an enumeration (set) of ALL keys in order, starting with the list indices then the keys.voidlistAdd(QDLValue value)Add the single object to the list in this stem.voidlistAppend(QDLStem stem)Append the list elements of the stem to this list.voidlistCopy(long startIndex, long length, QDLStem target, long insertIndex)Copies the elements from this list to the target list.voidlistInsertAt(long startIndex, long length, QDLStem target, long insertIndex)Insert the currentQDLStemlistSubset(long startIndex)Insert the whole argument in to the current stem, re-adjusting indices.QDLStemlistSubset(long startIndex, long length)QDLStemmask(QDLStem stem2)protected voidmyPut(QDLValue index, QDLValue value)IndexListnewGet(IndexList indexList, boolean strictMatching)Strict matching is used at the last resolution of the stem.QDLStemnewInstance()QDLStemnewInstance(Long count, QDLValue[] fillList)QDLValueput(int index, Object value)QDLValueput(int index, QDLValue value)QDLValueput(String key, QDLValue value)QDLValueput(QDLKey qdlKey, Object object)Convenience method that converts second argument to aQDLValueas needed.QDLValueput(QDLKey qdlKey, QDLValue qdlValue)voidputAll(Map<? extends QDLKey,? extends QDLValue> map)This does not add the list elements because that causes issues with the contract for maps.QDLValueremove(Long key)QDLValueremove(Object kk)QDLValueremove(String key)booleanremove(IndexList indexList)voidremove(StemMultiIndex w)booleanremoveAllByValue(QDLValue c, boolean reorderLists)booleanremoveAllByValue(QDLValue c, boolean reorderLists, boolean listProcessed)booleanremoveAllByValues(Collection<QDLValue> c, boolean reorderLists)Removes every value in the collection from everywhere in the stem.voidrenameKeys(QDLStem newKeys, boolean overWriteKeys)voidset(IndexList indexList, QDLValue value)voidset(StemMultiIndex w, QDLValue value)voidsetDefaultValue(Object defaultValue)Do conversion toQDLValue.voidsetDefaultValue(QDLValue defaultValue)voidsetQDLList(QDLList qdlList)voidsetQDLMap(QDLMap qdlMap)intsize()As per usual JavaMap.size()contract, return axis 0 countlongsize(int axis)Return count down to a given axis.net.sf.json.JSONtoJSON()Converts this to a JSON object.net.sf.json.JSONtoJSON(boolean escapeNames, int codecType)StringtoString()protected StringtoString(boolean showDefaultValues)Whether or not to show default values.StringtoString(int indentFactor)StringtoString(int indentFactor, String currentIndent)Used as a utility, this prints with an indent relative to a current indent.QDLStemunion(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.QDLSetvalueSet()IteratorvaluesIterator()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_AXESas 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:
containsKeyin interfaceMap<QDLKey,QDLValue>
-
containsValue
public boolean containsValue(Object value)
- Specified by:
containsValuein 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 aQDLValueit 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 inWhileLoops.- 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 aQDLValueas 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).
-
-