Created
July 4, 2017 13:08
-
-
Save cattaka/3fa0a44aacd7a0eca14e2022bab5dee8 to your computer and use it in GitHub Desktop.
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 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