diff options
| author | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
|---|---|---|
| committer | Ben Culkin <scorpress@gmail.com> | 2020-04-13 18:40:41 -0400 |
| commit | d4ca769e542b2489b1e23cfcbdc3a0b7275b87cd (patch) | |
| tree | 1653a7399f97fb0c63ce62e3f60fd830d5c37f70 /base/src/main/java/bjc/utils/components | |
| parent | 2ac2e31a56ae59ee582e43a90c3495f86dd9ee7a (diff) | |
Cleanup pass
Cleanup pass to uniformize things
Diffstat (limited to 'base/src/main/java/bjc/utils/components')
6 files changed, 160 insertions, 129 deletions
diff --git a/base/src/main/java/bjc/utils/components/ComponentDescription.java b/base/src/main/java/bjc/utils/components/ComponentDescription.java index 57005e2..189ef90 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescription.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescription.java @@ -8,12 +8,13 @@ package bjc.utils.components; public class ComponentDescription implements IDescribedComponent { /* Check arguments are good. */ @SuppressWarnings("unused") - private static void sanityCheckArgs(final String name, final String author, final String description, - final int version) { - if(name == null) { + private static void sanityCheckArgs(final String name, final String author, + final String description, final int version) { + 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"); } } @@ -30,22 +31,22 @@ public class ComponentDescription implements IDescribedComponent { * 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) { + public ComponentDescription(final String name, final String author, + final String description, final int version) { sanityCheckArgs(name, author, description, version); this.name = name; @@ -56,7 +57,7 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getAuthor() { - if(author == null) { + if (author == null) { return IDescribedComponent.super.getAuthor(); } @@ -65,7 +66,7 @@ public class ComponentDescription implements IDescribedComponent { @Override public String getDescription() { - if(description == null) { + if (description == null) { return IDescribedComponent.super.getDescription(); } @@ -112,25 +113,35 @@ public class ComponentDescription implements IDescribedComponent { */ @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; final ComponentDescription other = (ComponentDescription) obj; - if(author == null) { - if(other.author != null) return false; - } else if(!author.equals(other.author)) return false; - - if(description == null) { - if(other.description != null) return false; - } else if(!description.equals(other.description)) return false; - - if(name == null) { - if(other.name != null) return false; - } else if(!name.equals(other.name)) return false; - - if(version != other.version) return false; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; + + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; + + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; + + if (version != other.version) + return false; return true; } diff --git a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java index ee6e913..87a749e 100644 --- a/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/base/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -13,11 +13,12 @@ import bjc.utils.ioutils.RuleBasedConfigReader; * The file format is based entirely off of pragma statements, and should have * at least one of each of the following statements * <ul> - * <li>pragma name <component-name&rt; </li> - * <li>pragma author <component-version&rt; </li> - * <li>pragma description <component-description&rt;</li> - * <li>pragma version <component-version&rt; </li> + * <li>pragma name <component-name&rt;</li> + * <li>pragma author <component-version&rt;</li> + * <li>pragma description <component-description&rt;</li> + * <li>pragma version <component-version&rt;</li> * </ul> + * * @author ben */ public class ComponentDescriptionFileParser { @@ -27,14 +28,13 @@ public class ComponentDescriptionFileParser { /* 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. */ }, (tokenizer, state) -> { /* Don't need to do anything on rule continuation. */ - }, (state) -> { + }, state -> { /* Don't need to do anything on rule end. */ }); @@ -46,19 +46,19 @@ public class ComponentDescriptionFileParser { * Parse a component description from a stream. * * @param inputSource - * The stream to parse from. + * The stream to parse from. * * @return The description parsed from the stream. */ public static ComponentDescription fromStream(final InputStream inputSource) { - if(inputSource == null) { + if (inputSource == null) { throw new NullPointerException("Input source must not be null"); } ComponentDescriptionState state = new ComponentDescriptionState(); /* - * This is valid, because the thing that is returned is the same - * reference we passed in. + * This is valid, because the thing that is returned is the same reference we + * passed in. */ reader.fromStream(inputSource, state); @@ -67,14 +67,17 @@ public class ComponentDescriptionFileParser { /* Create all the pragmas the reader needs to function. */ private static void setupReaderPragmas() { - reader.addPragma("name", buildStringCollapser("name", (name, state) -> state.setName(name))); + reader.addPragma("name", + buildStringCollapser("name", (name, state) -> state.setName(name))); - reader.addPragma("author", buildStringCollapser("author", (author, state) -> state.setAuthor(author))); + reader.addPragma("author", buildStringCollapser("author", + (author, state) -> state.setAuthor(author))); reader.addPragma("description", buildStringCollapser("description", (description, state) -> state.setDescription(description))); - reader.addPragma("version", buildInteger("version", (version, state) -> state.setVersion(version))); + reader.addPragma("version", + buildInteger("version", (version, state) -> state.setVersion(version))); } private static final class ComponentDescriptionState { @@ -94,7 +97,7 @@ public class ComponentDescriptionFileParser { * 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; @@ -104,7 +107,7 @@ public class ComponentDescriptionFileParser { * 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; @@ -114,7 +117,7 @@ public class ComponentDescriptionFileParser { * 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; @@ -124,7 +127,7 @@ public class ComponentDescriptionFileParser { * 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; @@ -154,25 +157,35 @@ public class ComponentDescriptionFileParser { @Override public boolean equals(final Object obj) { - if(this == obj) return true; - if(obj == null) return false; - if(getClass() != obj.getClass()) return false; + if (this == obj) + return true; + if (obj == null) + return false; + if (getClass() != obj.getClass()) + return false; final ComponentDescriptionState other = (ComponentDescriptionState) obj; - if(author == null) { - if(other.author != null) return false; - } else if(!author.equals(other.author)) return false; + if (author == null) { + if (other.author != null) + return false; + } else if (!author.equals(other.author)) + return false; - if(description == null) { - if(other.description != null) return false; - } else if(!description.equals(other.description)) return false; + if (description == null) { + if (other.description != null) + return false; + } else if (!description.equals(other.description)) + return false; - if(name == null) { - if(other.name != null) return false; - } else if(!name.equals(other.name)) return false; + if (name == null) { + if (other.name != null) + return false; + } else if (!name.equals(other.name)) + return false; - if(version != other.version) return false; + if (version != other.version) + return false; return true; } @@ -187,19 +200,19 @@ public class ComponentDescriptionFileParser { final StringBuilder builder = new StringBuilder(); builder.append("ComponentDescriptionState ["); - if(name != null) { + if (name != null) { builder.append("name="); builder.append(name); builder.append(", "); } - if(description != null) { + if (description != null) { builder.append("description="); builder.append(description); builder.append(", "); } - if(author != null) { + if (author != null) { builder.append("author="); builder.append(author); builder.append(", "); diff --git a/base/src/main/java/bjc/utils/components/FileComponentRepository.java b/base/src/main/java/bjc/utils/components/FileComponentRepository.java index 113c6dc..82d2770 100644 --- a/base/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -22,12 +22,13 @@ import bjc.utils.funcutils.FileUtils; * @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. */ - private static final Logger CLASS_LOGGER = Logger.getLogger("FileComponentRepository"); + private static final Logger CLASS_LOGGER + = Logger.getLogger("FileComponentRepository"); /* The internal storage of components. */ private IMap<String, ComponentType> components; @@ -39,27 +40,27 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> * Create a new component repository sourcing components from files in a * 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. + * 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) { + if (directory == null) { throw new NullPointerException("Directory must not be null"); - } else if(!directory.isDirectory()) { + } 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) { + } else if (componentReader == null) { throw new NullPointerException("Component reader must not be null"); } @@ -71,40 +72,40 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> 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. - */ - return false; - } - - /* - * Don't skip the first directory, that's the parent - * directory. - */ - isFirstDir.replace(false); - - return true; - }; + final BiPredicate<Path, BasicFileAttributes> 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 { - FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, (pth, attr) -> { - loadComponent(componentReader, pth); - - /* - * Keep loading components, even if this one - * failed. - */ - return true; - }); - } catch(final IOException ioex) { - CLASS_LOGGER.log(Level.WARNING, ioex, () -> "Error found reading component from file."); + FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, + (pth, attr) -> { + loadComponent(componentReader, pth); + + /* + * Keep loading components, even if this one failed. + */ + return true; + }); + } catch (final IOException ioex) { + CLASS_LOGGER.log(Level.WARNING, ioex, + () -> "Error found reading component from file."); } } @@ -129,35 +130,39 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> } /* Load a component from a file */ - private void loadComponent(final Function<File, ? extends ComponentType> componentReader, final Path pth) { + private void loadComponent( + final Function<File, ? extends ComponentType> componentReader, + final Path pth) { try { /* 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())) { + } else if (!components.containsKey(component.getName())) { /* - * We only care about the latest version of a - * component. + * We only care about the latest version of a component. */ - final ComponentType oldComponent = components.put(component.getName(), component); + final ComponentType oldComponent + = components.put(component.getName(), component); - if(oldComponent.getVersion() > component.getVersion()) { + if (oldComponent.getVersion() > component.getVersion()) { components.put(oldComponent.getName(), oldComponent); } } else { 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( + "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) { - String msg = String.format("Error found reading component from file %s. It will not be loaded.", + } catch (final Exception ex) { + 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); @@ -176,13 +181,13 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> final StringBuilder builder = new StringBuilder(); builder.append("FileComponentRepository ["); - if(components != null) { + if (components != null) { builder.append("components="); builder.append(components); builder.append(", "); } - if(sourceDirectory != null) { + if (sourceDirectory != null) { builder.append("sourceDirectory="); builder.append(sourceDirectory); } diff --git a/base/src/main/java/bjc/utils/components/IComponentRepository.java b/base/src/main/java/bjc/utils/components/IComponentRepository.java index 9339a19..5ebb1de 100644 --- a/base/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/IComponentRepository.java @@ -10,7 +10,7 @@ import bjc.funcdata.IMap; * @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> { /** @@ -25,10 +25,9 @@ public interface IComponentRepository<ComponentType extends IDescribedComponent> * Get a component with a specific name. * * @param name - * The name of the component to retrieve. + * The name of the component to retrieve. * - * @return The named component, or null if no component with that name - * exists. + * @return The named component, or null if no component with that name exists. */ public ComponentType getByName(String name); diff --git a/base/src/main/java/bjc/utils/components/IDescribedComponent.java b/base/src/main/java/bjc/utils/components/IDescribedComponent.java index 6921849..ae3e06c 100644 --- a/base/src/main/java/bjc/utils/components/IDescribedComponent.java +++ b/base/src/main/java/bjc/utils/components/IDescribedComponent.java @@ -22,8 +22,8 @@ public interface IDescribedComponent extends Comparable<IDescribedComponent> { /** * Get the description of this component. * - * Providing this is optional, with the default being a note that no - * description was provided. + * Providing this is optional, with the default being a note that no description + * was provided. * * @return The description of the component */ @@ -55,7 +55,7 @@ public interface IDescribedComponent extends Comparable<IDescribedComponent> { default int compareTo(final IDescribedComponent o) { int res = getName().compareTo(o.getName()); - if(res == 0) { + if (res == 0) { res = getVersion() - o.getVersion(); } diff --git a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java index 22bc55d..ec3911e 100644 --- a/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/MemoryComponentRepository.java @@ -4,10 +4,11 @@ import bjc.funcdata.IMap; /** * A repository of components stored in memory. - * + * * @author bjculkin * - * @param <ComponentType> The type of component stored in the repository. + * @param <ComponentType> + * The type of component stored in the repository. */ public class MemoryComponentRepository<ComponentType extends IDescribedComponent> implements IComponentRepository<ComponentType> { @@ -17,9 +18,9 @@ public class MemoryComponentRepository<ComponentType extends IDescribedComponent /** * Create a new memory component repository. - * + * * @param repo - * The set of components to use. + * The set of components to use. */ public MemoryComponentRepository(IMap<String, ComponentType> repo) { this(repo, "memory"); @@ -27,11 +28,11 @@ public class MemoryComponentRepository<ComponentType extends IDescribedComponent /** * Create a new memory component repository. - * + * * @param repo - * The set of components to use. + * The set of components to use. * @param source - * Where the components came from. + * Where the components came from. */ public MemoryComponentRepository(IMap<String, ComponentType> repo, String source) { this.repo = repo; @@ -44,10 +45,12 @@ public class MemoryComponentRepository<ComponentType extends IDescribedComponent return repo; } + @Override public ComponentType getByName(String name) { return repo.get(name); } + @Override public String getSource() { return source; } |
