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.parser; 018 019import com.github.siroshun09.mccommand.common.argument.Argument; 020import org.jetbrains.annotations.NotNull; 021import org.jetbrains.annotations.Nullable; 022 023import java.util.UUID; 024 025/** 026 * A class that collect basic parsers. 027 */ 028public final class BasicParser { 029 030 private BasicParser() { 031 throw new UnsupportedOperationException(); 032 } 033 034 /** 035 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Boolean} 036 */ 037 public static final ArgumentParser<Boolean> BOOLEAN = argument -> { 038 if (argument.get().equalsIgnoreCase("true")) { 039 return true; 040 } 041 042 if (argument.get().equalsIgnoreCase("false")) { 043 return false; 044 } 045 046 return null; 047 }; 048 049 /** 050 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Short} 051 */ 052 public static final ArgumentParser<Short> SHORT = new ArgumentParser<>() { 053 @Override 054 public @Nullable Short parse(@NotNull Argument argument) { 055 try { 056 return Short.parseShort(argument.get()); 057 } catch (NumberFormatException e) { 058 return null; 059 } 060 } 061 062 @Override 063 public @NotNull Short parseOrThrow(@NotNull Argument argument) throws IllegalArgumentException { 064 try { 065 return Short.parseShort(argument.get()); 066 } catch (NumberFormatException e) { 067 throw generateException(argument, e); 068 } 069 } 070 }; 071 072 /** 073 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Integer} 074 */ 075 public static final ArgumentParser<Integer> INTEGER = new ArgumentParser<>() { 076 @Override 077 public @Nullable Integer parse(@NotNull Argument argument) { 078 try { 079 return Integer.parseInt(argument.get()); 080 } catch (NumberFormatException e) { 081 return null; 082 } 083 } 084 085 @Override 086 public @NotNull Integer parseOrThrow(@NotNull Argument argument) throws IllegalArgumentException { 087 try { 088 return Integer.parseInt(argument.get()); 089 } catch (NumberFormatException e) { 090 throw generateException(argument, e); 091 } 092 } 093 }; 094 095 /** 096 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Long} 097 */ 098 public static final ArgumentParser<Long> LONG = new ArgumentParser<>() { 099 @Override 100 public @Nullable Long parse(@NotNull Argument argument) { 101 try { 102 return Long.parseLong(argument.get()); 103 } catch (NumberFormatException e) { 104 return null; 105 } 106 } 107 108 @Override 109 public @NotNull Long parseOrThrow(@NotNull Argument argument) throws IllegalArgumentException { 110 try { 111 return Long.parseLong(argument.get()); 112 } catch (NumberFormatException e) { 113 throw generateException(argument, e); 114 } 115 } 116 }; 117 118 /** 119 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Float} 120 */ 121 public static final ArgumentParser<Float> FLOAT = new ArgumentParser<>() { 122 @Override 123 public @Nullable Float parse(@NotNull Argument argument) { 124 try { 125 return Float.parseFloat(argument.get()); 126 } catch (NumberFormatException e) { 127 return null; 128 } 129 } 130 131 @Override 132 public @NotNull Float parseOrThrow(@NotNull Argument argument) throws IllegalArgumentException { 133 try { 134 return Float.parseFloat(argument.get()); 135 } catch (NumberFormatException e) { 136 throw generateException(argument, e); 137 } 138 } 139 }; 140 141 /** 142 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link Double} 143 */ 144 public static final ArgumentParser<Double> DOUBLE = new ArgumentParser<>() { 145 @Override 146 public @Nullable Double parse(@NotNull Argument argument) { 147 try { 148 return Double.parseDouble(argument.get()); 149 } catch (NumberFormatException e) { 150 return null; 151 } 152 } 153 154 @Override 155 @NotNull 156 public Double parseOrThrow(@NotNull Argument argument) throws IllegalArgumentException { 157 try { 158 return Double.parseDouble(argument.get()); 159 } catch (NumberFormatException e) { 160 throw generateException(argument, e); 161 } 162 } 163 }; 164 165 /** 166 * An instance of {@link ArgumentParser} that parses an {@link Argument} to {@link UUID} 167 */ 168 public static final ArgumentParser<UUID> UUID = new UUIDParser(); 169}