001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.util.task; 020 021/** 022 * A task that can be run and cancelled (if repeating) 023 */ 024public interface PlotSquaredTask extends Runnable { 025 026 /** 027 * Get a new {@link NullTask} 028 * 029 * @return Null task instance 030 */ 031 static NullTask nullTask() { 032 return new NullTask(); 033 } 034 035 /** 036 * Run the task. Don't override this, instead 037 * implement {@link #runTask()} 038 */ 039 @Override 040 default void run() { 041 if (isCancelled()) { 042 return; 043 } 044 this.runTask(); 045 } 046 047 /** 048 * Run the task 049 */ 050 void runTask(); 051 052 /** 053 * Check if the task has been cancelled 054 * 055 * @return {@code true} if the tasks is cancelled, 056 * {@code false} if not 057 */ 058 boolean isCancelled(); 059 060 /** 061 * Cancel the task 062 */ 063 void cancel(); 064 065 /** 066 * Task that does nothing and is always cancelled 067 */ 068 class NullTask implements PlotSquaredTask { 069 070 @Override 071 public void runTask() { 072 } 073 074 @Override 075 public boolean isCancelled() { 076 return true; 077 } 078 079 @Override 080 public void cancel() { 081 } 082 083 } 084 085}