Last active
August 10, 2020 15:39
-
-
Save stefanofago73/e25910638d9286347e1c8e2db6efa133 to your computer and use it in GitHub Desktop.
This file contains 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
import static java.util.stream.Collectors.toList; | |
import java.util.ArrayList; | |
import java.util.Collection; | |
import java.util.List; | |
import java.util.Objects; | |
import java.util.function.Function; | |
public interface Initializer<R> { | |
public Collection<R> provide(); | |
static <T, R> Initializer<R> from(Collection<T> coll, Function<T, R> mapper) { | |
Objects.requireNonNull(coll, "collection"); | |
Objects.requireNonNull(mapper, "mapper"); | |
List<R> results = coll.stream().filter(Objects::nonNull).map(mapper).collect(toList()); | |
return () -> results; | |
} | |
static <T, R> Initializer<R> from(T[] coll, Function<T, R> mapper) { | |
Objects.requireNonNull(coll, "collection"); | |
Objects.requireNonNull(mapper, "mapper"); | |
ArrayList<R> results = new ArrayList<>(coll.length); | |
for (int i = 0, len = coll.length; i < len; results.add(mapper.apply(coll[i])), i++); | |
return () -> results; | |
} | |
default R[] provideArray(@SuppressWarnings("unchecked") R ... ignore) { | |
return provide().toArray(ignore); | |
} | |
} |
This file contains 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
import static java.util.Arrays.asList; | |
import java.util.Arrays; | |
import java.util.Collection; | |
import java.util.List; | |
public class InitializerUsageOnClass { | |
private Collection<String> families; | |
private Collection<Integer> paragraphSpacing = Initializer.from(new String[]{"I","II","III"}, String::length).provide(); | |
private Integer [] familyNamesLenght; | |
public InitializerUsageOnClass(List<Person> persons) { | |
families = Initializer.from(persons, Person::familyName).provide(); | |
familyNamesLenght = Initializer.from(families, String::length).provideArray(); | |
} | |
public Collection<String> families() { | |
return families; | |
} | |
public Integer [] familyNamesLength() { | |
return familyNamesLenght; | |
} | |
public Collection<Integer> paragraphSpacing() { | |
return paragraphSpacing; | |
} | |
public static void main(String[] args) { | |
List<Person> persons = asList( | |
new Person("John", "Black"), | |
new Person("Peter", "Red"), | |
new Person("Jack", "White")); | |
InitializerUsageOnClass tmp = new InitializerUsageOnClass(persons); | |
System.out.println(tmp.families()); | |
System.out.println(tmp.paragraphSpacing()); | |
System.out.println(Arrays.toString(tmp.familyNamesLength())); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment