Skip to content

Sets and Slice Notation for Java Boon!

RichardHightower edited this page Feb 16, 2014 · 3 revisions

If you are new to boon, you might want to start here. Boon is a opinionated, low ceremony Java framework.

Sets and slice notation in Java Boon!

Slice notations works with sorted maps and sorted sets.

        Set<String> set =
                set("apple", "oranges", "pears", "grapes", "kiwi");

        assertEquals (5, len( se t)) ;
        assertTrue  ( in ( "apple", set ) );

The above uses in len and in which are universal operators in Boon.

We can even use slice notations with tree sets.

        NavigableSet<String> set =
                sortedSet("apple", "kiwi", "oranges", "pears", "pineapple");

        assertEquals(
                5,
                len(set)
        );

        assertTrue(
                in("apple", set)
        );

        assertEquals(

                "oranges", idx(set, "ora")

        );

        assertEquals(

                "oranges", idx(set, "o")

        );

        assertEquals(

                "pears",
                idx(set, "p")

        );

        assertEquals(

                "pineapple",
                idx(set, "pi")

        );

        assertEquals(

                "pineapple",
                after(set, "pi")

        );

        assertEquals(

                "pears",
                before(set, "pi")

        );


        assertEquals(

                sortedSet("apple", "kiwi"),
                slc(set, "ap", "o")

        );

        assertEquals(

                sortedSet("apple", "kiwi"),
                slc(set, "o")

        );

        assertEquals(

                sortedSet("oranges", "pears", "pineapple"),
                slcEnd(set, "o")
        );

The above uses in len and in which are universal operators in Boon.

                set = sortedSet("apple", "kiwi", "oranges", "pears", "pineapple")

                slcEnd( set, "o" )      //returns ("oranges", "pears", "pineapple")
                slc( set, "ap", "o" )   //returns ("apple", "kiwi"),
                slc( set, "o" )         //returns ("apple", "kiwi")

What you are really doing with slicing with treemaps and treesets is a between query of sorts.

What item comes after "pi"?

                after(set, "pi") //pineapple

And before pineapple?

                before(set, "pi")
        Set<String> set = set("apple", "pear", "orange");

        Set<String> set2;

        set2 = set( copy(  set ) );
        assertEquals(
                set, set2
        );

        set2 = set( copy( sortedSet( set ) ) );
        assertEquals(
                set, set2
        );


        set2 = set( copy( safeSet( set ) ) );
        assertEquals(
                set, set2
        );



        set2 = set( copy( safeSortedSet( set ) ) );
        assertEquals(
                set, set2
        );

Boon allows you to easily convert from another collection, enumeration, array, etc. into a set.

        Set<String> set = set("apple", "pear", "orange");

        Set<String> set2 = set(enumeration(set));
        assertEquals(
                set, set2
        );

        set2 = sortedSet(enumeration(set));
        assertEquals(
                set, set2
        );


        set2 = safeSet(enumeration(set));
        assertEquals(
                set, set2
        );

        set2 = safeSortedSet(enumeration(set));
        assertEquals(
                set, set2
        );

Works with itearale too.

        Set<String> set3 = set((Iterable)set2);
        assertEquals(
                set2, set3
        );

        set3 = sortedSet((Iterable)set2);
        assertEquals(
                set2, set3
        );


        set3 = safeSortedSet((Iterable)set2);
        assertEquals(
                set2, set3
        );


        set3 = safeSet((Iterable)set2);
        assertEquals(
                set2, set3
        );


        Set<String> set4 = set((Collection)set3);
        assertEquals(
                set3, set4
        );


        set4 = safeSet((Collection)set3);
        assertEquals(
                set3, set4
        );


        set4 = safeSortedSet((Collection)set3);
        assertEquals(
                set3, set4
        );

        set4 = sortedSet((Collection)set3);
        assertEquals(
                set3, set4
        );

        Set<String> set5 = set(set4.iterator());
        assertEquals(
                set4, set5
        );


        set5 = sortedSet(set4.iterator());
        assertEquals(
                set4, set5
        );


        set5 = safeSortedSet(set4.iterator());
        assertEquals(
                set4, set5
        );


        set5 = safeSet(set4.iterator());
        assertEquals(
                set4, set5
        );

    }

You can create sets based on the contents of other sets:

        assertTrue (

        sortedSet("apple", "pear", "orange") .equals(
                set("apple", "pear", "orange") ) &&
        safeSet("apple", "pear", "orange").equals(
                safeSortedSet("apple", "pear", "orange") ) &&
        sortedSet("apple", "pear", "orange") .equals(
                safeSortedSet("apple", "pear", "orange") )

        );
        Set<String> set = set("apple", "grape", "pears");
        Set<String> set2 = set(enumeration(set));
        assertEquals(
                set, set2
        );

The universal add works with sets too.

Add an apple

        add(set, "apple");

        assertTrue(

                len(set) == 1

        );



        assertTrue(

                !(set instanceof SortedSet) || idx(set, "a").equals("apple")

        );



        Set<String> set2 = copy(set);
        assertTrue(

                !(set2 instanceof SortedSet) || idx(set2, "a").equals("apple")

        );

        assertTrue(

                len(set2) == 1

        );

Thoughts

Thoughts? Write me at richard high tower AT g mail dot c-o-m (Rick Hightower).

Further Reading:

If you are new to boon start here:

Why Boon?

Easily read in files into lines or a giant string with one method call. Works with files, URLs, class-path, etc. Boon IO support will surprise you how easy it is. Boon has Slice notation for dealing with Strings, Lists, primitive arrays, Tree Maps, etc. If you are from Groovy land, Ruby land, Python land, or whatever land, and you have to use Java then Boon might give you some relief from API bloat. If you are like me, and you like to use Java, then Boon is for you too. Boon lets Java be Java, but adds the missing productive APIs from Python, Ruby, and Groovy. Boon may not be Ruby or Groovy, but its a real Boon to Java development.

Core Boon Philosophy

Core Boon will never have any dependencies. It will always be able to run as a single jar. This is not just NIH, but it is partly. My view of what Java needs is more inline with what Python, Ruby and Groovy provide. Boon is an addition on top of the JVM to make up the difference between the harder to use APIs that come with Java and the types of utilities that are built into Ruby, Python, PHP, Groovy etc. Boon is a Java centric view of those libs. The vision of Boon and the current implementation is really far apart.

===

Contact Info

blog|[twitter](https://twitter.com/RickHigh|[infoq]http://www.infoq.com/author/Rick-Hightower|[stackoverflow](http://stackoverflow.com/users/2876739/rickhigh)|[java lobby](http://java.dzone.com/users/rhightower)|Other | richard high tower AT g mail dot c-o-m (Rick Hightower)|work|cloud|nosql

Clone this wiki locally