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.plot.flag.types;
020
021import com.plotsquared.core.configuration.caption.Caption;
022import com.plotsquared.core.configuration.caption.TranslatableCaption;
023import com.plotsquared.core.plot.flag.FlagParseException;
024import com.plotsquared.core.plot.flag.PlotFlag;
025import org.checkerframework.checker.nullness.qual.NonNull;
026
027import java.util.Arrays;
028import java.util.Collection;
029import java.util.Locale;
030
031public abstract class BooleanFlag<F extends PlotFlag<Boolean, F>> extends PlotFlag<Boolean, F> {
032
033    private static final Collection<String> positiveValues =
034            Arrays.asList("1", "yes", "allow", "true");
035    private static final Collection<String> negativeValues =
036            Arrays.asList("0", "no", "deny", "disallow", "false");
037
038    /**
039     * Construct a new flag instance.
040     *
041     * @param value       Flag value
042     * @param description Flag description
043     */
044    protected BooleanFlag(final boolean value, final Caption description) {
045        super(value, TranslatableCaption.of("flags.flag_category_boolean"), description);
046    }
047
048    /**
049     * Construct a new boolean flag, with
050     * {@code false} as the default value.
051     *
052     * @param description Flag description
053     */
054    protected BooleanFlag(final Caption description) {
055        this(false, description);
056    }
057
058    @Override
059    public F parse(@NonNull String input) throws FlagParseException {
060        if (positiveValues.contains(input.toLowerCase(Locale.ENGLISH))) {
061            return this.flagOf(true);
062        } else if (negativeValues.contains(input.toLowerCase(Locale.ENGLISH))) {
063            return this.flagOf(false);
064        } else {
065            throw new FlagParseException(this, input, TranslatableCaption.of("flags.flag_error_boolean"));
066        }
067    }
068
069    @Override
070    public F merge(@NonNull Boolean newValue) {
071        return this.flagOf(getValue() || newValue);
072    }
073
074    @Override
075    public String getExample() {
076        return "true";
077    }
078
079    @Override
080    public String toString() {
081        return this.getValue().toString();
082    }
083
084    @Override
085    public Collection<String> getTabCompletions() {
086        return Arrays.asList("true", "false");
087    }
088
089}