From 5cf7bcf156970fe72f79e40b8a6e320ea160ac83 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Fri, 21 Oct 2016 14:14:48 -0400 Subject: Documentation --- .../main/java/bjc/utils/cli/GenericCommand.java | 17 +++++-- .../java/bjc/utils/cli/GenericCommandMode.java | 16 +++++-- .../src/main/java/bjc/utils/cli/GenericHelp.java | 4 +- .../main/java/bjc/utils/cli/ICommandHandler.java | 1 + .../src/main/java/bjc/utils/cli/NullHelp.java | 20 ++++++++ .../src/main/java/bjc/utils/cli/package-info.java | 3 +- .../bjc/utils/components/ComponentDescription.java | 14 +++--- .../components/ComponentDescriptionFileParser.java | 4 ++ .../utils/components/FileComponentRepository.java | 43 +++++++++-------- .../java/bjc/utils/components/package-info.java | 1 + .../src/main/java/bjc/utils/data/BoundLazy.java | 32 ++++++++++--- .../main/java/bjc/utils/data/BoundLazyPair.java | 55 ++++++++++++++-------- .../src/main/java/bjc/utils/data/package-info.java | 1 + .../src/main/java/bjc/utils/funcdata/IList.java | 15 +++--- 14 files changed, 155 insertions(+), 71 deletions(-) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java (limited to 'BJC-Utils2/src/main/java/bjc/utils') diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java index e73d936..54d2fa0 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommand.java @@ -1,5 +1,7 @@ package bjc.utils.cli; +import org.eclipse.jdt.annotation.Nullable; + /** * Generic command implementation * @@ -18,19 +20,24 @@ public class GenericCommand implements ICommand { * @param handler * The handler to use for the command * @param description - * The description of the command + * The description of the command. May be null * @param help - * The detailed help message for the command + * The detailed help message for the command. May be null */ - public GenericCommand(ICommandHandler handler, String description, - String help) { + public GenericCommand(ICommandHandler handler, @Nullable String description, + @Nullable String help) { if (handler == null) { throw new NullPointerException( "Command handler must not be null"); } this.handler = handler; - this.help = new GenericHelp(description, help); + + if (description == null) { + this.help = new NullHelp(); + } else { + this.help = new GenericHelp(description, help); + } } @Override diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java index 4b3b4fd..4aab8a4 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericCommandMode.java @@ -7,6 +7,8 @@ import java.util.function.Consumer; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IMap; +import org.eclipse.jdt.annotation.Nullable; + /** * A general command mode, with a customizable set of commands * @@ -50,6 +52,14 @@ public class GenericCommandMode implements ICommandMode { */ public GenericCommandMode(Consumer normalOutput, Consumer errorOutput) { + if (normalOutput == null) { + throw new NullPointerException( + "Normal output source must be non-null"); + } else if (errorOutput == null) { + throw new NullPointerException( + "Error output source must be non-null"); + } + this.normalOutput = normalOutput; this.errorOutput = errorOutput; @@ -371,7 +381,7 @@ public class GenericCommandMode implements ICommandMode { * The custom prompt for this mode, or null to disable the * custom prompt */ - public void setCustomPrompt(String prompt) { + public void setCustomPrompt(@Nullable String prompt) { customPrompt = prompt; } @@ -382,7 +392,7 @@ public class GenericCommandMode implements ICommandMode { * The desired name of this mode, or null to use the default * name */ - public void setModeName(String name) { + public void setModeName(@Nullable String name) { modeName = name; } @@ -394,7 +404,7 @@ public class GenericCommandMode implements ICommandMode { * on unknown commands */ public void setUnknownCommandHandler( - BiConsumer handler) { + @Nullable BiConsumer handler) { if (handler == null) { throw new NullPointerException("Handler must not be null"); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java index 8742b5d..2e46202 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/GenericHelp.java @@ -1,5 +1,7 @@ package bjc.utils.cli; +import org.eclipse.jdt.annotation.Nullable; + /** * Generic implementation of a help topic * @@ -20,7 +22,7 @@ public class GenericHelp implements ICommandHelp { * The description of this help topic, or null if this help * topic doesn't have a more detailed description */ - public GenericHelp(String summary, String description) { + public GenericHelp(String summary, @Nullable String description) { if (summary == null) { throw new NullPointerException( "Help summary must be non-null"); diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java index 3d451a7..33182b3 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/ICommandHandler.java @@ -8,6 +8,7 @@ import java.util.function.Function; * @author ben * */ +@FunctionalInterface public interface ICommandHandler extends Function { /** * Execute this command diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java new file mode 100644 index 0000000..e150ba6 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/NullHelp.java @@ -0,0 +1,20 @@ +package bjc.utils.cli; + +/** + * Implementation of a help topic that doesn't exist + * + * @author ben + * + */ +public class NullHelp implements ICommandHelp { + + @Override + public String getDescription() { + return "No description provided"; + } + + @Override + public String getSummary() { + return "No summary provided"; + } +} \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java index a87aa24..1bdfd14 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java +++ b/BJC-Utils2/src/main/java/bjc/utils/cli/package-info.java @@ -4,4 +4,5 @@ * @author ben * */ -package bjc.utils.cli; \ No newline at end of file +@org.eclipse.jdt.annotation.NonNullByDefault +package bjc.utils.cli; diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java index ab48c9b..1ec5b80 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java @@ -11,12 +11,6 @@ public class ComponentDescription implements IDescribedComponent { String description, int version) { if (name == null) { throw new NullPointerException("Component name can't be null"); - } else if (author == null) { - throw new NullPointerException( - "Component author can't be null"); - } else if (description == null) { - throw new NullPointerException( - "Component description can't be null"); } else if (version <= 0) { throw new IllegalArgumentException( "Component version must be greater than 0"); @@ -67,11 +61,19 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getAuthor() { + if(author == null) { + return IDescribedComponent.super.getAuthor(); + } + return author; } @Override public String getDescription() { + if(description == null) { + return IDescribedComponent.super.getDescription(); + } + return description; } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index ef8f8b6..ad5e1a2 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -41,6 +41,10 @@ public class ComponentDescriptionFileParser { */ public static ComponentDescription fromStream( InputStream inputSource) { + if(inputSource == null) { + throw new NullPointerException("Input source must not be null"); + } + ComponentDescriptionState readState = reader .fromStream(inputSource, new ComponentDescriptionState()); diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java index 6c1bb71..e98c06b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -25,8 +25,7 @@ import bjc.utils.funcutils.FileUtils; * @param * The type of component being read in */ -public class FileComponentRepository< - ComponentType extends IDescribedComponent> +public class FileComponentRepository implements IComponentRepository { // The logger to use for storing data about this class private static final Logger CLASS_LOGGER = Logger @@ -54,7 +53,9 @@ public class FileComponentRepository< public FileComponentRepository(File directory, Function componentReader) { // Make sure we have valid arguments - if (!directory.isDirectory()) { + if (directory == null) { + throw new NullPointerException("Directory must not be null"); + } else if (!directory.isDirectory()) { throw new IllegalArgumentException("File " + directory + " is not a directory.\n" + "Components can only be read from a directory"); @@ -72,25 +73,23 @@ public class FileComponentRepository< // Predicate to use to traverse all the files in a directory, but // not recurse into sub-directories - BiPredicate firstLevelTraverser = (pth, attr) -> { - if (attr.isDirectory() && !isFirstDir.getValue()) { - - /* - * Skip directories, they probably have component - * support files. - */ - return false; - } - - /* - * Don't skip the first directory, that's the parent - * directory - */ - isFirstDir.replace(false); - - return true; - }; + BiPredicate firstLevelTraverser = (pth, + attr) -> { + if (attr.isDirectory() && !isFirstDir.getValue()) { + /* + * Skip directories, they probably have component support + * files. + */ + return false; + } + + /* + * Don't skip the first directory, that's the parent directory + */ + isFirstDir.replace(false); + + return true; + }; // Try reading components try { diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java index 5ed7777..14a852a 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/package-info.java @@ -4,4 +4,5 @@ * @author ben * */ +@org.eclipse.jdt.annotation.NonNullByDefault package bjc.utils.components; \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java index d6f3b1d..b2ae7ce 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazy.java @@ -7,7 +7,7 @@ import java.util.function.UnaryOperator; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.IList; -/* +/** * Implements a lazy holder that has been bound */ class BoundLazy @@ -35,8 +35,7 @@ class BoundLazy /* * Transformations currently pending on the bound value */ - private IList> actions = new FunctionalList<>(); + private IList> actions = new FunctionalList<>(); /* * Create a new bound lazy value @@ -50,11 +49,14 @@ class BoundLazy @Override public IHolder bind( Function> bindr) { + if (bindr == null) { + throw new NullPointerException("Binder must not be null"); + } + /* * Prepare a list of pending actions */ - IList> pendingActions = new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); /* @@ -84,6 +86,11 @@ class BoundLazy @Override public Function> lift( Function func) { + if (func == null) { + throw new NullPointerException( + "Function to lift must not be null"); + } + return (val) -> { return new Lazy<>(func.apply(val)); }; @@ -92,9 +99,12 @@ class BoundLazy @Override public IHolder map( Function mapper) { + if (mapper == null) { + throw new NullPointerException("Mapper must not be null"); + } + // Prepare a list of pending actions - IList> pendingActions = new FunctionalList<>(); + IList> pendingActions = new FunctionalList<>(); actions.forEach(pendingActions::add); // Prepare the new supplier @@ -127,6 +137,10 @@ class BoundLazy @Override public IHolder transform( UnaryOperator transformer) { + if (transformer == null) { + throw new NullPointerException("Transformer must not be null"); + } + actions.add(transformer); return this; @@ -135,6 +149,10 @@ class BoundLazy @Override public UnwrappedType unwrap( Function unwrapper) { + if (unwrapper == null) { + throw new NullPointerException("Unwrapper must not be null"); + } + if (!holderBound) { boundHolder = oldSupplier.get().unwrap(binder::apply); } diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java index 622bd2e..2b3fb29 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/BoundLazyPair.java @@ -4,23 +4,38 @@ import java.util.function.BiFunction; import java.util.function.Function; import java.util.function.Supplier; +/** + * Implements a lazy pair that has been bound + */ class BoundLazyPair implements IPair { - private Supplier< - OldLeft> leftSupplier; - private Supplier< - OldRight> rightSupplier; - + /* + * The supplier of the left value + */ + private Supplier leftSupplier; + /* + * The supplier of the right value + */ + private Supplier rightSupplier; + + /* + * The binder to transform values + */ private BiFunction> binder; - private IPair boundPair; + /* + * The bound pair + */ + private IPair boundPair; + /* + * Whether the pair has been bound yet + */ private boolean pairBound; public BoundLazyPair(Supplier leftSupp, - Supplier rightSupp, BiFunction> bindr) { + Supplier rightSupp, + BiFunction> bindr) { leftSupplier = leftSupp; rightSupplier = rightSupp; binder = bindr; @@ -28,10 +43,13 @@ class BoundLazyPair @Override public IPair bind( - BiFunction> bindr) { - IHolder> newPair = new Identity<>(boundPair); + BiFunction> bindr) { + if (bindr == null) { + throw new NullPointerException("Binder must not be null"); + } + + IHolder> newPair = new Identity<>( + boundPair); IHolder newPairMade = new Identity<>(pairBound); Supplier leftSupp = () -> { @@ -94,13 +112,10 @@ class BoundLazyPair } @Override - public IPair combine( - IPair otherPair, - BiFunction leftCombiner, - BiFunction rightCombiner) { + public IPair combine( + IPair otherPair, + BiFunction leftCombiner, + BiFunction rightCombiner) { return otherPair.bind((otherLeft, otherRight) -> { return bind((leftVal, rightVal) -> { return new LazyPair<>( diff --git a/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java b/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java index a6f05dd..f5039bc 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java +++ b/BJC-Utils2/src/main/java/bjc/utils/data/package-info.java @@ -4,4 +4,5 @@ * @author ben * */ +@org.eclipse.jdt.annotation.NonNullByDefault package bjc.utils.data; \ No newline at end of file diff --git a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java index 47acf1a..014c298 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java +++ b/BJC-Utils2/src/main/java/bjc/utils/funcdata/IList.java @@ -28,6 +28,10 @@ public interface IList { */ boolean add(ContainedType item); + default boolean addAll(IList items) { + return items.map(this::add).anyMatch((bl) -> bl == false); + } + /** * Check if all of the elements of this list match the specified * predicate. @@ -62,8 +66,8 @@ public interface IList { */ public default ReducedType collect( Collector collector) { - BiConsumer accumulator = collector.accumulator(); + BiConsumer accumulator = collector + .accumulator(); return reduceAux(collector.supplier().get(), (value, state) -> { accumulator.accept(state, value); @@ -92,8 +96,8 @@ public interface IList { * @return A new list containing the merged pairs of lists. */ IList combineWith( - IList rightList, BiFunction itemCombiner); + IList rightList, + BiFunction itemCombiner); /** * Check if the list contains the specified item @@ -261,8 +265,7 @@ public interface IList { * its final state. */ ReducedType reduceAux(StateType initialValue, - BiFunction stateAccumulator, + BiFunction stateAccumulator, Function resultTransformer); /** -- cgit v1.2.3