private void doAddToStack(LinkedList<UndoableGroup> stack, UndoableGroup group, int limit) { if (!group.isUndoable() && stack.isEmpty()) return; stack.addLast(group); while (stack.size() > limit) { stack.removeFirst(); } }