Skip to content

Instantly share code, notes, and snippets.

View liviutudor's full-sized avatar

Liviu Tudor liviutudor

View GitHub Profile
@liviutudor
liviutudor / MapStream.java
Created April 1, 2017 00:50
`Map` on java streams
public Collection<Double> extract(Collection<Employee> collection) {
return collection.stream().map(c -> c.salary).collect(Collectors.toList());
}
@liviutudor
liviutudor / FindExtractMaxNoStreams.java
Created April 1, 2017 00:56
Find by name, extract salary and compute max in java, no streams
public Optional<Double> max(Collection<Double> collection) {
if (collection == null || collection.isEmpty()) return Optional.empty();
Iterator<Double> iter = collection.iterator();
Double max = iter.next();
while (iter.hasNext()) {
Double n = iter.next();
if (n > max) max = n;
}
@liviutudor
liviutudor / FindExtractMaxStreams.java
Last active April 1, 2017 01:06
Find elements, map and extract in java using streams
public Optional<Double> findExtractAndMax(Collection<Employee> collection, String name) {
return collection.stream().filter(e -> e.firstName.equals(name)).map(e -> e.salary).max((a, b) -> (int) Math.signum(a - b));
}
//usage
double sal = findExtractAndMax(collection, "Bob");
@liviutudor
liviutudor / Sample.java
Created June 28, 2017 07:05
Sample on how to process a http request sequentially in Java
@GET
public Response processRequest( @QueryParam("id")String id, @QueryParam("userId")String userId ) {
Record retrieve = database.retrieveRecord(id);
logger.writeEntryFor( id, userId );
UserProfile profile = userStore.retrieve(userId);
generateAdResponse(retrieve, profile);
}
@liviutudor
liviutudor / Sample.java
Created June 28, 2017 07:19
Writing the same code using RxJava Observable
@GET
public Observable<Response> processRequest( @QueryParam("id")String id, @QueryParam("userId")String userId ) {
return Observable.zip(
Observable.fromCallable(() -> database.retrieveRecord(id)),
Observable.fromCallable(() -> userStore.retrieve(userId)),
Observable.fromCallable(() -> logger.writeEntryFor(id, userId)),
this::generateAdResponse
);
}
@liviutudor
liviutudor / JacksonBean.java
Created September 16, 2017 00:29
Usage of Jackson annotations when removing setters and zero-args constructor
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
public class JacksonBean {
private final int value;
private final String another;
@JsonCreator
public JacksonBean(@JsonProperty("value") int value, @JsonProperty("another") String another) {
this.value = value;
@liviutudor
liviutudor / JacksonBean.java
Created September 16, 2017 00:47
Usage of ConstructorProperties annotation when removing setters and zero-args constructor
import java.beans.ConstructorProperties;
public class JacksonBean {
private final int value;
private final String another;
@ConstructorProperties({"value", "another"})
public JacksonBean(int value, String another) {
this.value = value;
this.another = another;
@liviutudor
liviutudor / BeanOne.groovy
Created September 21, 2017 05:45
Using @DeleGate in groovy, first bean
import groovy.transform.Sortable
import groovy.transform.TupleConstructor
@Sortable
@TupleConstructor
class BeanOne {
String firstName
String familyName
public String getFullName() {
@liviutudor
liviutudor / BeanTwo.groovy
Created September 21, 2017 05:46
Using @DeleGate in groovy, second bean
import groovy.transform.Sortable
import groovy.transform.TupleConstructor
@Sortable
@TupleConstructor
class BeanTwo {
int yob
public int getAge() {
new Date().year - yob
@liviutudor
liviutudor / DelegationBean.groovy
Last active September 22, 2017 05:48
Aggregation 2 classes with @DeleGate
class DelegationBean {
@Delegate
BeanOne person
@Delegate
BeanTwo dateOfBirth
static void main(String... args) {
DelegationBean one = new DelegationBean(person: new BeanOne("Liviu", "Tudor"), dateOfBirth: new BeanTwo(yob: 1975))
DelegationBean two = new DelegationBean(person: new BeanOne("William", "Shakespeare"), dateOfBirth: new BeanTwo(yob: 1564))