Skip to content

Instantly share code, notes, and snippets.

@cattaka
Created July 4, 2017 13:07
Show Gist options
  • Select an option

  • Save cattaka/172ff264441969ef8c4d69e3887b1a82 to your computer and use it in GitHub Desktop.

Select an option

Save cattaka/172ff264441969ef8c4d69e3887b1a82 to your computer and use it in GitHub Desktop.
package pattern2;
import java.util.*;
/**
* Created by cattaka on 17/07/04.
* <p>
* Gist for https://twitter.com/Kory__3/status/882212409967919104
*/
public abstract class MyCacheMap<K, V> {
public static class VoteCacheByScores extends MyCacheMap<VoteScore, Set<VotePoint>> {
@Override
Set<VotePoint> createNewSet() {
return new HashSet<>();
}
}
public static class UuidToPlayerVotesMap extends MyCacheMap<UUID, VoteCacheByScores> {
@Override
VoteCacheByScores createNewSet() {
return new VoteCacheByScores();
}
}
public static class VotePointVotesCache extends MyCacheMap<VotePoint, Set<Vote>> {
@Override
Set<Vote> createNewSet() {
return new HashSet<>();
}
}
private Map<K, V> mMap;
private MyCacheMap() {
mMap = new HashMap<>();
}
public V get(K k) {
V result = mMap.get(k);
if (result == null) {
// NOTE: Java 8ならmMap.computeIfAbsent()が使える
result = createNewSet();
mMap.put(k, result);
}
return result;
}
abstract V createNewSet();
// 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