Details

    • Type: Improvement Improvement
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: None
    • Fix Version/s: 2.5, 2.1
    • Component/s: core-collections
    • Labels:
      None

      Description

      Add some usuful methods to Pivot Collections like those needed to process all elements, going more "function style", like addAll, contains, etc ...
      To simplify usage from people coming from other languages, try to align to other languages names/conventions (for what is possible) , like Scala, C# .
      Verify even if add a interface (defining a single method) that could be passed to some methods to contain logic for processing all elements (like the apply() in Scala), and chose a right name for it (maybe function or other similar).

      Some info here:
      http://apache-pivot-developers.417237.n3.nabble.com/Some-idea-on-extending-Pivot-collections-td3321472.html

      But for more changes in Collections (still to be discussed), wait the 3.0 ...

        Issue Links

          Activity

          Hide
          Sandro Martini added a comment - - edited

          Other things that could be a good addition:

          Of course before all this I'll verify if/how we can do this (in Java), both as coding strategies (if there are some pitfalls, etc), and by licensing (should not exist problems here, but I'll do some check to be sure).
          Note that some things described here are patterns, and Scala implementation is only one.

          Show
          Sandro Martini added a comment - - edited Other things that could be a good addition: add a maximum number of elements (by default -1 or no limit) use case: for example, put a limit in how many objects can be put in the resourceCache (inside ApplicationContext), for example look here: http://stackoverflow.com/questions/5601333/limiting-the-max-size-of-a-hashmap-in-java add some utility class (see in what package, inside core) to implement the concept of null-safe data container (or monad, in Scala terms), and related concepts of Option, Some, None, etc as found in Scala. Some reference: http://www.codecommit.com/blog/scala/the-option-pattern/print/ , http://blog.tmorris.net/scalaoption-cheat-sheet/ verify if could be good to start retrofit existing behavior to use them, but probably not before release 3.0 ... add fallback methods like getOrElse in some Scala collections, some reference: http://blog.danielwellman.com/2010/08/more-thoughts-on-scalas-option-class.html add method to apply to any collection element a transformation (a Function, or maybe a Command), like apply() or apply(Object argument), some reference: http://stackoverflow.com/questions/9350528/how-to-work-with-javap-for-scala-java-interop Of course before all this I'll verify if/how we can do this (in Java), both as coding strategies (if there are some pitfalls, etc), and by licensing (should not exist problems here, but I'll do some check to be sure). Note that some things described here are patterns, and Scala implementation is only one.
          Hide
          Sandro Martini added a comment -

          Put question to Apache Legals, to be sure of what/how to do things, if needed (if we will include/implement this in Pivot)

          Show
          Sandro Martini added a comment - Put question to Apache Legals, to be sure of what/how to do things, if needed (if we will include/implement this in Pivot)
          Hide
          Sandro Martini added a comment -
          Show
          Sandro Martini added a comment - Some useful info on Scala Collections (etc): http://alexandre-masselot.blogspot.it/2013/08/scala-6-silver-bullets.html
          Hide
          Sandro Martini added a comment -

          Sample Groovy Monad example, check if port to Java and add to Pivot sources (in a new package, under core).

          Show
          Sandro Martini added a comment - Sample Groovy Monad example, check if port to Java and add to Pivot sources (in a new package, under core).
          Hide
          Roger Whitcomb added a comment -

          Did a little work here (in "trunk"):
          Sending core\src\org\apache\pivot\collections\ArrayList.java
          Sending core\src\org\apache\pivot\collections\HashMap.java
          Transmitting file data ..
          Committed revision 1587756.

          This change basically allows you to initialize either a Pivot ArrayList from a java.util.Collection or a Pivot HashMap from a java.util.Map. This facilitates (a little bit) the interoperation between the standard objects and our objects.

          Show
          Roger Whitcomb added a comment - Did a little work here (in "trunk"): Sending core\src\org\apache\pivot\collections\ArrayList.java Sending core\src\org\apache\pivot\collections\HashMap.java Transmitting file data .. Committed revision 1587756. This change basically allows you to initialize either a Pivot ArrayList from a java.util.Collection or a Pivot HashMap from a java.util.Map. This facilitates (a little bit) the interoperation between the standard objects and our objects.
          Hide
          Sandro Martini added a comment -

          Usually we put this kind of code in *Adapter classes, anyway for so-common cases I think it's a good shortcut to have directly inside collection classes .

          Just for Info: in Scala this kind of operations are done in companion classes, like static object factories ( http://keramida.wordpress.com/2013/06/19/factory-objects-in-scala-code/ , http://daily-scala.blogspot.it/2009/09/companion-object.html ) ... maybe we could implement something like this but in Java.

          Show
          Sandro Martini added a comment - Usually we put this kind of code in *Adapter classes, anyway for so-common cases I think it's a good shortcut to have directly inside collection classes . Just for Info: in Scala this kind of operations are done in companion classes, like static object factories ( http://keramida.wordpress.com/2013/06/19/factory-objects-in-scala-code/ , http://daily-scala.blogspot.it/2009/09/companion-object.html ) ... maybe we could implement something like this but in Java.
          Hide
          Roger Whitcomb added a comment -

          I totally understand the use of the Adapter classes, but there are cases where you start with a java.util data structure, but then want to manipulate the list with other things from the UI, so it is more appropriate to use the Pivot collection afterwards. Anyway, I liked how simple these changes were, so it seemed useful, and I did use it right away, and it simplified my code...

          Show
          Roger Whitcomb added a comment - I totally understand the use of the Adapter classes, but there are cases where you start with a java.util data structure, but then want to manipulate the list with other things from the UI, so it is more appropriate to use the Pivot collection afterwards. Anyway, I liked how simple these changes were, so it seemed useful, and I did use it right away, and it simplified my code...
          Hide
          Sandro Martini added a comment - - edited

          Hi Roger, mine wasn't a complaint, but only to remember the usage of Adapters (and some checks if doing something like Scala Companion classes could be useful even to us in this kind of operations, in the future) ... and of course any simplification of code is good

          Show
          Sandro Martini added a comment - - edited Hi Roger, mine wasn't a complaint, but only to remember the usage of Adapters (and some checks if doing something like Scala Companion classes could be useful even to us in this kind of operations, in the future) ... and of course any simplification of code is good
          Hide
          Roger Whitcomb added a comment -

          So, that's good. My actual use case was this:

          • Construct an empty Pivot Collections List
          • Add a (possibly translated) string for "Default" to the Pivot List
          • Add all the elements of another java.util.List of valid font names in a loop doing "add" to the Pivot List
          • Use the Pivot List as the data for a ListButton.

          With this constructor it simply becomes this:

          • Construct a new Pivot Collections List from the java.util.List of font names
          • Insert the "Default" string at index 0
          • Use the list as data for the ListButton

          So, several lines less code and much faster since we don't have the overhead of multiple (maybe 100s) of adds to the Pivot list.

          But, along the lines of the companion classes, I was wondering if we could create some generic factory methods that would automatically wrap things in a ListAdapter or MapAdapter using generics. Not sure exactly what that would look like, though. Is this kind of what you were thinking?

          Show
          Roger Whitcomb added a comment - So, that's good. My actual use case was this: Construct an empty Pivot Collections List Add a (possibly translated) string for "Default" to the Pivot List Add all the elements of another java.util.List of valid font names in a loop doing "add" to the Pivot List Use the Pivot List as the data for a ListButton. With this constructor it simply becomes this: Construct a new Pivot Collections List from the java.util.List of font names Insert the "Default" string at index 0 Use the list as data for the ListButton So, several lines less code and much faster since we don't have the overhead of multiple (maybe 100s) of adds to the Pivot list. But, along the lines of the companion classes, I was wondering if we could create some generic factory methods that would automatically wrap things in a ListAdapter or MapAdapter using generics. Not sure exactly what that would look like, though. Is this kind of what you were thinking?
          Hide
          Sandro Martini added a comment -

          Hi,
          > But, along the lines of the companion classes, I was wondering if we could create some generic factory methods that would automatically wrap things in a ListAdapter or MapAdapter using generics. Not sure exactly what that would look like, though. Is this kind of what you were thinking?
          yes, that was my main intent, but still I don't have a clear idea on how we could implement it (assuming it makes sense here) ... maybe later

          Show
          Sandro Martini added a comment - Hi, > But, along the lines of the companion classes, I was wondering if we could create some generic factory methods that would automatically wrap things in a ListAdapter or MapAdapter using generics. Not sure exactly what that would look like, though. Is this kind of what you were thinking? yes, that was my main intent, but still I don't have a clear idea on how we could implement it (assuming it makes sense here) ... maybe later
          Hide
          Roger Whitcomb added a comment -

          Made another constructor for ListAdapter that converts a regular Java array to a Pivot list:
          Sending core\src\org\apache\pivot\collections\adapter\ListAdapter.java
          Transmitting file data .
          Committed revision 1588304.

          Show
          Roger Whitcomb added a comment - Made another constructor for ListAdapter that converts a regular Java array to a Pivot list: Sending core\src\org\apache\pivot\collections\adapter\ListAdapter.java Transmitting file data . Committed revision 1588304.
          Hide
          Sandro Martini added a comment -

          Add other methods like those just added in some Java 8 collections, for example:
          http://marxsoftware.blogspot.it/2014/04/handy-new-map-default-methods-in-jdk-8.html

          Show
          Sandro Martini added a comment - Add other methods like those just added in some Java 8 collections, for example: http://marxsoftware.blogspot.it/2014/04/handy-new-map-default-methods-in-jdk-8.html
          Hide
          Sandro Martini added a comment -

          Classes that map functional interfaces, and Initial version of Option-like value handling.

          Unless objections I'll do the commit in next days ... at least for functional interfaces (all works even without Java 8), and see later on Option-like classes (related tests still to do).

          Show
          Sandro Martini added a comment - Classes that map functional interfaces, and Initial version of Option-like value handling. Unless objections I'll do the commit in next days ... at least for functional interfaces (all works even without Java 8), and see later on Option-like classes (related tests still to do).
          Hide
          Roger Whitcomb added a comment -

          I'm getting a couple of compile warnings on your latest code (in "trunk"):
          [javac] /Users/Roger/Projects/pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java:31: warning: [unchecked] unchecked conversion
          [javac] return INSTANCE;
          [javac] ^
          [javac] required: None<T>
          [javac] found: None
          [javac] where T is a type-variable:
          [javac] T extends Object declared in method <T>getInstance()
          [javac] /Users/Roger/Projects/pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java:31: warning: [unchecked] unchecked conversion
          [javac] return INSTANCE;
          [javac] ^
          [javac] required: OptionCompanion<T>
          [javac] found: OptionCompanion
          [javac] where T is a type-variable:
          [javac] T extends Object declared in method <T>getInstance()
          [javac] 2 warnings

          Show
          Roger Whitcomb added a comment - I'm getting a couple of compile warnings on your latest code (in "trunk"): [javac] /Users/Roger/Projects/pivot/trunk/core/src/org/apache/pivot/functional/monad/None.java:31: warning: [unchecked] unchecked conversion [javac] return INSTANCE; [javac] ^ [javac] required: None<T> [javac] found: None [javac] where T is a type-variable: [javac] T extends Object declared in method <T>getInstance() [javac] /Users/Roger/Projects/pivot/trunk/core/src/org/apache/pivot/functional/monad/OptionCompanion.java:31: warning: [unchecked] unchecked conversion [javac] return INSTANCE; [javac] ^ [javac] required: OptionCompanion<T> [javac] found: OptionCompanion [javac] where T is a type-variable: [javac] T extends Object declared in method <T>getInstance() [javac] 2 warnings
          Hide
          Sandro Martini added a comment -

          Hi Roger, thanks, I wasn't aware because eclipse says nothing on those warnings ... I just committed some small changes.
          Don't worry if all monad-related classes are a work-in-progress, in next weeks I'm planning to make them work good (and add some unit test), and if possible use even in some new methods in our colections.
          Or if we find that they are not so useful for us, we can remove them later (but they are in svn history at least) before the release.

          Thanks for now. Bye

          Show
          Sandro Martini added a comment - Hi Roger, thanks, I wasn't aware because eclipse says nothing on those warnings ... I just committed some small changes. Don't worry if all monad-related classes are a work-in-progress, in next weeks I'm planning to make them work good (and add some unit test), and if possible use even in some new methods in our colections. Or if we find that they are not so useful for us, we can remove them later (but they are in svn history at least) before the release. Thanks for now. Bye

            People

            • Assignee:
              Sandro Martini
              Reporter:
              Sandro Martini
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:

                Development