001/* 002 * Copyright 2021 Siroshun09 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package com.github.siroshun09.mccommand.common.argument; 018 019import com.github.siroshun09.mccommand.common.argument.parser.ArgumentParser; 020import com.github.siroshun09.mccommand.common.argument.parser.BasicParser; 021import org.jetbrains.annotations.NotNull; 022import org.jetbrains.annotations.Nullable; 023 024import java.util.Objects; 025 026/** 027 * Represents a command argument. 028 */ 029public interface Argument { 030 031 /** 032 * Creates an {@link Argument} from an index and a string. 033 * <p> 034 * The {@link Argument} returned by this method implements {@link Object#hashCode()}, {@link Object#equals(Object)}, and {@link Object#toString()}. 035 * 036 * @param index the position of this argument 037 * @param argument the argument 038 * @return {@link Argument} 039 */ 040 static Argument of(int index, @NotNull String argument) { 041 return new Argument() { 042 @Override 043 public int getIndex() { 044 return index; 045 } 046 047 @Override 048 public @NotNull String get() { 049 return argument; 050 } 051 052 @Override 053 public int hashCode() { 054 return Objects.hash(index, argument); 055 } 056 057 @Override 058 public boolean equals(Object o) { 059 if (this == o) { 060 return true; 061 } 062 063 if (!(o instanceof Argument)) { 064 return false; 065 } 066 067 Argument that = (Argument) o; 068 069 return index == that.getIndex() && argument.equals(that.get()); 070 } 071 072 @Override 073 public String toString() { 074 return "Argument{" + 075 "index=" + index + 076 ", argument=" + argument + 077 '}'; 078 } 079 }; 080 } 081 082 /** 083 * Gets the position of this argument. 084 * 085 * @return the position of this argument 086 */ 087 int getIndex(); 088 089 /** 090 * Gets this argument as a string. 091 * 092 * @return this argument as a string 093 */ 094 @NotNull 095 String get(); 096 097 /** 098 * Parses the argument to {@link Boolean}. 099 * 100 * @return the result of parsing the arguments, or {@code null} if it fails. 101 * @see BasicParser#BOOLEAN} 102 */ 103 @Nullable 104 default Boolean parseBoolean() { 105 return BasicParser.BOOLEAN.parse(this); 106 } 107 108 /** 109 * Parses the argument to {@link Short}. 110 * 111 * @return the result of parsing the arguments, or {@code null} if it fails. 112 * @see BasicParser#SHORT} 113 */ 114 @Nullable 115 default Short parseShort() { 116 return BasicParser.SHORT.parse(this); 117 } 118 119 /** 120 * Parses the argument to {@link Integer}. 121 * 122 * @return the result of parsing the arguments, or {@code null} if it fails. 123 * @see BasicParser#INTEGER} 124 */ 125 @Nullable 126 default Integer parseInteger() { 127 return BasicParser.INTEGER.parse(this); 128 } 129 130 /** 131 * Parses the argument to {@link Long}. 132 * 133 * @return the result of parsing the arguments, or {@code null} if it fails. 134 * @see BasicParser#LONG} 135 */ 136 @Nullable 137 default Long parseLong() { 138 return BasicParser.LONG.parse(this); 139 } 140 141 /** 142 * Parses the argument to {@link Float}. 143 * 144 * @return the result of parsing the arguments, or {@code null} if it fails. 145 * @see BasicParser#FLOAT} 146 */ 147 @Nullable 148 default Float parseFloat() { 149 return BasicParser.FLOAT.parse(this); 150 } 151 152 /** 153 * Parses the argument to {@link Double}. 154 * 155 * @return the result of parsing the arguments, or {@code null} if it fails. 156 * @see BasicParser#DOUBLE} 157 */ 158 @Nullable 159 default Double parseDouble() { 160 return BasicParser.DOUBLE.parse(this); 161 } 162 163 /** 164 * Parses the argument using the specified parser. 165 * 166 * @param parser the parser 167 * @param <T> the value type 168 * @return the result of parsing the arguments, or {@code null} if it fails. 169 * @see ArgumentParser#parse(Argument) 170 */ 171 default <T> T parse(ArgumentParser<T> parser) { 172 return parser.parse(this); 173 } 174}