forked from SteamWar/SteamWar
Fix some more issues
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
/*
|
||||
* This file is a part of the SteamWar software.
|
||||
*
|
||||
* Copyright (C) 2020 SteamWar.de-Serverteam
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Affero 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 Affero General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Affero General Public License
|
||||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
package de.steamwar.bausystem.region;
|
||||
|
||||
import de.steamwar.bausystem.region.flags.Flag;
|
||||
import lombok.AccessLevel;
|
||||
import lombok.AllArgsConstructor;
|
||||
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Optional;
|
||||
|
||||
@AllArgsConstructor(access = AccessLevel.PRIVATE)
|
||||
public class FlagOptional<T extends Enum<T> & Flag.Value<T>> {
|
||||
|
||||
private final Flag<T> flag;
|
||||
private final T value;
|
||||
|
||||
public static <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> of(Flag<T> flag) {
|
||||
return new FlagOptional<>(flag, null);
|
||||
}
|
||||
|
||||
public static <T extends Enum<T> & Flag.Value<T>> FlagOptional<T> of(Flag<T> flag, T value) {
|
||||
return new FlagOptional<>(flag, value);
|
||||
}
|
||||
|
||||
public boolean isPresent() {
|
||||
return value != null;
|
||||
}
|
||||
|
||||
public boolean isEmpty() {
|
||||
return value == null;
|
||||
}
|
||||
|
||||
public boolean is(T value) {
|
||||
if (isEmpty()) return false;
|
||||
return this.value.equals(value);
|
||||
}
|
||||
|
||||
public boolean isWithDefault(T value) {
|
||||
if (isEmpty()) {
|
||||
return flag.getDefaultValue().equals(value);
|
||||
} else {
|
||||
return this.value.equals(value);
|
||||
}
|
||||
}
|
||||
|
||||
public T get() {
|
||||
if (isEmpty()) {
|
||||
throw new NoSuchElementException("No value present");
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
public T getWithDefault() {
|
||||
if (isEmpty()) {
|
||||
return flag.getDefaultValue();
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public T orElse(T defaultValue) {
|
||||
if (isEmpty()) {
|
||||
return defaultValue;
|
||||
} else {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<String> name() {
|
||||
if (isEmpty()) {
|
||||
return Optional.empty();
|
||||
}
|
||||
return Optional.of(value.name());
|
||||
}
|
||||
|
||||
public String nameWithDefault() {
|
||||
return getWithDefault().name();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user