@ladislavGazo
gazo@seges.sk
(2006 - 2015)
Function<Integer, Integer> add = x -> x + 1;
Integer result = add.apply(4);
Function<Integer, Integer> add = MathUtils::add;
notable:
- calculate current user and if not empty, set it
- without object hell
- not to repeat everywhere this:
String username = Session.getCurrentUsername();
if(username != null || !username.isEmpty()) {
map.put(key, username);
}
and again, but different
String username = Session.getCurrentUsername();
if(username != null || !username.isEmpty()) {
manager.call(username);
}
securityManager.setCurrentUsername(
user -> variables.put(ProcessVariableConstant.USERNAME, user)
);
reminds OO-approach a bit
public class SecurityManager {
...
public void setCurrentUsername(VoidFunction f) {
String username = getCurrentUsername();
if(username != null || !username.isEmpty()) {
f.run(username);
}
}
@FunctionalInterface
public interface VoidFunction {
void run(T input);
}
}
public class SecurityManager {
...
public void setCurrentUsername(Consumer f) {
String username = getCurrentUsername();
if(username != null || !username.isEmpty()) {
f.accept(username);
}
}
}
Optional<Integer> userId = repository.findOne("john");
Optional opt = Optional.of(nonNullInteger);
Optional opt = Optional.ofNullable(possiblyNullInteger);
Optional empty = Optional.empty();
if ( opt.isPresent() ) {
service.store(opt.orElse(new UserAccount()));
}
opt.ifPresent( obj -> service.store(obj.get()) );
List<FormProcessDefinition> processDefinitions = form.getProcessDefinitions();
a.k.a. MAP
processDefinitions.stream().map((definition) -> {
ProcessDefinition processDefinition = new ProcessDefinition();
processDefinition.setKey(definition.getKey());
processDefinition.setName(definition.getName());
return processDefinition;
}).collect(Collectors.toList());
Set<String> roleSystemNames = getRequiredRoleNames();
roleSystemNames.
forEach(roleSystemName -> {
...
});
or if function exists for that
Set roleSystemNames = getRequiredRoleNames();
roleSystemNames.forEach(service::store);
Map
Map<String, JSONObject> jsonVariables = new HashMap<>();
variables.forEach((k, v) -> {
jsonVariables.put(k, createTypedValue(v));
});
but maybe for parallel processing
Stream will be more efficient
!
with streams
=
more pleasure
Files.list(new File(".").toPath())
.filter(p -> !p.getFileName().toString().startsWith("."))
.limit(3)
.forEach(System.out::println);
... and reduce ... and limit example
try {
FileInputStream fstream = new FileInputStream("textfile.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String strLine;
while ((strLine = br.readLine()) != null) {
...
}
in.close();
}catch (Exception e) {
...
}
try (Stream<String> stream = Files.lines(Paths.get("my.json"))) {
stream
.filter(line -> line.contains("name"))
.map(String::trim)
.forEach(System.out::println);
}
... with automatic file closing ...
String.join(":","Java","Group","2015");
// Java:Group:2015
Pattern pattern = Pattern.compile(".*@seges\\.sk");
Stream.of("gazo@seges.sk", "john@hotmail.com")
.filter(pattern.asPredicate())
.count();
// 1
Pattern.compile(";")
.splitAsStream("javagroup;java;group")
.filter(s -> s.contains("group"))
.sorted()
.collect(Collectors.joining(":"));
// => group:javagroup