60 lines
1.5 KiB
Java
60 lines
1.5 KiB
Java
|
|
package fixtures;
|
||
|
|
|
||
|
|
import java.util.*;
|
||
|
|
import java.util.stream.*;
|
||
|
|
|
||
|
|
public class Valid {
|
||
|
|
public static class Person {
|
||
|
|
private final int id;
|
||
|
|
private final String name;
|
||
|
|
private final String email;
|
||
|
|
|
||
|
|
public Person(int id, String name, String email) {
|
||
|
|
this.id = id;
|
||
|
|
this.name = name;
|
||
|
|
this.email = email;
|
||
|
|
}
|
||
|
|
|
||
|
|
public int getId() { return id; }
|
||
|
|
public String getName() { return name; }
|
||
|
|
public String getEmail() { return email; }
|
||
|
|
|
||
|
|
@Override
|
||
|
|
public String toString() {
|
||
|
|
return name + " <" + email + ">";
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static class Repository<T> {
|
||
|
|
private final Map<Integer, T> items = new HashMap<>();
|
||
|
|
|
||
|
|
public void add(int key, T item) {
|
||
|
|
items.put(key, item);
|
||
|
|
}
|
||
|
|
|
||
|
|
public Optional<T> find(int key) {
|
||
|
|
return Optional.ofNullable(items.get(key));
|
||
|
|
}
|
||
|
|
|
||
|
|
public List<T> getAll() {
|
||
|
|
return new ArrayList<>(items.values());
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public static int add(int a, int b) {
|
||
|
|
return a + b;
|
||
|
|
}
|
||
|
|
|
||
|
|
public static void main(String[] args) {
|
||
|
|
Repository<Person> repo = new Repository<>();
|
||
|
|
repo.add(1, new Person(1, "Alice", "alice@example.com"));
|
||
|
|
repo.add(2, new Person(2, "Bob", "bob@example.com"));
|
||
|
|
|
||
|
|
repo.getAll().stream()
|
||
|
|
.map(Person::getName)
|
||
|
|
.forEach(System.out::println);
|
||
|
|
|
||
|
|
System.out.println("3 + 4 = " + add(3, 4));
|
||
|
|
}
|
||
|
|
}
|