From 77fcc58d1facffbc3af50be8c05985350e9f1355 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Sun, 17 Apr 2016 15:01:44 -0400 Subject: Code maintenace and changes --- .../components/ComponentDescriptionFileParser.java | 59 +++++++------- .../utils/components/FileComponentRepository.java | 90 ++++++++++++---------- 2 files changed, 78 insertions(+), 71 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 254e380..eec3fa8 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java @@ -1,8 +1,10 @@ package bjc.utils.components; import java.io.InputStream; +import java.util.function.BiConsumer; import bjc.utils.exceptions.PragmaFormatException; +import bjc.utils.funcdata.FunctionalStringTokenizer; import bjc.utils.funcutils.ListUtils; import bjc.utils.parserutils.RuleBasedConfigReader; @@ -26,40 +28,21 @@ public class ComponentDescriptionFileParser { // Don't need to do anything on rule end }); - reader.addPragma("name", (tokenizer, state) -> { - if (!tokenizer.hasMoreTokens()) { - throw new PragmaFormatException( - "Pragma name requires one string argument"); - } - - state.setName(ListUtils - .collapseTokens(tokenizer.toList((strang) -> strang))); - }); - - reader.addPragma("author", (tokenizer, state) -> { - if (!tokenizer.hasMoreTokens()) { - throw new PragmaFormatException( - "Pragma author requires one string argument"); - } + setupReaderPragmas(); + } - state.setAuthor(ListUtils - .collapseTokens(tokenizer.toList((strang) -> strang))); - }); + private static void setupReaderPragmas() { + reader.addPragma("name", buildStringCollapserPragma("name")); - reader.addPragma("description", (tokenizer, state) -> { - if (!tokenizer.hasMoreTokens()) { - throw new PragmaFormatException( - "Pragma description requires one string argument"); - } + reader.addPragma("author", buildStringCollapserPragma("author")); - state.setDescription(ListUtils - .collapseTokens(tokenizer.toList((strang) -> strang))); - }); + reader.addPragma("description", + buildStringCollapserPragma("description")); reader.addPragma("version", (tokenizer, state) -> { if (!tokenizer.hasMoreTokens()) { throw new PragmaFormatException( - "Pragma name requires one integer argument"); + "Pragma version requires one integer argument"); } String token = tokenizer.nextToken(); @@ -67,8 +50,8 @@ public class ComponentDescriptionFileParser { try { state.setVersion(Integer.parseInt(token)); } catch (NumberFormatException nfex) { - PragmaFormatException pfex = new PragmaFormatException( - "Argument " + token + PragmaFormatException pfex = + new PragmaFormatException("Argument " + token + " to version pragma isn't a valid integer. " + "This pragma requires a integer argument"); @@ -79,6 +62,20 @@ public class ComponentDescriptionFileParser { }); } + private static + BiConsumer + buildStringCollapserPragma(String pragmaName) { + return (tokenizer, state) -> { + if (!tokenizer.hasMoreTokens()) { + throw new PragmaFormatException("Pragma " + pragmaName + + " requires one string argument"); + } + + state.setName(ListUtils + .collapseTokens(tokenizer.toList((strang) -> strang))); + }; + } + /** * Parse a component description from a stream * @@ -86,8 +83,8 @@ public class ComponentDescriptionFileParser { * The stream to parse from * @return The description parsed from the stream */ - public static ComponentDescription fromStream( - InputStream inputSource) { + public static ComponentDescription + fromStream(InputStream inputSource) { 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 39a7eed..53219fb 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -3,13 +3,15 @@ package bjc.utils.components; import java.io.File; import java.io.IOException; import java.nio.file.Path; +import java.nio.file.attribute.BasicFileAttributes; +import java.util.function.BiPredicate; import java.util.function.Function; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import bjc.utils.data.experimental.IHolder; -import bjc.utils.data.experimental.Identity; +import bjc.utils.data.IHolder; +import bjc.utils.data.Identity; import bjc.utils.funcdata.FunctionalList; import bjc.utils.funcdata.FunctionalMap; import bjc.utils.funcdata.IFunctionalList; @@ -28,8 +30,8 @@ import bjc.utils.funcutils.FileUtils; public class FileComponentRepository implements IComponentRepository { - private static final Logger CLASS_LOGGER = LoggerFactory - .getLogger(FileComponentRepository.class); + private static final Logger CLASS_LOGGER = + LoggerFactory.getLogger(FileComponentRepository.class); /** * The internal storage of components @@ -68,49 +70,57 @@ public class FileComponentRepository IHolder isFirstDir = new Identity<>(true); - try { - FileUtils.traverseDirectory(sourceDirectory, (pth, attr) -> { - if (attr.isDirectory() && !isFirstDir.getValue()) { - // Don't skip the first directory, that's the parent - isFirstDir.replace(false); - // Skip directories, they probably have component - return false; - } - - return true; - }, (pth, attr) -> { - try { - E component = componentReader.apply(pth.toFile()); - - 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 ."); + BiPredicate firstLevelTraverser = + (pth, attr) -> { + if (attr.isDirectory() && !isFirstDir.getValue()) { + // Don't skip the first directory, that's the + // parent + isFirstDir.replace(false); + // Skip directories, they probably have + // component + return false; } - } catch (Exception ex) { - CLASS_LOGGER.warn( - "Error found reading component from file " - + pth.toString() - + ". This component will not be loaded", - ex); - } - - // Keep loading components, even if this one failed - return true; - }); + + return true; + }; + + try { + FileUtils.traverseDirectory(sourceDirectory, + firstLevelTraverser, (pth, attr) -> { + loadComponent(componentReader, pth); + + // Keep loading components, even if this one failed + return true; + }); } catch (IOException ioex) { CLASS_LOGGER.warn("Error found reading component from file.", ioex); } } + private void loadComponent(Function componentReader, + Path pth) { + try { + E component = componentReader.apply(pth.toFile()); + + 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) { + CLASS_LOGGER.warn("Error found reading component from file " + + pth.toString() + + ". This component will not be loaded", ex); + } + } + @Override public IFunctionalList getComponentList() { IFunctionalList returnedList = new FunctionalList<>(); -- cgit v1.2.3