title: How to suitably compose Predicate and Function in a Java function?
body: <p>The purpose is to create a new Predicate usable in a stream filter :</p>
  
  <pre><code>myCollectionOfElement
  .stream()
  .filter(
      MyStaticHelperClass.compose(MyStaticHelperClass.getSubElement1OfTheElement(),MyStaticHelperClass.getPredicate1OnSubElement1()))
  .sorted(MyStaticHelperClass.getOtherSubElement().reversed())
  .limit(10)
  .collect(Collectors.toList())
  </code></pre>
  
  <p><code>getSubElement1OfTheElement()</code> returns <code>Function&lt;E,S&gt;</code> (E contains a S property)
  <code>getPredicate1OnSubElement1()</code> returns <code>Predicate&lt;S&gt;</code></p>
  
  <p>I use static functions to expose method references and functions.
  I do this because the stream is called in a <strong>Velocity template</strong> and this context <strong>doesn't support lambda syntax and method reference</strong>.
  <strong>I don't want to create a static function for all possible combinaisons, so I really want them to be composable</strong>.</p>
  
  <p>For example here, I don't want to have a static <code>getPredicate1OnElementThatCheckProperty1OnTheSubElement1()</code> because I can compose <code>getSubElement1OfTheElement()</code> and <code>getPredicate1OnSubElement1()</code>.</p>
  
  <p>So I need a compose function :</p>
  
  <pre><code>// returns a new Predicate constructed by applying Predicate predicate on the result of Function function
  public static &lt;E,S&gt; Predicate&lt;E&gt; compose(Function&lt;E,S&gt; function, Predicate&lt;S&gt; predicate)
  
  // most intuitive : lambda
  return value -&gt; predicate.test(function.apply(value));
  
  // with method references
  return function.andThen(predicate::test)::apply;
  // predicate.compose is not available because Predicate interface doesn't extends Function interface
  </code></pre>
  
  <p>inspired by <a href="https://stackoverflow.com/questions/33300011/is-there-a-convenience-method-to-create-a-predicate-that-tests-if-a-field-equals/33308391#33308391">Is there a convenience method to create a Predicate that tests if a field equals a given value?</a></p>
  
  <pre><code>// step by step with variables
  Function &lt;S,Boolean&gt; predicateFunction = predicate::test;
  // a kind of @FunctionalInterface implicit "conversion" ? Predicate -&gt; Function.
  // is that safe ?
  
  Function &lt;E,Boolean&gt; composed = function.andThen(predicateFunction::apply);
  return composed::apply;
  </code></pre>
  
  <hr>
  
  <p>Edit : </p>
  
  <p>It's called a cast context : <a href="https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html</a></p>
  
  <pre><code>// the implementation of my compose can also be this ugly one-liner :
  return ((Function &lt;S,Boolean&gt;)predicate::test).compose(function)::apply;
  </code></pre>
  
  <p>So, we cannot implement a generic compose function taking any functional interface (in my case Function and Predicate) because the name of the abstract method differs for each interface (test and apply in my case).<br>
  I'm OK with that.</p>
  
  <p>To conclude, what I really need is two static functions, one that converts a Predicate to a Function and the opposite. Every Predicate will be used as a Function and the final operation will convert the composed Function to Predicate in order to match with the parameter type of the filter function.</p>
  
  <pre><code>public static &lt;S&gt; Function&lt;S,Boolean&gt; predicateToFunction(Predicate&lt;S&gt; predicate){
      return predicate::test;
  }
  public static &lt;S&gt; Predicate&lt;S&gt; functionToPredicate(Function&lt;S,Boolean&gt; function){
      return function::apply;
  }
  </code></pre>
  
  <hr>
  
  <p>Is that correct ?<br>
  If so, is there any interest in releasing the bounds in the function signature ?</p>