diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/components')
6 files changed, 169 insertions, 146 deletions
diff --git a/base/src/main/java/bjc/utils/components/ComponentDescription.java b/base/src/main/java/bjc/utils/components/ComponentDescription.java index 28f81d1..8d44855 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescription.java @@ -1,50 +1,47 @@ package bjc.utils.components; /** - * Generic implementation of a description for a component + * Generic implementation of a description for a component. * * @author ben - * */ public class ComponentDescription implements IDescribedComponent { + /* Check arguments are good. */ private static void sanityCheckArgs(final String name, final String author, final String description, final int version) { - if (name == null) + if (name == null) { throw new NullPointerException("Component name can't be null"); - else if (version <= 0) throw new IllegalArgumentException("Component version must be greater than 0"); + } else if (version <= 0) { + throw new IllegalArgumentException("Component version must be greater than 0"); + } } - /** - * The author of the component - */ - private final String author; - /** - * The description of the component - */ - private final String description; - /** - * The name of the component - */ - private final String name; - - /** - * The version of the component - */ + /** The author of the component */ + private final String author; + /** The description of the component */ + private final String description; + /** The name of the component */ + private final String name; + /** The version of the component */ private final int version; /** - * Create a new component description + * Create a new component description. * * @param name - * The name of the component + * The name of the component. + * * @param author - * The author of the component + * The author of the component. + * * @param description - * The description of the component + * The description of the component. + * * @param version - * The version of the component + * The version of the component. + * * @throws IllegalArgumentException - * thrown if version is less than 1 + * Thrown if version is less than 1. */ public ComponentDescription(final String name, final String author, final String description, final int version) { @@ -58,14 +55,18 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getAuthor() { - if (author == null) return IDescribedComponent.super.getAuthor(); + if (author == null) { + return IDescribedComponent.super.getAuthor(); + } return author; } @Override public String getDescription() { - if (description == null) return IDescribedComponent.super.getDescription(); + if (description == null) { + return IDescribedComponent.super.getDescription(); + } return description; } @@ -82,7 +83,7 @@ public class ComponentDescription implements IDescribedComponent { @Override public String toString() { - return name + " component v" + version + ", written by " + author; + return String.format("%s component v%d, written by %s", name, version, author); } /* diff --git a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index f7ddaff..3855f8f 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -1,57 +1,63 @@ package bjc.utils.components; -import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildInteger; -import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildStringCollapser; - import java.io.InputStream; import bjc.utils.ioutils.RuleBasedConfigReader; +import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildInteger; +import static bjc.utils.ioutils.RuleBasedReaderPragmas.buildStringCollapser; + /** - * Read a component description from a file + * Read a component description from a file. * * @author ben - * */ public class ComponentDescriptionFileParser { - // The reader used to read in component descriptions + /* The reader used to read in component descriptions */ private static RuleBasedConfigReader<ComponentDescriptionState> reader; - // Initialize the reader and its pragmas + /* Initialize the reader and its pragmas. */ static { - // This reader works entirely off of pragmas, so no need to - // handle - // rules + /* + * This reader works entirely off of pragmas, so no need to + * handle rules. + */ reader = new RuleBasedConfigReader<>((tokenizer, statePair) -> { - // Don't need to do anything on rule start + /* Don't need to do anything on rule start. */ }, (tokenizer, state) -> { - // Don't need to do anything on rule continuation + /* Don't need to do anything on rule continuation. */ }, (state) -> { - // Don't need to do anything on rule end + /* Don't need to do anything on rule end. */ }); + /* Setup reader pragmas. */ setupReaderPragmas(); } /** - * Parse a component description from a stream + * Parse a component description from a stream. * * @param inputSource - * The stream to parse from - * @return The description parsed from the stream + * The stream to parse from. + * + * @return The description parsed from the stream. */ public static ComponentDescription fromStream(final InputStream inputSource) { - if (inputSource == null) throw new NullPointerException("Input source must not be null"); + if (inputSource == null) { + throw new NullPointerException("Input source must not be null"); + } - final ComponentDescriptionState readState = reader.fromStream(inputSource, - new ComponentDescriptionState()); + ComponentDescriptionState state = new ComponentDescriptionState(); + /* + * This is valid, because the thing that is returned is the same + * reference we passed in. + */ + reader.fromStream(inputSource, state); - return readState.toDescription(); + return state.toDescription(); } - /* - * Create all the pragmas the reader needs to function - */ + /* Create all the pragmas the reader needs to function. */ private static void setupReaderPragmas() { reader.addPragma("name", buildStringCollapser("name", (name, state) -> state.setName(name))); diff --git a/base/src/main/java/bjc/utils/components/ComponentDescriptionState.java b/base/src/main/java/bjc/utils/components/ComponentDescriptionState.java index 8d66f85..f944a9e 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescriptionState.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescriptionState.java @@ -1,68 +1,68 @@ package bjc.utils.components; /** - * Internal state of component description parser + * Internal state of component description parser. * * @author ben - * */ public class ComponentDescriptionState { - // Tentative name of this component + /* Tentative name of this component. */ private String name; - // Tentative description of this componet + /* Tentative description of this component. */ private String description; - // Tentative author of this component + /* Tentative author of this component. */ private String author; - // Tentative version of this component + /* Tentative version of this component. */ private int version; /** - * Set the author of this component + * Set the author of this component. * * @param author - * The author of this component + * The author of this component. */ public void setAuthor(final String author) { this.author = author; } /** - * Set the description of this component + * Set the description of this component. * * @param description - * The description of this component + * The description of this component. */ public void setDescription(final String description) { this.description = description; } /** - * Set the name of this component + * Set the name of this component. * * @param name - * The name of this component + * The name of this component. */ public void setName(final String name) { this.name = name; } /** - * Set the version of this component + * Set the version of this component. * * @param version - * The version of this component + * The version of this component. */ public void setVersion(final int version) { this.version = version; } /** - * Convert this state into the description it represents + * Convert this state into the description it represents. * - * @return The description represented by this state + * @return + * The description represented by this state. */ public ComponentDescription toDescription() { return new ComponentDescription(name, author, description, version); @@ -71,7 +71,7 @@ public class ComponentDescriptionState { @Override public int hashCode() { final int prime = 31; - int result = 1; + int result = 1; result = prime * result + (author == null ? 0 : author.hashCode()); result = prime * result + (description == null ? 0 : description.hashCode()); @@ -140,5 +140,4 @@ public class ComponentDescriptionState { return builder.toString(); } - } diff --git a/base/src/main/java/bjc/utils/components/FileComponentRepository.java b/base/src/main/java/bjc/utils/components/FileComponentRepository.java index efde5c7..284c10c 100644 --- a/base/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -17,85 +17,86 @@ import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.FileUtils; /** - * A component repository that loads its components from files in a directory + * A component repository that loads its components from files in a directory. * * @author ben * * @param <ComponentType> - * The type of component being read in + * The type of component being read in. */ public class FileComponentRepository<ComponentType extends IDescribedComponent> implements IComponentRepository<ComponentType> { - // The logger to use for storing data about this class + /* The logger to use for storing data about this class. */ private static final Logger CLASS_LOGGER = Logger.getLogger("FileComponentRepository"); - // The internal storage of components + /* The internal storage of components. */ private IMap<String, ComponentType> components; - // The path that all the components came from + /* The path that all the components came from. */ private Path sourceDirectory; /** * Create a new component repository sourcing components from files in a - * directory + * directory. * * An exception thrown during the loading of a component will only cause * the loading of that component to fail, but a warning will be logged. * * @param directory - * The directory to read component files from + * The directory to read component files from. + * * @param componentReader - * The function to use to convert files to components + * The function to use to convert files to components. */ public FileComponentRepository(final File directory, final Function<File, ? extends ComponentType> componentReader) { - // Make sure we have valid arguments - if (directory == null) + /* Make sure we have valid arguments. */ + 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"); - else if (componentReader == null) throw new NullPointerException("Component reader must not be null"); + } else if (!directory.isDirectory()) { + String msg = String.format("File %s is not a directory. Components can only be read from a directory.", + directory); + + throw new IllegalArgumentException(msg); + } else if (componentReader == null) { + throw new NullPointerException("Component reader must not be null"); + } - // Initialize our fields + /* Initialize our fields. */ components = new FunctionalMap<>(); sourceDirectory = directory.toPath().toAbsolutePath(); - // Marker for making sure we don't skip the parent + /* Marker for making sure we don't skip the parent. */ final IHolder<Boolean> isFirstDir = new Identity<>(true); - // Predicate to use to traverse all the files in a directory, - // but - // not recurse into sub-directories + /* + * Predicate to use to traverse all the files in a directory, + * but not recurse into sub-directories. + */ final BiPredicate<Path, BasicFileAttributes> firstLevelTraverser = (pth, attr) -> { - if (attr.isDirectory() && !isFirstDir.getValue()) /* - * Skip - * directories, - * they - * probably - * have - * component - * support - * files. - */ + 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 + * directory. */ isFirstDir.replace(false); return true; }; - // Try reading components + /* Try reading components. */ try { FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, (pth, attr) -> { loadComponent(componentReader, pth); - // Keep loading components, even if this one - // failed + /* Keep loading components, even if this one failed. */ return true; }); } catch (final IOException ioex) { @@ -120,40 +121,48 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> @Override public String getSource() { - return "Components read from directory " + sourceDirectory + "."; + return String.format("Components read from directory %s.", sourceDirectory); } - /* - * Load a component from a file - */ + /* Load a component from a file */ private void loadComponent(final Function<File, ? extends ComponentType> componentReader, final Path pth) { try { - // Try to load the component + /* Try to load the component. */ final ComponentType component = componentReader.apply(pth.toFile()); - if (component == null) + if (component == null) { throw new NullPointerException("Component reader read null component"); - else if (!components.containsKey(component.getName())) { - // We only care about the latest version of a - // component + } else if (!components.containsKey(component.getName())) { + /* + * We only care about the latest version of a + * component. + */ final ComponentType oldComponent = components.put(component.getName(), component); if (oldComponent.getVersion() > component.getVersion()) { components.put(oldComponent.getName(), oldComponent); } } else { - CLASS_LOGGER.warning("Found a duplicate component.\n" - + "Multiple versions of the same component are not currently supported.\n" - + "Only the latest version of the component" + component - + " will be registered ."); + StringBuilder sb = new StringBuilder(); + sb.append("Found a duplicate component.\n"); + sb.append("Multiple versions of the same component are not currently supported.\n"); + sb.append("Only the latest version of the component "); + sb.append(component); + sb.append(" will be registered."); + + CLASS_LOGGER.warning(sb.toString()); } } catch (final Exception ex) { - CLASS_LOGGER.log(Level.WARNING, ex, () -> "Error found reading component from file " - + pth.toString() + ". This component will not be loaded"); + String msg = String.format("Error found reading component from file %s. It will not be loaded.", pth.toString()); + + CLASS_LOGGER.log(Level.WARNING, ex, () -> msg); } } /* + * @NOTE + * Should this be changed to something more readable? + * * (non-Javadoc) * * @see java.lang.Object#toString() @@ -178,4 +187,4 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> return builder.toString(); } -}
\ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/components/IComponentRepository.java b/base/src/main/java/bjc/utils/components/IComponentRepository.java index 6ee51f3..099693f 100644 --- a/base/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/IComponentRepository.java @@ -5,45 +5,49 @@ import bjc.utils.funcdata.IMap; /** * A collection of implementations of a particular type of - * {@link IDescribedComponent} + * {@link IDescribedComponent}. * * @author ben * * @param <ComponentType> - * The type of components contained in this repository + * The type of components contained in this repository. */ public interface IComponentRepository<ComponentType extends IDescribedComponent> { /** - * Get all of the components this repository knows about + * Get all of the components this repository knows about. * - * @return A map from component name to component, containing all of the - * components in the repositories + * @return + * A map from component name to component, containing all of the + * components in the repositories. */ public IMap<String, ComponentType> getAll(); /** - * Get a component with a specific name + * Get a component with a specific name. * * @param name - * The name of the component to retrieve - * @return The named component, or null if no component with that name - * exists + * The name of the component to retrieve. + * + * @return + * The named component, or null if no component with that name exists. */ public ComponentType getByName(String name); /** - * Get a list of all the registered components + * Get a list of all the registered components. * - * @return A list of all the registered components + * @return + * A list of all the registered components. */ public default IList<ComponentType> getList() { return getAll().valueList(); } /** - * Get the source from which these components came + * Get the source from which these components came. * - * @return The source from which these components came + * @return + * The source from which these components came. */ public String getSource(); -}
\ No newline at end of file +} diff --git a/base/src/main/java/bjc/utils/components/IDescribedComponent.java b/base/src/main/java/bjc/utils/components/IDescribedComponent.java index 952b375..78af4bc 100644 --- a/base/src/main/java/bjc/utils/components/IDescribedComponent.java +++ b/base/src/main/java/bjc/utils/components/IDescribedComponent.java @@ -2,30 +2,32 @@ package bjc.utils.components; /** * Represents a optional component that has status information associated with - * it + * it. * * @author ben * */ public interface IDescribedComponent extends Comparable<IDescribedComponent> { /** - * Get the author of this component + * Get the author of this component. * - * Providing this is optional, with "Anonymous" as the default author + * Providing this is optional, with "Anonymous" as the default author. * - * @return The author of the component + * @return + * The author of the component. */ default String getAuthor() { return "Anonymous"; } /** - * Get the description of this component + * Get the description of this component. * * Providing this is optional, with the default being a note that no - * description was provided + * description was provided. * - * @return The description of the component + * @return + * The description of the component */ default String getDescription() { return "No description provided."; @@ -34,18 +36,20 @@ public interface IDescribedComponent extends Comparable<IDescribedComponent> { /** * Get the name of this component. * - * This is the only thing required of all components + * This is the only thing required of all components. * - * @return The name of the component + * @return + * The name of the component. */ String getName(); /** - * Get the version of this component + * Get the version of this component. * - * Providing this is optional, with "1" as the default version + * Providing this is optional, with "1" as the default version. * - * @return The version of this component + * @return + * The version of this component. */ default int getVersion() { return 1; @@ -61,4 +65,4 @@ public interface IDescribedComponent extends Comparable<IDescribedComponent> { return res; } -}
\ No newline at end of file +} |
