Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created July 4, 2017 13:08
Show Gist options
  • Save cattaka/3fa0a44aacd7a0eca14e2022bab5dee8 to your computer and use it in GitHub Desktop.
Save cattaka/3fa0a44aacd7a0eca14e2022bab5dee8 to your computer and use it in GitHub Desktop.
package pattern3;
import java.util.*;
import java.util.function.Supplier;
/**
* Created by cattaka on 17/07/04.
* <p>
* Gist for https://twitter.com/Kory__3/status/882212409967919104
*/
public class MyCacheMapForJava8<K, V> {
public static class VoteCacheByScores extends MyCacheMapForJava8<VoteScore, Set<VotePoint>> {
public VoteCacheByScores() {
super(HashSet::new);
}
}
public static class UuidToPlayerVotesMap extends MyCacheMapForJava8<UUID, VoteCacheByScores> {
public UuidToPlayerVotesMap(Supplier<VoteCacheByScores> supplier) {
super(VoteCacheByScores::new);
}
}
public static class VotePointVotesCache extends MyCacheMapForJava8<VotePoint, Set<Vote>> {
public VotePointVotesCache() {
super(HashSet::new);
}
}
private Supplier<V> mSupplier;
private Map<K, V> mMap;
private MyCacheMapForJava8(Supplier<V> supplier) {
mSupplier = supplier;
mMap = new HashMap<>();
}
public V get(K k) {
V result = mMap.get(k);
if (result == null) {
// NOTE: Java 8ならmMap.computeIfAbsent()が使える
result = mSupplier.get();
mMap.put(k, result);
}
return result;
}
// TODO: 必要に応じてMapのメソッドをdelegateする
}
class VoteScore {
}
class VotePoint {
}
class Vote {
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment