public interface UniversalOperations<ITEM,INDEX>
//Works with lists, arrays, sets, maps, sorted maps, etc. ListfruitList; String [] fruitArray; Set veggiesSet; char [] letters; byte [] bytes; NavigableMap favoritesMap; Map map; // These helper methods are used to create common Java types. // Sets and lists have concurrent and non concurrent variants // Set also has sorted and non sorted variants // This makes safeList, list, set, sortedSet, safeSet, safeSortedSet veggiesSet = set( "salad", "broccoli", "spinach"); fruitList = list( "apple", "oranges", "pineapple"); fruitArray = array( "apple", "oranges", "pineapple"); letters = array( 'a', 'b', 'c'); bytes = array( new byte[]{0x1, 0x2, 0x3, 0x4}); //You add up name / value pairs as a pseudo literal for map favoritesMap = sortedMap( 2, "pineapple", 1, "oranges", 3, "apple" ); // You add up name / value pairs as a pseudo literal for map // map, sortedMap, safeMap (thread safe concurrent), and sortedSafeMap are // supported. map = map ( "pineapple", 2, "oranges", 1, "apple", 3 );
Modifier and Type | Interface and Description |
---|---|
static class |
UniversalOperations.Returns |
Modifier and Type | Method and Description |
---|---|
UniversalOperations.Returns |
add(ITEM item)
Adds something to a collection, map or array
For maps you add an entry
Implemented by:
|
UniversalOperations |
clone(UniversalOperations thing)
NOT IMPLEMENTED YET.
|
UniversalOperations |
copy(UniversalOperations thing)
Copies something.
|
ITEM |
idx(INDEX index)
Get the item at the index.
|
void |
idx(INDEX index,
ITEM item)
Sets the value at an index.
|
void |
insert(ITEM item)
This does not work with Sets or Maps (use idx for those)
Implemented by:
|
int |
len()
Gets the length
This works with Maps, TreeSets, Strings, Object arrays, primitive arrays,
etc.
|
void |
slc(INDEX start) |
void |
slc(INDEX start,
INDEX end)
Slice a string, array, TreeSet, or TreeMap.
|
void |
slcEnd(INDEX end) |
ITEM idx(INDEX index)
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists org.boon.Maps org.boon.SetsWorks with maps and sets
MapInitialize our examplesdogMap; //map of dogs Dog dog = new Dog("dog"); NavigableSet set; //set of strings List list; char[] letters;
dogMap = map("dog", dog); set = sortedSet("apple", "kiwi", "oranges", "pears", "pineapple"); list = list("apple", "oranges", "pears"); letters = array('a', 'b', 'c', 'd');The methods map, list, sortedSet, arrays are utility methods for creating Maps, lists sets, etc.
//Get the dog at the index "dog" in the map assertEquals( dog, idx(dogMap, "dog") ); //Get the string "oranges" at index "ora" assertEquals( "oranges", idx(set, "ora") ); //Get the string "oranges" at index "o" assertEquals( "oranges", idx(set, "o") ); //Get the string "oranges" at index 1 of the list. assertEquals( "oranges", idx(list, 1) ); // Get the string "pears" at index -1 (using Python style slice notation) // of the list. assertEquals( "pears", idx(list, -1) ); //oranges are two from the back assertEquals( "oranges", idx(list, -2)); //apple are two from the back assertEquals( "apple", idx(list, -3));Based on the example at top
UniversalOperations
:
Negative index works with list like, array like things.//Using idx to access a value. assert idx( veggiesSet, "b").equals("broccoli"); assert idx( fruitList, 1 ).equals("oranges"); assert idx( fruitArray, 1 ).equals("oranges"); assert idx( letters, 1 ) == 'b'; assert idx( bytes, 1 ) == 0x2; assert idx( favoritesMap, 2 ).equals("pineapple"); assert idx( map, "pineapple" ) == 2;
//Negative indexes assert idx( fruitList, -2 ).equals("oranges"); assert idx( fruitArray, -2 ).equals("oranges"); assert idx( letters, -2 ) == 'b'; assert idx( bytes, -3 ) == 0x2;
index
- the index of the itemByt
,
Chr
,
Int
,
Arrays
,
Lists
,
Maps
,
Sets.idx(java.util.NavigableSet, Object)
,
Byt.idx(byte[], int)
,
Chr.idx(char[], int)
,
Int
,
Arrays.idx(Object[], int)
,
Lists
,
Maps
,
Sets
void idx(INDEX index, ITEM item)
index
- indexitem
- item you are settingint len()
UniversalOperations
).
Implemented by:// Getting the length assert len( veggiesSet ) == 3; assert len( fruitList ) == 3; assert len( fruitArray ) == 3; assert len( letters ) == 3; assert len( bytes ) == 4; assert len( favoritesMap ) == 3; assert len( map ) == 3;
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists org.boon.Maps org.boon.Sets
UniversalOperations.Returns add(ITEM item)
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists org.boon.Maps org.boon.Sets
UniversalOperations copy(UniversalOperations thing)
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists org.boon.Maps org.boon.Sets
UniversalOperations clone(UniversalOperations thing)
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists org.boon.Maps org.boon.Sets
void insert(ITEM item)
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists
void slc(INDEX start, INDEX end)
>>> string [0:3] 'foo' >>> string [-3:7] 'bar'What follows is derived from Python's slice notation .
a[ start : end ] # items start through end-1 a[ start : ] # items start through the rest of the array a[ : end ] # items from the beginning through end-1 a[ : ] # a copy of the whole arrayBoon would be (Java):
slc( a, start, end ) // items start through end-1 slc( a, start ) // items start through the rest of the array slcEnd( a, end ) // items from the beginning through end-1 copy( a ) // a copy of the whole arrayNOT IMPLEMENTED YET:
a[ start : end : step] # start through not past end, by stepBoon
slc(a, start , end, step) // start through not past end, by stepThe key point to remember is that the :end value represents the first value that is not in the selected slice. So, the difference between end and start is the number of elements selected (if step is 1, the default).
a[ -1 ] # last item in the array a[ -2: ] # last two items in the array a[ :-2 ] # everything except the last two itemsBoon slice notation
slc( a, -1) # last item in the array slc( -2 ) # last two items in the array slcEnd( -2 ) # everything except the last two itemsPython and boon are kind to the programmer if there are fewer items than you ask for. For example, if you ask for a[:-2] and a only contains one element, you get an empty list instead of an error. Sometimes you would prefer the error, so you have to be aware that this may happen. Implemented by:
org.boon.primitive.Byt org.boon.primitive.Chr org.boon.primitive.Int (in progress) org.boon.primitive.Lng (in progress) org.boon.Arrays org.boon.core.Strings (planned) org.boon.core.StrBuf (planned) org.boon.Lists
void slc(INDEX start)
start
- index startslc(Object, Object)
void slcEnd(INDEX end)
end
- index endslc(Object, Object)