Created
November 26, 2020 11:46
-
-
Save anton0xf/3d011d96bee786add345251baa7ab86b 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 java.io.IOException; | |
import java.util.HashSet; | |
import java.util.List; | |
import java.util.Set; | |
import java.util.function.Function; | |
import java.util.stream.Collectors; | |
import com.fasterxml.jackson.databind.JsonNode; | |
import com.fasterxml.jackson.databind.ObjectMapper; | |
class Main { | |
static class Loader<T> { | |
Client client; | |
Function<String, T> parser; | |
public Loader(Client client, Function<String, T> parser) { | |
this.client = client; | |
this.parser = parser; | |
} | |
public List<T> list(List<Long> ids) { | |
return ids.stream() | |
.map(this::get) | |
.collect(Collectors.toList()); | |
} | |
public T get(Long id) { | |
String json = client.get(id); | |
return parser.apply(json); | |
} | |
} | |
static class BatchLoader<T> { | |
Client client; | |
Function<String, T> parser; | |
public BatchLoader(Client client, Function<String, T> parser) { | |
this.client = client; | |
this.parser = parser; | |
} | |
public List<T> list(List<Long> ids) { | |
Set<Long> set = new HashSet<Long>(ids); | |
return client.getAll().stream() | |
.map(line -> parser.apply(line)) | |
.filter(set::contains) | |
.collect(Collectors.toList()); | |
} | |
} | |
interface Parser { | |
long readLong(String input, String path); | |
String readString(String input, String path); | |
} | |
static class AJsonParser implements Parser { | |
ObjectMapper mapper; | |
@Override | |
public long readLong(String input, String path) { | |
JsonNode node = mapper.readValue(json, JsonNode.class); | |
return node.get(path).asLong(); | |
} | |
@Override | |
public String readString(String input, String path) { | |
JsonNode node = mapper.readValue(json, JsonNode.class); | |
return node.get(path).asText(); | |
} | |
} | |
static class CsvParser implements Parser { | |
} | |
static class TopicLoader { | |
TopicClient client = new TopicClient(); | |
ObjectMapper mapper; | |
Loader<Topic> loader = new Loader<Topic>(client, this::parse); | |
public List<Topic> list(List<Long> ids) { | |
return loader.list(ids); | |
} | |
public Main.Topic parse(String json) { | |
try { | |
JsonNode node = mapper.readValue(json, JsonNode.class); | |
long id = node.get("id").asLong(); | |
String title = node.get("title").asText(); | |
return new Topic(id, title); | |
} catch (IOException e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} | |
static class UserLoader { | |
Parser parser; | |
TopicClient client = new TopicClient(); | |
BatchLoader<User> loader = new BatchLoader<User>(client, this::parse); | |
public List<User> list(List<Long> ids) { | |
return loader.list(ids); | |
} | |
public Main.User parse(String line) { | |
long id = parser.readLong(line, "id"); | |
String firstName = parser.readString(line, "fname"); | |
String lastName = parser.readString(line, "lname"); | |
return new User(id, firstName, lastName); | |
} | |
} | |
static class User { | |
long id; | |
String firstName; | |
String lastName; | |
public User(long id, String firstName, String lastName) { | |
this.id = id; | |
this.firstName = firstName; | |
this.lastName = lastName; | |
} | |
} | |
static class Topic { | |
long id; | |
String title; | |
public Topic(long id, String title) { | |
this.id = id; | |
this.title = title; | |
} | |
} | |
interface Client { | |
String get(long id); | |
List<String> getAll(); | |
} | |
static class UserClient implements Client { | |
public String get(long id) { | |
return "" + id; | |
} | |
@Override | |
public List<String> getAll() { | |
return List.of("...."); | |
} | |
} | |
static class TopicClient implements Client { | |
public String get(long id) { | |
return "" + id; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://checkvist.com/checklists/753734