This project consists of some useful helper classes and interfaces, which help to write more functional Java code, even in pre-Java8 environments.
This project consists of some useful helper classes and interfaces, which help to write more functional Java code.
The classes are:
-
FList
- contains static methods returning mostlyjava.util.List
s and acceptingIterable
s and arrays, likemap
,flatMap
,filter
, … -
F0
,F1
,F2
,F3
,F4
- Interfaces representing a function with n args where n is given by the interface name suffix -
CheckedF0
,CheckedF1
- Interfaces representing a function with n args throwing an Exception where n is given by the interface name suffix -
Procedure0
,Procedure1
,Procedure2
,Procedure3
,Procedure4
- Interfaces representing a procedure with n args where n is given by the interface name suffix -
Optional
- An immutable class representing an optional value -
Either
- An immutable class representing either one of two values -
Tuple2
,Tuple3
, … - Classes representing tuples with n args where n is given by the name -
Try
- An experimental wrapper around computations, that may throw exceptions -
CanEqualsSupport
- An helper class to produce properequals
andhashCode
as well ascanEqual
methods.
All those classes and interfaces can be used with Java 8 and above. Releases up until 2.3.0
also supported Java 5+. Although by their use, your code will not get more compact, but hopefully less errorprone.
There are a lot of more sophisticated functional libraries for Java8+ around, but they all cannot be used with older Java versions. Therefore, the historical aim of this library was to stay Java5+ compatible.
Of course, programming with lambda expressions and anonymous classes in older Java versions isn’t much fun. Please read the Java8 sections below, to learn how to code in Java8 source level but compile and convert Bytecode to older Java versions transparently.
import java.util.*;
import de.tototec.utils.functional.*;
import static de.tototec.utils.functional.FList.*;
List<String> names = Arrays.asList("Alice", "Bob", "Carlie", "Dylan");
// --> [Alice, Bob, Carlie, Dylan]
// Filtering
List<String> namesWithE_ = filter(names, name -> name.contains("e"));
// --> [Alice, Carlie]
// Apply a function to all elements
List<String> namesUppercase_ = map(names, name -> name.toUppercase());
// --> [ALICE, BOB, CARLIE, DYLAN]
2.3.0
)import java.util.*;
import de.tototec.utils.functional.*;
import static de.tototec.utils.functional.FList.*;
List<String> names = Arrays.asList("Alice", "Bob", "Carlie", "Dylan");
// --> [Alice, Bob, Carlie, Dylan]
// Filtering
List<String> namesWithE = filter(names, new F1<String, Boolean>() {
public Boolean apply(String name) {
return name.contains("e");
}
});
// --> [Alice, Carlie]
// Apply a function to all elements
List<String> namesUppercase = map(names, new F1<String, String>() {
public String apply(String name) {
return name.toUppercase();
}
});
// --> [ALICE, BOB, CARLIE, DYLAN]
Functional Utils is available from Maven central repository.
<dependency>
<groupId>de.tototec</groupId>
<artifactId>de.tototec.utils.functional</artifactId>
<version>2.3.0</version>
</dependency>
"de.tototec" % "de.tototec.utils.functional" % "2.3.0"
ivy"de.tototec:de.tototec.utils.functional:2.3.0"
Writing functional code can be much more pleasant if Java 8 Lambda expressions can be used. The function and procedure interfaces all fulfil the requirements for functional interfaces and can be thus implicitly inferred when lambda expressions are used.
Please refer to an older version of this Readme (e.g. 2.3.0
) for this information.
-
Added
FList.append
andFList.prepend
-
Added
FList.toList
-
Added
Optional.of
as a preferred alternative toOptional.lift
-
Added
Procedure4
-
Added more
FList.toHashMap
variants -
Deprecated
Optional.lift
in favour ofOptional.of
-
Changed signature of
Optional.orElseF
-
Made classes
Either
andTry
final
-
Made some classes and methods final
-
Changed from Default JavaDoc processor to Asciidoclet processor
-
Migrated Maven build file (
pom.xml
) to Polyglot Scala Extension (pom.scala
) -
Added extractor methods
Tuple2.extractA
andTuple2.extractB
-
Removed deprecated
Optional.getOrElse
override forF0
-
Changed thrown exceptions from
NullPointerException
toNoSuchElementException
, e.g. inOptional.get
orEither.right
-
Added
Tuple5
-
Added experimental
Try
-
Added
CheckedF0
andCheckedF1
as throwing couterpart ofF0
andF1
-
Added
Either.leftTry
andEither.rightTry
-
Added
FList.drop
-
Added
FList.take
-
Fixed faulty
FList.distict
method -
Added no-op impls for Procedure interfaces
-
Improved / fixed signature of
FList.flatten
-
Changed
toString
ofOptional
-
Added
FList.headOption
-
Added
FList.tail
-
Added
Optional.getOrElseF
-
Added
Optional.orElse
andOptional.orElseF
-
Added
FList.filterNotNull
-
Added
FList.concat
-
Changed order of type parameters in methods of FList
-
Added Optional.getOrElse
-
Added Optional.foreach
-
Added FList.contains
-
Added FList.containsAll
-
Added FList.toHashMap