Java tutorial
/* * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ package works.chatterbox.hooks.ircchannels.channels.modes; import com.google.common.base.Preconditions; import org.jetbrains.annotations.NotNull; import works.chatterbox.hooks.ircchannels.api.IRCChannelsAPI; import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; public interface Mode<T> { static Mode fromString(@NotNull final String string) { Preconditions.checkNotNull(string); Preconditions.checkArgument(string.length() > 0, "Zero-length (or smaller) string given"); final char modeChar = string.charAt(0); final Mode mode = IRCChannelsAPI.getInstance().getModesAPI().getModes().getMode(modeChar); Preconditions.checkState(mode != null, "No mode for " + modeChar); if (mode.getArgumentLength() > 0) { Preconditions.checkState(string.length() > 2, "No arguments for mode requiring arguments"); final String[] args = string.substring(2).split(" "); Preconditions.checkState(args.length == mode.getArgumentLength(), "Invalid number of arguments. Expected " + mode.getArgumentLength() + ", received " + args.length); final List<String> arguments = Arrays.stream(args).collect(Collectors.toList()); //noinspection unchecked mode.setArguments(arguments); } return mode; } int getArgumentLength(); List<T> getArguments(); void setArguments(@NotNull final List<T> arguments); char[] getModes(); }