| 24 | import java.util.regex.Pattern; |
| 25 | |
| 26 | public final class ToolType |
| 27 | { |
| 28 | private static final Pattern VALID_NAME = Pattern.compile("[^a-z_]"); //Only a-z and _ are allowed, meaning names must be lower case. And use _ to separate words. |
| 29 | private static final Map<String, ToolType> VALUES = new ConcurrentHashMap<>(); |
| 30 | |
| 31 | public static final ToolType AXE = get("axe"); |
| 32 | public static final ToolType HOE = get("hoe"); |
| 33 | public static final ToolType PICKAXE = get("pickaxe"); |
| 34 | public static final ToolType SHOVEL = get("shovel"); |
| 35 | |
| 36 | /** |
| 37 | * Gets the ToolType for the specified name, or creates a new one if none for that name does yet exist. |
| 38 | * This method can be called during parallel loading |
| 39 | */ |
| 40 | public static ToolType get(String name) |
| 41 | { |
| 42 | return VALUES.computeIfAbsent(name, k -> |
| 43 | { |
| 44 | if (VALID_NAME.matcher(name).find()) |
| 45 | throw new IllegalArgumentException("ToolType.get() called with invalid name: " + name); |
| 46 | return new ToolType(name); |
| 47 | }); |
| 48 | } |
| 49 | |
| 50 | private final String name; |
| 51 | |
| 52 | private ToolType(String name) |
| 53 | { |
| 54 | this.name = name; |
| 55 | } |
| 56 | |
| 57 | public String getName() |
| 58 | { |
| 59 | return name; |
| 60 | } |
| 61 | } |