From a471bfec6aaa67ed617f730d73b235bf3371a7eb Mon Sep 17 00:00:00 2001 From: CocoTheOwner Date: Sat, 14 Aug 2021 12:14:36 +0200 Subject: [PATCH] BooleanHandler --- .../util/decree/handlers/BooleanHandler.java | 72 +++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 src/main/java/com/volmit/iris/util/decree/handlers/BooleanHandler.java diff --git a/src/main/java/com/volmit/iris/util/decree/handlers/BooleanHandler.java b/src/main/java/com/volmit/iris/util/decree/handlers/BooleanHandler.java new file mode 100644 index 000000000..184d1d1f3 --- /dev/null +++ b/src/main/java/com/volmit/iris/util/decree/handlers/BooleanHandler.java @@ -0,0 +1,72 @@ +package com.volmit.iris.util.decree.handlers; + +import com.volmit.iris.util.collection.KList; +import com.volmit.iris.util.decree.DecreeParameterHandler; +import com.volmit.iris.util.decree.exceptions.DecreeParsingException; +import com.volmit.iris.util.decree.exceptions.DecreeWhichException; + +public class BooleanHandler implements DecreeParameterHandler { + private static final KList trues = new KList<>( + "true", + "yes", + "t", + "1" + ); + private static final KList falses = new KList<>( + "false", + "no", + "f", + "0" + ); + + /** + * Should return the possible values for this type + * + * @return Possibilities for this type. + */ + @Override + public KList getPossibilities() { + return null; + } + + /** + * Converting the type back to a string (inverse of the {@link #parse(String) parse} method) + * + * @param aBoolean The input of the designated type to convert to a String + * @return The resulting string + */ + @Override + public String toString(Boolean aBoolean) { + return aBoolean.toString(); + } + + /** + * Should parse a String into the designated type + * + * @param in The string to parse + * @return The value extracted from the string, of the designated type + * @throws DecreeParsingException Thrown when the parsing fails (ex: "oop" translated to an integer throws this) + * @throws DecreeWhichException Thrown when multiple results are possible + */ + @Override + public Boolean parse(String in) throws DecreeParsingException, DecreeWhichException { + if (trues.contains(in)){ + return true; + } + if (falses.contains(in)){ + return false; + } + throw new DecreeParsingException("Cannot convert \"" + in + "\" to a boolean (" + trues.toString(", ") + " / " + falses.toString(", ") + ")"); + } + + /** + * Returns whether a certain type is supported by this handler
+ * + * @param type The type to check + * @return True if supported, false if not + */ + @Override + public boolean supports(Class type) { + return type.equals(boolean.class) || type.equals(Boolean.class); + } +}