Commits

Micha Kops committed 30bd545 Draft

Funcito examples added.

Comments (0)

Files changed (3)

 			<artifactId>commons-collections</artifactId>
 			<version>3.2.1</version>
 		</dependency>
+		<dependency>
+			<groupId>org.codehaus.jedi</groupId>
+			<artifactId>jedi-core</artifactId>
+			<version>3.0.5</version>
+		</dependency>
+		<dependency>
+			<groupId>org.codehaus.jedi</groupId>
+			<artifactId>jedi-annotations</artifactId>
+			<version>3.0.5</version>
+		</dependency>
+		<dependency>
+			<groupId>com.googlecode.funcito</groupId>
+			<artifactId>funcito</artifactId>
+			<version>1.2.0</version>
+		</dependency>
 	</dependencies>
 	<repositories>
 		<repository>

src/main/java/com/hascode/tutorial/FuncitoExample1.java

+package com.hascode.tutorial;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.funcito.FuncitoGuava.callsTo;
+import static org.funcito.FuncitoGuava.predicateFor;
+
+import java.util.List;
+
+import com.google.common.base.Predicate;
+import com.google.common.collect.Iterables;
+
+public class FuncitoExample1 {
+	public static void main(final String[] args) {
+		List<User> users = newArrayList(new User("Bob", false), new User("Tim",
+				true), new User("Suz", true), new User("Charlize", false),
+				new User("Allan", false), new User("Steve", true));
+
+		Predicate<User> activeUsers = predicateFor(callsTo(User.class)
+				.isActive());
+
+		System.out.println("active users are:");
+		System.out.println(Iterables.filter(users, activeUsers).toString());
+	}
+
+	private static class User {
+		private final String name;
+		private final boolean active;
+
+		public User(final String name, final boolean active) {
+			this.name = name;
+			this.active = active;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public boolean isActive() {
+			return active;
+		}
+
+		@Override
+		public String toString() {
+			return "User [name=" + name + ", active=" + active + "]";
+		}
+	}
+}

src/main/java/com/hascode/tutorial/FuncitoExample2.java

+package com.hascode.tutorial;
+
+import static com.google.common.collect.Lists.newArrayList;
+import static org.funcito.Funcito.callsTo;
+import static org.funcito.FuncitoGuava.functionFor;
+
+import java.util.List;
+
+import com.google.common.base.Function;
+import com.google.common.collect.Lists;
+
+public class FuncitoExample2 {
+	public static void main(final String[] args) {
+		List<User> users = newArrayList(new User("Bob", false), new User("Tim",
+				true), new User("Suz", true), new User("Charlize", false),
+				new User("Allan", false), new User("Steve", true));
+
+		System.out.println("user names:");
+		Function<User, String> namesFunc = functionFor(callsTo(User.class)
+				.getName());
+		List<String> userNames = Lists.transform(users, namesFunc);
+		System.out.println(userNames);
+	}
+
+	private static class User {
+		private final String name;
+		private final boolean active;
+
+		public User(final String name, final boolean active) {
+			this.name = name;
+			this.active = active;
+		}
+
+		public String getName() {
+			return name;
+		}
+
+		public boolean isActive() {
+			return active;
+		}
+
+		@Override
+		public String toString() {
+			return "User [name=" + name + ", active=" + active + "]";
+		}
+	}
+}