Created
July 4, 2017 13:08
-
-
Save cattaka/241237fb1206541152a1f610ef07fc0d 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 pattern4; | |
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 MyCacheMapForJava6<K, V> { | |
public static class VoteCacheByScores extends MyCacheMapForJava6<VoteScore, Set<VotePoint>> { | |
public VoteCacheByScores() { | |
super((Class<? extends Set<VotePoint>>) HashSet.class); // 無理やり、、、 | |
} | |
} | |
public static class UuidToPlayerVotesMap extends MyCacheMapForJava6<UUID, VoteCacheByScores> { | |
public UuidToPlayerVotesMap(Supplier<VoteCacheByScores> supplier) { | |
super(VoteCacheByScores.class); | |
} | |
} | |
public static class VotePointVotesCache extends MyCacheMapForJava6<VotePoint, Set<Vote>> { | |
public VotePointVotesCache() { | |
super((Class<? extends Set<Vote>>) HashSet.class); // 無理やり、、、 | |
} | |
} | |
private Class<? extends V> mClass; | |
private Map<K, V> mMap; | |
private MyCacheMapForJava6(Class<? extends V> clazz) { | |
mClass = clazz; | |
mMap = new HashMap<>(); | |
} | |
public V get(K k) { | |
V result = mMap.get(k); | |
if (result == null) { | |
// NOTE: Java 8ならmMap.computeIfAbsent()が使える | |
try { | |
result = mClass.newInstance(); // 正直、怖いのであんまりやりたくない | |
} catch (InstantiationException | IllegalAccessException e) { | |
throw new RuntimeException(e); // 無いはずだけど、、、 | |
} | |
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