package holeg.model; import java.util.ArrayList; import java.util.Collection; import java.util.logging.Logger; import java.util.stream.Stream; import holeg.preferences.ImagePreference; import holeg.serialize.PostDeserialize; public class GroupNode extends AbstractCanvasObject implements PostDeserialize { private static final Logger log = Logger.getLogger(AbstractCanvasObject.class.getName()); private final ArrayList objectList = new ArrayList<>(); private final ArrayList switchList = new ArrayList<>(); private final ArrayList nodeList = new ArrayList<>(); private final ArrayList groupNodeList = new ArrayList<>(); public GroupNode(String nodeName) { super(nodeName); } @Override public String getImagePath() { return ImagePreference.Canvas.GroupNode; } public void addAll(Stream stream) { stream.forEach(this::add); } public void addAll(Collection collection) { collection.forEach(this::add); } public void add(AbstractCanvasObject object) { if (object instanceof HolonObject hObject) { objectList.add(hObject); }else if(object instanceof HolonSwitch hSwitch) { switchList.add(hSwitch); }else if(object instanceof Node node) { nodeList.add(node); }else if(object instanceof GroupNode groupNode) { groupNodeList.add(groupNode); } object.setGroupNode(this); } public void remove(AbstractCanvasObject object) { if (object instanceof HolonObject hObject) { objectList.remove(hObject); }else if(object instanceof HolonSwitch hSwitch) { switchList.remove(hSwitch); }else if(object instanceof Node node) { nodeList.remove(node); }else if(object instanceof GroupNode groupNode) { groupNodeList.remove(groupNode); } object.setGroupNode(null); } public void removeAll(Stream stream){ stream.forEach(this::remove); } public void removeAll(Collection collection){ collection.forEach(this::remove); } public Stream getObjectsInThisLayer() { return Stream.of(objectList.stream(), switchList.stream(), nodeList.stream(), groupNodeList.stream()) .flatMap(s -> s); } public Stream getAllObjectsRecursive() { Stream objects = Stream.of(objectList.stream(), switchList.stream(), nodeList.stream()) .flatMap(s -> s); return Stream.concat(objects, groupNodeList.stream().flatMap(GroupNode::getObjectsInThisLayer)); } public Stream getAllHolonObjectsRecursive() { return Stream.concat(objectList.stream(), groupNodeList.stream().flatMap(GroupNode::getAllHolonObjectsRecursive)); } public Stream getAllSwitchObjectsRecursive() { return Stream.concat(switchList.stream(), groupNodeList.stream().flatMap(GroupNode::getAllSwitchObjectsRecursive)); } public Stream getAllNodeObjectsRecursive() { return Stream.concat(nodeList.stream(), groupNodeList.stream().flatMap(GroupNode::getAllNodeObjectsRecursive)); } public Stream getAllGroupNodeObjectsRecursive() { return Stream.concat(groupNodeList.stream(), groupNodeList.stream().flatMap(GroupNode::getAllGroupNodeObjectsRecursive)); } public Stream getHolonObjects() { return objectList.stream(); } public Stream getSwitches() { return switchList.stream(); } public Stream getNodes() { return nodeList.stream(); } public Stream getGroupNodes() { return groupNodeList.stream(); } public Stream getHolonElements(){ return objectList.stream().flatMap(HolonObject::elementsStream); } public void clear() { objectList.clear(); switchList.clear(); nodeList.clear(); groupNodeList.clear(); } @Override public void postDeserialize() { getObjectsInThisLayer().forEach(obj -> obj.setGroupNode(this)); } }