summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/components
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 15:01:44 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2016-04-17 15:01:44 -0400
commit77fcc58d1facffbc3af50be8c05985350e9f1355 (patch)
treeb7b81d24c107e644924dc526f8bb034efc62d2dc /BJC-Utils2/src/main/java/bjc/utils/components
parenta5850915df72f5968fd1b281eb9e455d50c580ee (diff)
Code maintenace and changes
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/components')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java59
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java90
2 files changed, 78 insertions, 71 deletions
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<FunctionalStringTokenizer, ComponentDescriptionState>
+ 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<E extends IDescribedComponent>
implements IComponentRepository<E> {
- 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<E extends IDescribedComponent>
IHolder<Boolean> 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<Path, BasicFileAttributes> 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<File, E> 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<E> getComponentList() {
IFunctionalList<E> returnedList = new FunctionalList<>();