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
021import com.google.common.base.Objects;
022import org.checkerframework.checker.index.qual.NonNegative;
023import org.checkerframework.checker.nullness.qual.NonNull;
024
025/**
026 * Task timings
027 */
028public final class TaskTime {
029
030    private final long time;
031    private final TaskUnit unit;
032
033    private TaskTime(@NonNegative final long time, final @NonNull TaskUnit unit) {
034        this.time = time;
035        this.unit = unit;
036    }
037
038    /**
039     * Create a new task time in seconds
040     *
041     * @param seconds Seconds
042     * @return Created task time instance
043     */
044    public static @NonNull TaskTime seconds(@NonNegative final long seconds) {
045        return new TaskTime(seconds * 1000L, TaskUnit.MILLISECONDS);
046    }
047
048    /**
049     * Create a new task time in server ticks
050     *
051     * @param ticks Server ticks
052     * @return Created task time instance
053     */
054    public static @NonNull TaskTime ticks(@NonNegative final long ticks) {
055        return new TaskTime(ticks, TaskUnit.TICKS);
056    }
057
058    /**
059     * Create a new task time in milliseconds
060     *
061     * @param ms Milliseconds
062     * @return Created task time instance
063     */
064    public static @NonNull TaskTime ms(@NonNegative final long ms) {
065        return new TaskTime(ms, TaskUnit.MILLISECONDS);
066    }
067
068    /**
069     * Get the task time
070     *
071     * @return Task time
072     */
073    @NonNegative
074    public long getTime() {
075        return this.time;
076    }
077
078    /**
079     * Get the time unit
080     *
081     * @return Time unit
082     */
083    public @NonNull TaskUnit getUnit() {
084        return this.unit;
085    }
086
087    @Override
088    public boolean equals(final Object o) {
089        if (this == o) {
090            return true;
091        }
092        if (o == null || getClass() != o.getClass()) {
093            return false;
094        }
095        final TaskTime taskTime = (TaskTime) o;
096        return getTime() == taskTime.getTime() && getUnit() == taskTime.getUnit();
097    }
098
099    @Override
100    public int hashCode() {
101        return Objects.hashCode(getTime(), getUnit());
102    }
103
104
105    public enum TaskUnit {
106        TICKS,
107        MILLISECONDS
108    }
109
110
111    public interface TimeConverter {
112
113        /**
114         * Convert from milliseconds to server ticks
115         *
116         * @param ms Milliseconds
117         * @return Server ticks
118         */
119        @NonNegative
120        long msToTicks(@NonNegative final long ms);
121
122        /**
123         * Convert from server ticks to milliseconds
124         *
125         * @param ticks Server ticks
126         * @return Milliseconds
127         */
128        @NonNegative
129        long ticksToMs(@NonNegative final long ticks);
130
131        /**
132         * Convert the task time to server ticks
133         *
134         * @param taskTime Task time
135         * @return Server ticks
136         */
137        @NonNegative
138        default long toTicks(final @NonNull TaskTime taskTime) {
139            if (taskTime.getUnit() == TaskUnit.TICKS) {
140                return taskTime.getTime();
141            } else {
142                return this.msToTicks(taskTime.getTime());
143            }
144        }
145
146        /**
147         * Convert the task time to milliseconds
148         *
149         * @param taskTime Task time
150         * @return Milliseconds
151         */
152        @NonNegative
153        default long toMs(final @NonNull TaskTime taskTime) {
154            if (taskTime.getUnit() == TaskUnit.MILLISECONDS) {
155                return taskTime.getTime();
156            } else {
157                return this.ticksToMs(taskTime.getTime());
158            }
159        }
160
161    }
162
163}