summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/components
diff options
context:
space:
mode:
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/components')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java35
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java29
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java19
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java85
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java11
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java4
6 files changed, 81 insertions, 102 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java
index 1ec5b80..c473150 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java
@@ -7,50 +7,47 @@ package bjc.utils.components;
*
*/
public class ComponentDescription implements IDescribedComponent {
- private static void sanityCheckArgs(String name, String author,
- String description, int version) {
+ private static void sanityCheckArgs(String name, String author, String description, 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");
+ throw new IllegalArgumentException("Component version must be greater than 0");
}
}
/**
* The author of the component
*/
- private String author;
+ private String author;
/**
* The description of the component
*/
- private String description;
+ private String description;
/**
* The name of the component
*/
- private String name;
+ private String name;
/**
* The version of the component
*/
- private int version;
+ private int version;
/**
* 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(String name, String author,
- String description, int version) {
+ public ComponentDescription(String name, String author, String description, int version) {
sanityCheckArgs(name, author, description, version);
this.name = name;
@@ -61,19 +58,19 @@ public class ComponentDescription implements IDescribedComponent {
@Override
public String getAuthor() {
- if(author == null) {
+ if (author == null) {
return IDescribedComponent.super.getAuthor();
}
-
+
return author;
}
@Override
public String getDescription() {
- if(description == null) {
+ if (description == null) {
return IDescribedComponent.super.getDescription();
}
-
+
return description;
}
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 ad5e1a2..22221d2 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
@@ -19,7 +19,8 @@ public class ComponentDescriptionFileParser {
// Initialize the reader and its pragmas
static {
- // This reader works entirely off of pragmas, so no need to handle
+ // 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
@@ -36,17 +37,15 @@ 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(
- InputStream inputSource) {
- if(inputSource == null) {
+ public static ComponentDescription fromStream(InputStream inputSource) {
+ if (inputSource == null) {
throw new NullPointerException("Input source must not be null");
}
-
- ComponentDescriptionState readState = reader
- .fromStream(inputSource, new ComponentDescriptionState());
+
+ ComponentDescriptionState readState = reader.fromStream(inputSource, new ComponentDescriptionState());
return readState.toDescription();
}
@@ -55,17 +54,13 @@ 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("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)));
}
}
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java
index a17a70b..70f308b 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java
@@ -8,22 +8,22 @@ package bjc.utils.components;
*/
public class ComponentDescriptionState {
// Tentative name of this component
- private String name;
+ private String name;
// Tentative description of this componet
- private String description;
+ private String description;
// Tentative author of this component
- private String author;
+ private String author;
// Tentative version of this component
- private int version;
+ private int version;
/**
* Set the author of this component
*
* @param author
- * The author of this component
+ * The author of this component
*/
public void setAuthor(String author) {
this.author = author;
@@ -33,7 +33,7 @@ public class ComponentDescriptionState {
* Set the description of this component
*
* @param description
- * The description of this component
+ * The description of this component
*/
public void setDescription(String description) {
this.description = description;
@@ -43,7 +43,7 @@ public class ComponentDescriptionState {
* Set the name of this component
*
* @param name
- * The name of this component
+ * The name of this component
*/
public void setName(String name) {
this.name = name;
@@ -53,7 +53,7 @@ public class ComponentDescriptionState {
* Set the version of this component
*
* @param version
- * The version of this component
+ * The version of this component
*/
public void setVersion(int version) {
this.version = version;
@@ -65,7 +65,6 @@ public class ComponentDescriptionState {
* @return The description represented by this state
*/
public ComponentDescription toDescription() {
- return new ComponentDescription(name, author, description,
- version);
+ return new ComponentDescription(name, author, description, version);
}
}
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 e98c06b..7aa7c07 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java
@@ -17,51 +17,45 @@ 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
- 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;
+ private IMap<String, ComponentType> components;
// The path that all the components came from
- private Path sourceDirectory;
+ private Path sourceDirectory;
/**
- * Create a new component repository sourcing components from files in
- * a directory
+ * 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(File directory,
- Function<File, ? extends ComponentType> componentReader) {
+ public FileComponentRepository(File directory, Function<File, ? extends ComponentType> componentReader) {
// 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"
+ 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");
+ throw new NullPointerException("Component reader must not be null");
}
// Initialize our fields
@@ -71,20 +65,21 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
// Marker for making sure we don't skip the parent
IHolder<Boolean> isFirstDir = new Identity<>(true);
- // Predicate to use to traverse all the files in a directory, but
+ // Predicate to use to traverse all the files in a directory,
+ // but
// not recurse into sub-directories
- BiPredicate<Path, BasicFileAttributes> firstLevelTraverser = (pth,
- attr) -> {
+ BiPredicate<Path, BasicFileAttributes> firstLevelTraverser = (pth, attr) -> {
if (attr.isDirectory() && !isFirstDir.getValue()) {
/*
- * Skip directories, they probably have component support
- * files.
+ * Skip directories, they probably have
+ * component support files.
*/
return false;
}
/*
- * Don't skip the first directory, that's the parent directory
+ * Don't skip the first directory, that's the parent
+ * directory
*/
isFirstDir.replace(false);
@@ -93,16 +88,15 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
// Try reading components
try {
- FileUtils.traverseDirectory(sourceDirectory,
- firstLevelTraverser, (pth, attr) -> {
- loadComponent(componentReader, pth);
+ FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, (pth, attr) -> {
+ loadComponent(componentReader, pth);
- // Keep loading components, even if this one failed
- return true;
- });
+ // Keep loading components, even if this one
+ // failed
+ return true;
+ });
} catch (IOException ioex) {
- CLASS_LOGGER.log(Level.WARNING, ioex,
- () -> "Error found reading component from file.");
+ CLASS_LOGGER.log(Level.WARNING, ioex, () -> "Error found reading component from file.");
}
}
@@ -129,20 +123,17 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
/*
* Load a component from a file
*/
- private void loadComponent(
- Function<File, ? extends ComponentType> componentReader,
- Path pth) {
+ private void loadComponent(Function<File, ? extends ComponentType> componentReader, Path pth) {
try {
// Try to load the component
ComponentType component = componentReader.apply(pth.toFile());
if (component == null) {
- throw new NullPointerException(
- "Component reader read null component");
+ throw new NullPointerException("Component reader read null component");
} else if (!components.containsKey(component.getName())) {
- // We only care about the latest version of a component
- ComponentType oldComponent = components
- .put(component.getName(), component);
+ // We only care about the latest version of a
+ // component
+ ComponentType oldComponent = components.put(component.getName(), component);
if (oldComponent.getVersion() > component.getVersion()) {
components.put(oldComponent.getName(), oldComponent);
@@ -150,14 +141,12 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
} 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 .");
+ + "Only the latest version of the component" + component
+ + " will be registered .");
}
} catch (Exception ex) {
- CLASS_LOGGER.log(Level.WARNING, ex,
- () -> "Error found reading component from file "
- + pth.toString()
- + ". This component will not be loaded");
+ CLASS_LOGGER.log(Level.WARNING, ex, () -> "Error found reading component from file "
+ + pth.toString() + ". This component will not be loaded");
}
}
} \ No newline at end of file
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 a4dbfde..f3aab76 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java
@@ -10,15 +10,14 @@ import bjc.utils.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> {
+public interface IComponentRepository<ComponentType extends IDescribedComponent> {
/**
* 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();
@@ -26,7 +25,7 @@ public interface IComponentRepository<
* 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
*/
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
index c3576a3..6b87ba7 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
@@ -1,8 +1,8 @@
package bjc.utils.components;
/**
- * Represents a optional component that has status information associated
- * with it
+ * Represents a optional component that has status information associated with
+ * it
*
* @author ben
*