import java.util.HashMap; import java.util.HashSet; import java.util.Map; import java.util.Map.Entry; import java.util.Set; import java.util.concurrent.atomic.AtomicInteger; public class Environment { Set living = new HashSet(); Map neighbours = new HashMap<>(); public Environment(Set ps) { living = ps; } public Environment next() { Environment ret = new Environment(new HashSet()); for (Entry entry : neighbours.entrySet()) { if (entry.getValue().get() == 3 || (entry.getValue().get() == 2 && living.contains(entry.getKey()))) { ret.living.add(entry.getKey()); } } return ret; } public void increment(Point p) { AtomicInteger val = neighbours.get(p); if (val == null) { synchronized (neighbours) { val = neighbours.get(p); if (val == null) { neighbours.put(p, new AtomicInteger(1)); } else { val.getAndIncrement(); } } } else { val.getAndIncrement(); } } public Set getPoints() { return living; } }