Created
October 12, 2016 23:55
-
-
Save garcia-jj/01b8bf32ce506099dd544a761780d976 to your computer and use it in GitHub Desktop.
Translating beans
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package siscom.model.pg; | |
import static java.util.stream.Collectors.collectingAndThen; | |
import static java.util.stream.Collectors.toList; | |
import java.util.stream.Collector; | |
public final class Collectors { | |
private Collectors() { | |
throw new UnsupportedOperationException(); | |
} | |
public static final <T, A, R> Collector<T, A, R> toSerializableList() { | |
return (Collector<T, A, R>) collectingAndThen(toList(), e -> new SerializableArrayList<>(e)); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package siscom.model.transform; | |
import static java.util.stream.Collectors.toList; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.function.Function; | |
import siscom.model.pg.Collectors; | |
import siscom.model.pg.PaginatedList; | |
import siscom.model.pg.SerializableList; | |
import siscom.model.pg.SerializablePaginatedList; | |
public final class Translator { | |
private Translator() { | |
throw new UnsupportedOperationException(); | |
} | |
public static <F, T> T translate(final F from, final Function<F, T> translator) { | |
return Optional.ofNullable(from).map(translator).orElse(null); | |
} | |
public static <F, T> SerializablePaginatedList<T> translate(final SerializablePaginatedList<F> source, | |
final Function<F, T> translator) { | |
final List<T> out = source.stream().map(translator).collect(toList()); | |
return PaginatedList.newList(source.getTotalSize(), source.getCurrentPage(), source.getTotalPages(), source.getFirstResult(), out); | |
} | |
public static <F, T> SerializableList<T> translate(final List<F> source, final Function<F, T> translator) { | |
if (source == null) { | |
return SerializableList.empty(); | |
} | |
if (source instanceof SerializablePaginatedList) { | |
return translate((SerializablePaginatedList<F>) source, translator); | |
} | |
return source.stream().map(translator).collect(Collectors.toSerializableList()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment