Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8213112

Add the 'process' method to Stream class

    Details

      Description

      A DESCRIPTION OF THE PROBLEM :
      Currently, if one needs to skip some stage or provide a group of reused stages one to have to do the following:

      Stream<T> s = collection.stream()
           .map(a -> doIt(a, bb))
          .filter(a -> !a.list().isEmpty());
      s = useSecuriytFilter(s, session)
      return s.collect(Collector.toList());

      Or like:

      return useSecuriytFilter(collection.stream()
           .map(a -> doIt(a, bb))
          .filter(a -> !a.list().isEmpty())
          , session);
          .collect(Collector.toList());

      In both cases, readability is of code is not high, as fluent sequence of actions is interrupted.

      It is suggested to add "process" method to stream like the following signature:

      default <R> R process(Function<Stream<T>, R> body) {
         return body.apply(this);
      }

      Then custom processing could be applied like the following:

      return collection.stream()
          .map(a -> doIt(a, bb))
          .filter(a -> !a.list().isEmpty()
          .process( s -> useSecuriytFilter(s, session))
          .collect(Collector.toList());

      While semantics is the same, the code structure looks much better. Note this proposed change is not about less typing, but about making local processing looking more local, rather than affecting entire processing pipeline visually.

      The detailed description for motivation is here:

      https://dzone.com/articles/making-java-fluent-api-more-flexible

      Some other libraries with processing chain usage pattern like date-time classes in java.util.time could benefit from this too as common blocks of date arithmetic could be reused in more clean way.


        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                psonal Pallavi Sonal
                Reporter:
                webbuggrp Webbug Group
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: