.. index:: Scolvo script **************** Data Structures **************** This page describes the available data structures in Scolvo script language. Collections ~~~~~~~~~~~ It represents a group of objects, known as its elements. .. code-block:: scolvoscript :caption: Collection definition example :linenos: var collection = [ { "key": "value1" }, { "key": "value2" }]; .. .. index:: collection.filter() Filtering =========== The filtering of a collection can be done using an inline function. The function has to return true on items to be kept, and it returns a new collection consisting of the kept items. .. code-block:: scolvoscript :caption: Example collection filtering :linenos: var collection = [ { "itemName": "something" }, { "itemName": "other" }]; var result = collection.filter(function(current) { return current.itemName == "something"; }); .. .. index:: collection.each() Iterating =========== The iteration on the items of a collection can be done using an inline function. The function receives the current item, and it can execute the business logic. .. code-block:: scolvoscript :caption: Example collection iteration with inline function :linenos: var result = 0; [1, 2, 3, 4].each(function(current) { result += current; }); .. It is also possible to use a predefined function when iterating through the items: .. code-block:: scolvoscript :caption: Example collection iteration with predefined function :linenos: var result = ""; var collection = [ { "itemName": "something" }, { "itemName": "other" }]; function append(current) { result += current.itemName; }; collection.each(append); .. Iterating over a variable containing JsonArray using inline function declaration (works with any collection type). .. code-block:: scolvoscript :caption: Iterating on JsonArray with inline function :linenos: var result = ""; var collection = [ { "itemName": "something" }, { "itemName": "other" }]; collection.each(function(current){ result += current.itemName; }); .. The *each* function returns a collection, so the function calls can be chained. Each one returns the same collection, so if an item is modified, it will be reflected on original instance as well (Consider using clones if required). .. code-block:: scolvoscript :caption: Iteraton with chaining :linenos: var result = ""; var collection = [ { "itemName": "something" }, { "itemName": "other" }]; function extend(current) { current.put("itemDescription", "newValue"); } collection.each(extend).each(function(currentItem) { result += currentItem.itemDescription; }); .. .. index:: collection.sort() Sorting =========== The *sort* function of a collection returns a java.util.List. The function has 1 parameter - a sorting function with 2 items as parameter returning -1, 0, 1 values according to the sorting logic. It works as the Comparable interface of Java. .. code-block:: scolvoscript :caption: Collection sort example :linenos: var collection = [2, 3, 1, 4, 5].sort(function(a, b) { if (a < b) { return -1; } else if (a > b) { return 1; } else { return 0; } }); .. .. index:: collection.join() Joining =========== The *join* function takes the itmes of the collection and returns a concatenated String by taking the collection items' toString() method and the separator parameter. .. code-block:: scolvoscript :caption: Collection join example :linenos: var fullContent = [1, 2, 3, 4].join(" "); .. .. index:: collection.slice() Slicing =========== Slicing is a concept in Python for taking a part of a collection using one or two parameters. If one parameter is given, then it returns elements from 0 position until the given position parameter. When 2 parameters are defined then it returns the items from the first position parameter to the second position parameter. If negative parameters are given, then the index value is calculated to left out the given number of elements (size of the collection + given negative number -1). .. code-block:: scolvoscript :caption: Collection slice example :linenos: // [1, 2, 3] items from 0th through 2nd position var result = [1, 2, 3, 4, 5].slice(2); // [2, 3, 4] items from 1st through 3rd result = [1, 2, 3, 4, 5].slice(1, 3); // [1, 2] items from 0th through (size -3 -1)th result = [1, 2, 3, 4, 5].slice(-3); // [1, 2, 3, 4, 5] no array index out of bounds exception result = [1, 2, 3, 4, 5].slice(7); // no array index out of bounds exception result = [1, 2, 3, 4, 5].slice(-7); //[4, 5] items from 3rd to last result = [1, 2, 3, 4, 5].slice(3, null); .. .. index:: collection.toString() ToString =========== The *toString* method returns JSON string representation of underlying collection object. .. code-block:: scolvoscript :caption: Collection toString example :linenos: var result = [1, 2, 3, 4, 5].toString(); .. .. index:: collection.deepClone() Deep clone =========== The *deepClone* method copies all fields, and makes copies of dynamically allocated memory pointed to by the fields. .. code-block:: scolvoscript :caption: Collection deep clone example :linenos: var collection = [ { "itemName": "something" }, { "itemName": "other" }]; var newCollection = collection.deepClone(); .. .. index:: collection.removeByIdx() Remove by index ================ The *removeByIdx* method removes an element by index and returns it. .. code-block:: scolvoscript :caption: Collection removeByIdx example :linenos: var collection = ["one", "two", "three"]; // removedElement value is "two" var removedElement = collection.removeByIdx(1); .. Maps ~~~~~~~~~~~ It represents a key-value map like the java.util.Map. The interface changes in Java8 edition are separately implemented. All methods stated below conform with the definition of corresponding `java.util.Map javadoc `__ of Java7, where it is not stated otherwise. .. code-block:: scolvoscript :caption: Map definition example :linenos: var map = {}; .. .. index:: map.get() Get === The *get* method returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. It is also possible to get the appropriate value using the key as property. .. code-block:: scolvoscript :caption: Map get example :linenos: var map = { "myKey": "myValue" }; var myValue = map.get("myKey"); var myValueByProperty = map.myKey; .. .. index:: map.getOrDefault() GetOrDefault ============= The *getOrDefault* method is not part of method Java 7 map implementation, this function is implemented according to the Java 8 Map API. It returns the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key. .. code-block:: scolvoscript :caption: Map getOrDefault example :linenos: var map = { "myKey": "myValue" }; var value = map.getOrDefault("nonExistingKey", "default value"); .. .. index:: map.put() Put ============= The method associates the specified value with the specified key in this map (optional operation). If the map previously contained a mapping for the key, the old value is replaced by the specified value. (A map m is said to contain a mapping for a key k if and only if m.containsKey(k) would return true.) .. code-block:: scolvoscript :caption: Map getOrDefault example :linenos: var data = {}; data.put("data","stringData"); data.put("statusCode", 400); var result = data.statusCode; .. .. index:: map.remove() Remove ============= It removes the mapping for a key from this map if it is present (optional operation). Returns the value to which this map previously associated the key, or null if the map contained no mapping for the key. .. code-block:: scolvoscript :caption: Map remove example :linenos: var data = { "data": "stringData", "statusCode": 400 }; var statusCode = data.remove("statusCode"); .. .. index:: map.containsKey() ContainsKey ============= It returns true if this map contains a mapping for the specified key. .. code-block:: scolvoscript :caption: Map remove example :linenos: var data = { "existing": "value" }; var isThereExisting = data.containsKey("existing"); var isThereNonExisting = data.containsKey("non-existing"); .. .. index:: map.size() Size ============= It returns the number of key-value mappings in this map. If the map contains more than Integer.MAX_VALUE elements, returns Integer.MAX_VALUE. .. code-block:: scolvoscript :caption: Map size example :linenos: var data = { "existing": "value" }; var mapSize = data.size(); .. .. index:: map.isEmpty() IsEmpty ============= It returns true if this map contains no key-value mappings. .. code-block:: scolvoscript :caption: Map isEmpty example :linenos: var data = { "existing": "value" }; if (data.isEmpty()) { info("Data is empty"); } else { info("Data has " + data.size() + " key(s)."); } .. .. index:: map.clear() Clear ============= It removes all of the mappings from this map. .. code-block:: scolvoscript :caption: Map clear example :linenos: var data = { "existing": "value" }; data.clear(); .. .. index:: map.keySet() KeySet ============= Returns a Set view of the keys contained in this map. The set is backed by the map, so changes to the map are reflected in the set, and vice-versa. .. code-block:: scolvoscript :caption: Map keySet example :linenos: var data = { "existing": "value" }; if (data.keySet().containsKey("exisiting")) { info("Key is existing by keySet()"); } .. .. index:: map.values() Values ============= Returns a Collection view of the values contained in this map. The collection is backed by the map, so changes to the map are reflected in the collection, and vice-versa. .. code-block:: scolvoscript :caption: Map values example :linenos: var data = { "existing": "value" }; var values = data.values(); .. .. index:: map.equals() Equals ============= Compares the specified object with this map for equality. Returns true if the given object is also a map and the two maps represent the same mappings. .. code-block:: scolvoscript :caption: Map equals example :linenos: var data = { "existing": "value" }; var otherData = { "existing": "value" }; var areEqual = data.equals(otherData); .. .. index:: map.hashCode() HashCode ============= It returns the hash code value for the map. .. index:: map.toString() Map ToString ============= It returns the Json representation of the map. .. code-block:: scolvoscript :caption: Map toString example :linenos: var data = { "existing": "value" }; info("The content of the map: " + data.toString()); .. .. index:: map.deepClone() Map DeepClone ============= It creates a clone from the map using Json serialization/deserialization. .. code-block:: scolvoscript :caption: Map deepClone example :linenos: var user = {"key": "value"}; var userClone = user.deepClone(); user.remove("key"); ..