This is the old documentation of lombok-pg. The new version can be found in the wiki of the github repository. Take me to the new Version then!

@Function

Overview

This annotation allows for a class to be automatically wrapped as an anonymous class, allowing the caller to pass it as an argument to a method that accepts a 'function'.

With Lombok

01 import lombok.ExtensionMethod;
02 import lombok.Function;
03 import java.util.Arrays;
04 import lombok.Functions.Function1;
05 import static lombok.Yield.yield;
06 
07 @ExtensionMethod(Operations.class)
08 public class Main {
09 
10   public static void main(String[] args) {
11     new String[] { "john""james""eddie" }.toList().filter(startsWith("j")).map(toUpperCase()).print('-');
12     // outputs "JOHN-JAMES"
13   }
14 
15   @Function
16   private static boolean startsWith(String element, String _prefix) {
17     return element.startsWith(_prefix);
18   }
19 
20   @Function
21   private static String toUpperCase(String element) {
22     return element.toUpperCase();
23   }
24 }
25 
26 public class Operations {
27 
28   public static <T> Iterable<T> filter(final Iterable<T> list, final Function1<T, Boolean> filter) {
29     for (T element : listif (filter.apply(element)) yield(element);
30   }
31 
32   public static <S, T> Iterable<T> map(final Iterable<S> list, final Function1<S, T> mapping) {
33     for (S element : listyield(mapping.apply(element));
34   }
35 
36   public static <T> Iterable<T> toList(final T[] array) {
37     return Arrays.asList(array);
38   }
39 
40   public static <T> void print(final Iterable<T> list, final char delimiter) {
41     StringBuilder sb = new StringBuilder();
42     for (T element : list) {
43       sb.append(element.toString());
44       sb.append(delimiter);
45     }
46 
47     System.out.println(sb.length() ? sb.substring(0, sb.length() 1"");
48   }
49 }

Vanilla Java

001 import java.util.Arrays;
002 import lombok.Functions.Function1;
003 
004 public class Main {
005 
006   public static void main(String[] args) {
007 
008     Operations.print(Operations.map(Operations.filter(Operations.toList(new String[]{"john""james""eddie"}), startsWith("j")), toUpperCase())'-');
009     // outputs "JOHN-JAMES"
010   }
011 
012   @java.lang.SuppressWarnings("all")
013   private static lombok.Functions.Function1<String, java.lang.Boolean> startsWith(final String _prefix) {
014     return new lombok.Functions.Function1<String, java.lang.Boolean>(){
015       
016       public java.lang.Boolean apply(final String element) {
017         return element.startsWith(_prefix);
018       }
019     };
020   }
021 
022   @java.lang.SuppressWarnings("all")
023   private static lombok.Functions.Function1<String, String> toUpperCase() {
024     return new lombok.Functions.Function1<String, String>(){
025       
026       public String apply(final String element) {
027         return element.toUpperCase();
028       }
029     };
030   }
031 }
032 
033 public class Operations {
034 
035   @java.lang.SuppressWarnings("all")
036   public static <T> Iterable<T> filter(final Iterable<T> list, final Function1<T, Boolean> filter) {
037 
038     class $YielderFilter implements java.util.Iterator<T>, java.lang.Iterable<T> {
039       private T element;
040       private java.util.Iterator $elementIter;
041       private int $state;
042       private boolean $hasNext;
043       private boolean $nextDefined;
044       private T $next;
045 
046       public java.util.Iterator<T> iterator() {
047         return new $YielderFilter();
048       }
049 
050       public boolean hasNext() {
051         if (!$nextDefined) {
052           $hasNext = getNext();
053           $nextDefined = true;
054         }
055         return $hasNext;
056       }
057 
058       public T next() {
059         if (!hasNext()) {
060           throw new java.util.NoSuchElementException();
061         }
062         $nextDefined = false;
063         return $next;
064       }
065 
066       public void remove() {
067         throw new java.lang.UnsupportedOperationException();
068       }
069 
070       private boolean getNext() {
071         while (trueswitch ($state) {
072         case 0
073           $elementIter = list.iterator();
074         case 1
075           if (!$elementIter.hasNext()) {
076             $state = 3;
077             continue;
078           }
079           element = (T)$elementIter.next();
080           if (!(filter.apply(element))) {
081             $state = 2;
082             continue;
083           }
084           $next = element;
085           $state = 2;
086           return true;
087         case 2
088           $state = 1;
089           continue;
090         case 3
091         default
092           return false;
093         }
094       }
095     }
096     return new $YielderFilter();
097   }
098 
099   @java.lang.SuppressWarnings("all")
100   public static <S, T> Iterable<T> map(final Iterable<S> list, final Function1<S, T> mapping) {
101 
102     class $YielderMap implements java.util.Iterator<T>, java.lang.Iterable<T> {
103       private S element;
104       private java.util.Iterator $elementIter;
105       private int $state;
106       private boolean $hasNext;
107       private boolean $nextDefined;
108       private T $next;
109 
110       public java.util.Iterator<T> iterator() {
111         return new $YielderMap();
112       }
113 
114       public boolean hasNext() {
115         if (!$nextDefined) {
116           $hasNext = getNext();
117           $nextDefined = true;
118         }
119         return $hasNext;
120       }
121 
122       public T next() {
123         if (!hasNext()) {
124           throw new java.util.NoSuchElementException();
125         }
126         $nextDefined = false;
127         return $next;
128       }
129 
130       public void remove() {
131         throw new java.lang.UnsupportedOperationException();
132       }
133 
134       private boolean getNext() {
135         while (trueswitch ($state) {
136         case 0
137           $elementIter = list.iterator();
138         case 1
139           if (!$elementIter.hasNext()) {
140             $state = 3;
141             continue;
142           }
143           element = (S)$elementIter.next();
144           $next = mapping.apply(element);
145           $state = 2;
146           return true;
147         case 2
148           $state = 1;
149           continue;
150         case 3
151         default
152           return false;
153         }
154       }
155     }
156     return new $YielderMap();
157   }
158 
159   public static <T> Iterable<T> toList(final T[] array) {
160     return Arrays.asList(array);
161   }
162 
163   public static <T> void print(final Iterable<T> list, final char delimiter) {
164     StringBuilder sb = new StringBuilder();
165     for (T element : list) {
166       sb.append(element.toString());
167       sb.append(delimiter);
168     }
169     System.out.println(sb.length() ? sb.substring(0, sb.length() 1"");
170   }
171 }

Small print

You may pass functions to other functions, creating higher order functions and monads. Furthermore closures are possible, just prefix a function's argument name with an underscore (_) to specify, as seen in the example above.