From 8a8b457c98e207d809a7616e73eb59bfe197a7a5 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Thu, 31 Mar 2016 11:43:21 -0400 Subject: More code maintenance --- .../components/ComponentDescriptionFileParser.java | 49 ++++++++------- .../utils/components/FileComponentRepository.java | 73 ++++++++++++++++------ .../bjc/utils/components/IComponentRepository.java | 10 +++ 3 files changed, 91 insertions(+), 41 deletions(-) (limited to 'BJC-Utils2/src/main/java/bjc/utils/components') 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 bf49762..9d18390 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -18,50 +18,50 @@ public class ComponentDescriptionFileParser { static { // This reader works entirely off of pragmas, so no need to handle // rules - reader = new RuleBasedConfigReader<>((fst, par) -> { - }, (fst, stat) -> { - }, (stat) -> { + reader = new RuleBasedConfigReader<>((tokenizer, statePair) -> { + }, (tokenizer, state) -> { + }, (state) -> { }); - reader.addPragma("name", (fst, stat) -> { - if (!fst.hasMoreTokens()) { + reader.addPragma("name", (tokenizer, state) -> { + if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException( "Pragma name requires at least one argument"); } else { - stat.setName( - ListUtils.collapseTokens(fst.toList((s) -> s))); + state.setName(ListUtils.collapseTokens( + tokenizer.toList((strang) -> strang))); } }); - reader.addPragma("author", (fst, stat) -> { - if (!fst.hasMoreTokens()) { + reader.addPragma("author", (tokenizer, state) -> { + if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException( "Pragma author requires at least one argument"); } else { - stat.setAuthor( - ListUtils.collapseTokens(fst.toList((s) -> s))); + state.setAuthor(ListUtils.collapseTokens( + tokenizer.toList((strang) -> strang))); } }); - reader.addPragma("description", (fst, stat) -> { - if (!fst.hasMoreTokens()) { + reader.addPragma("description", (tokenizer, state) -> { + if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException( "Pragma description requires at least one argument"); } else { - stat.setDescription( - ListUtils.collapseTokens(fst.toList((s) -> s))); + state.setDescription(ListUtils.collapseTokens( + tokenizer.toList((strang) -> strang))); } }); - reader.addPragma("version", (fst, stat) -> { - if (!fst.hasMoreTokens()) { + reader.addPragma("version", (tokenizer, state) -> { + if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException( "Pragma name requires at least one argument"); } else { - String token = fst.nextToken(); + String token = tokenizer.nextToken(); try { - stat.setVersion(Integer.parseInt(token)); + state.setVersion(Integer.parseInt(token)); } catch (NumberFormatException nfex) { throw new PragmaFormatException("Argument " + token + " to version pragma isn't a valid integer. " @@ -74,12 +74,15 @@ public class ComponentDescriptionFileParser { /** * Parse a component description from a stream * - * @param is + * @param inputSource * The stream to parse from * @return The description parsed from the stream */ - public static ComponentDescription fromStream(InputStream is) { - return reader.fromStream(is, new ComponentDescriptionState()) - .toDescription(); + public static ComponentDescription + fromStream(InputStream inputSource) { + ComponentDescriptionState readState = reader + .fromStream(inputSource, new ComponentDescriptionState()); + + return readState.toDescription(); } } 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 f2d2cf8..d808b8e 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -1,10 +1,12 @@ package bjc.utils.components; import java.io.File; +import java.nio.file.Path; import java.util.HashMap; import java.util.Map; import java.util.function.Function; +import org.slf4j.Logger; import org.slf4j.LoggerFactory; import bjc.utils.funcdata.FunctionalList; @@ -21,15 +23,18 @@ import bjc.utils.funcdata.FunctionalList; public class FileComponentRepository implements IComponentRepository { + private static final Logger CLASS_LOGGER = + LoggerFactory.getLogger(FileComponentRepository.class); + /** * The internal storage of components */ - private Map comps; + private Map components; /** * The path that all the components came from */ - private String sourcePath; + private Path sourceDirectory; /** * Create a new component repository sourcing components from files in @@ -39,28 +44,54 @@ public class FileComponentRepository * cause the loading of that component to fail, but a warning will be * logged. * - * @param dir + * @param directory * The directory to read component files from - * @param reader + * @param componentReader * The function to use to convert files to components */ - public FileComponentRepository(File dir, Function reader) { - comps = new HashMap<>(); - sourcePath = dir.getAbsolutePath(); + public FileComponentRepository(File directory, + Function componentReader) { + if (!directory.isDirectory()) { + throw new IllegalArgumentException("File " + directory + + " is not a directory.\n" + + "Components can only be read from a directory"); + } + + components = new HashMap<>(); + sourceDirectory = directory.toPath().toAbsolutePath(); - for (File fle : dir.listFiles()) { - if (fle.isDirectory()) { + File[] listFiles = directory.listFiles(); + + if (listFiles == null) { + throw new IllegalArgumentException("File " + directory + + " is not a valid directory.\n" + + "Components can only be read from a directory"); + } + + for (File componentFile : listFiles) { + if (componentFile.isDirectory()) { // Do nothing with directories. They probably contain // support files for components } else { try { - E comp = reader.apply(fle); + E component = componentReader.apply(componentFile); - comps.put(comp.getName(), comp); + if (component == null) { + throw new NullPointerException( + "Component reader read null component"); + } else if (!components + .containsKey(component.getName())) { + components.put(component.getName(), component); + } else { + CLASS_LOGGER.warn("Found a duplicate component.\n" + + "Multiple versions of the same component are not currently supported.\n" + + "The component" + component + + " will not be registered ."); + } } catch (Exception ex) { - LoggerFactory.getLogger(getClass()) + CLASS_LOGGER .warn("Error found reading component from file " - + fle.toString() + + componentFile.toString() + ". This component will not be loaded"); } @@ -70,20 +101,26 @@ public class FileComponentRepository @Override public FunctionalList getComponentList() { - FunctionalList ret = new FunctionalList<>(); + FunctionalList returnedList = new FunctionalList<>(); - comps.forEach((name, comp) -> ret.add(comp)); + components + .forEach((name, component) -> returnedList.add(component)); - return ret; + return returnedList; } @Override public Map getComponents() { - return comps; + return components; } @Override public String getSource() { - return "Components read from directory " + sourcePath + "."; + return "Components read from directory " + sourceDirectory + "."; + } + + @Override + public E getComponentByName(String name) { + return components.get(name); } } diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java index 3e284e6..04ff51b 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java @@ -28,6 +28,16 @@ public interface IComponentRepository { */ public Map getComponents(); + /** + * 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 + */ + public E getComponentByName(String name); + /** * Get the source from which these components came * -- cgit v1.2.3