From 59aa51e037b38d5d0557ef3ef091185c8c669760 Mon Sep 17 00:00:00 2001 From: sk89q Date: Sun, 30 Mar 2014 10:26:51 -0700 Subject: [PATCH] Added OperationQueue to execute multiple operations sequentially. --- .../function/operation/OperationQueue.java | 103 ++++++++++++++++++ 1 file changed, 103 insertions(+) create mode 100644 src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java diff --git a/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java b/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java new file mode 100644 index 000000000..0792394db --- /dev/null +++ b/src/main/java/com/sk89q/worldedit/function/operation/OperationQueue.java @@ -0,0 +1,103 @@ +/* + * WorldEdit, a Minecraft world manipulation toolkit + * Copyright (C) sk89q + * Copyright (C) WorldEdit team and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package com.sk89q.worldedit.function.operation; + +import com.sk89q.worldedit.WorldEditException; + +import java.util.ArrayDeque; +import java.util.Collection; +import java.util.Deque; + +import static com.google.common.base.Preconditions.checkNotNull; + +/** + * Executes multiple queues in order. + */ +public class OperationQueue implements Operation { + + private final Deque queue = new ArrayDeque(); + private Operation current; + + /** + * Create a new queue containing no operations. + */ + public OperationQueue() { + } + + /** + * Create a new queue with operations from the given collection. + * + * @param operations a collection of operations + */ + public OperationQueue(Collection operations) { + checkNotNull(operations); + for (Operation operation : operations) { + offer(operation); + } + } + + /** + * Create a new queue with operations from the given array. + * + * @param operation an array of operations + */ + public OperationQueue(Operation... operation) { + checkNotNull(operation); + for (Operation o : operation) { + offer(o); + } + } + + /** + * Add a new operation to the queue. + * + * @param operation the operation + */ + public void offer(Operation operation) { + checkNotNull(operation); + queue.offer(operation); + } + + @Override + public Operation resume() throws WorldEditException { + if (current == null && queue.size() > 0) { + current = queue.poll(); + } + + if (current != null) { + current = current.resume(); + + if (current == null) { + current = queue.poll(); + } + } + + return current != null ? this : null; + } + + @Override + public void cancel() { + for (Operation operation : queue) { + operation.cancel(); + } + queue.clear(); + } + +}