/** * Gets an Optional with or without a value of CVar argument at position * * @param cv * @return Optional */ public T get <T>(CommandVariable cv, Class <T> itemType, int position) { if (cv.arguments[position] != itemType) { throw new ArgumentException(String.Format("CVar argument at position %d is not of class %s", position, itemType.getName())); } if (!cVarMap.ContainsKey(cv)) { return(Optional.empty()); } var ret = (T)cVarMap.Get(cv)[position]; return(Optional.ofNullable(ret)); }
/** * Tries to apply a CVar argument at position to the consuming function * The magic is that you declare a lambda function or reference some method * and the type of object will be automatically picked from what you hinted * <p> * i.e. (String s) -> System.out.println(s) will try to get string, * (Object o) -> map.put(key, o) or o -> list.add(o.hashCode()) will try to get objects * and you dont have to specify class * <p> * The drawback is the ClassCastException will be thrown if the value is neither * what you expected, nor a subclass of it * * @param cv * @param position * @param action * @return false if CVar is not passed as Command Line Argument or the consuming action is incompatible */ public bool with <T>(CommandVariable cv, int position, Consumer <T> action) { try { var mapped = cVarMap.Get(cv); if (mapped == null) { return(false); } var item = (T)mapped[position]; action.accept(item); return(true); } catch (Exception ex) { return(false); } }
/** * Tries to replace the CVar argument if already present or add it along with CVar * * @param cv * @param value * @param position * @return false if invalid position or value class */ public bool unknown <T>(CommandVariable cv, T value, int position) { if (position < 0 || position >= cv.arguments.Count) { return(false); } if (!cv.arguments[position].isInstance(value)) { return(false); } cVarMap.compute(cv, (key, array)->{ if (array == null) { array = new Object[cv.arguments.Count]; } array[position] = value; return(array); });
/** * Checks that CVar of any type is passed as Command Line Argument * * @param cv * @return boolean */ public bool specified(CommandVariable cv) { return(cVarMap.ContainsKey(cv)); }
/** * Checks that CVar of any type is passed as Command Line Argument with proper value(s) * * @param cv * @return boolean */ public bool present(CommandVariable cv) { return(cVarMap[cv] != null); }
/** * Checks that CVar of switch-type is passed as Command Line Argument * * @param cv * @return boolean */ public bool Bool(CommandVariable cv) { return(cv.getType() == CommandVariable.Type.SWITCH && cVarMap.ContainsKey(cv)); }