summaryrefslogtreecommitdiff
path: root/BJC-Utils2/src/main/java/bjc/utils/components
diff options
context:
space:
mode:
authorbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:40:33 -0400
committerbculkin2442 <bjculkin@mix.wvu.edu>2017-04-10 16:40:33 -0400
commit889fac2bdf993dc86f64a8893c0260fdcf848acb (patch)
tree99ed08552efa86fdc5fdf4ddb8720d10e599fafe /BJC-Utils2/src/main/java/bjc/utils/components
parent1656b02144446aeedebb3d1179e07ed99c01861c (diff)
Cleanup
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/components')
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java62
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java7
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java54
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java70
-rw-r--r--BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java13
5 files changed, 106 insertions, 100 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 b750848..28f81d1 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescription.java
@@ -7,29 +7,30 @@ package bjc.utils.components;
*
*/
public class ComponentDescription implements IDescribedComponent {
- private static void sanityCheckArgs(String name, String author, String description, 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");
}
/**
* The author of the component
*/
- private String author;
+ private final String author;
/**
* The description of the component
*/
- private String description;
+ private final String description;
/**
* The name of the component
*/
- private String name;
+ private final String name;
/**
* The version of the component
*/
- private int version;
+ private final int version;
/**
* Create a new component description
@@ -45,7 +46,8 @@ public class ComponentDescription implements IDescribedComponent {
* @throws IllegalArgumentException
* thrown if version is less than 1
*/
- public ComponentDescription(String name, String author, String description, 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,14 +58,14 @@ public class ComponentDescription implements IDescribedComponent {
@Override
public String getAuthor() {
- if(author == null) return IDescribedComponent.super.getAuthor();
+ if (author == null) return IDescribedComponent.super.getAuthor();
return author;
}
@Override
public String getDescription() {
- if(description == null) return IDescribedComponent.super.getDescription();
+ if (description == null) return IDescribedComponent.super.getDescription();
return description;
}
@@ -85,7 +87,7 @@ public class ComponentDescription implements IDescribedComponent {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#hashCode()
*/
@Override
@@ -93,9 +95,9 @@ public class ComponentDescription implements IDescribedComponent {
final int prime = 31;
int result = 1;
- result = prime * result + ((author == null) ? 0 : author.hashCode());
- result = prime * result + ((description == null) ? 0 : description.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
+ result = prime * result + (author == null ? 0 : author.hashCode());
+ result = prime * result + (description == null ? 0 : description.hashCode());
+ result = prime * result + (name == null ? 0 : name.hashCode());
result = prime * result + version;
return result;
@@ -103,30 +105,30 @@ public class ComponentDescription implements IDescribedComponent {
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#equals(java.lang.Object)
*/
@Override
- public boolean equals(Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(getClass() != obj.getClass()) return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
- ComponentDescription other = (ComponentDescription) obj;
+ final ComponentDescription other = (ComponentDescription) 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;
}
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 5f98ce9..f7ddaff 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionFileParser.java
@@ -40,10 +40,11 @@ public class ComponentDescriptionFileParser {
* The stream to parse from
* @return The description parsed from the stream
*/
- public static ComponentDescription fromStream(InputStream inputSource) {
- if(inputSource == null) throw new NullPointerException("Input source must not be null");
+ public static ComponentDescription fromStream(final InputStream inputSource) {
+ if (inputSource == null) throw new NullPointerException("Input source must not be null");
- ComponentDescriptionState readState = reader.fromStream(inputSource, new ComponentDescriptionState());
+ final ComponentDescriptionState readState = reader.fromStream(inputSource,
+ new ComponentDescriptionState());
return readState.toDescription();
}
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 d6fbc5a..8d66f85 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/ComponentDescriptionState.java
@@ -25,7 +25,7 @@ public class ComponentDescriptionState {
* @param author
* The author of this component
*/
- public void setAuthor(String author) {
+ public void setAuthor(final String author) {
this.author = author;
}
@@ -35,7 +35,7 @@ public class ComponentDescriptionState {
* @param description
* The description of this component
*/
- public void setDescription(String description) {
+ public void setDescription(final String description) {
this.description = description;
}
@@ -45,7 +45,7 @@ public class ComponentDescriptionState {
* @param name
* The name of this component
*/
- public void setName(String name) {
+ public void setName(final String name) {
this.name = name;
}
@@ -55,7 +55,7 @@ public class ComponentDescriptionState {
* @param version
* The version of this component
*/
- public void setVersion(int version) {
+ public void setVersion(final int version) {
this.version = version;
}
@@ -73,62 +73,62 @@ public class ComponentDescriptionState {
final int prime = 31;
int result = 1;
- result = prime * result + ((author == null) ? 0 : author.hashCode());
- result = prime * result + ((description == null) ? 0 : description.hashCode());
- result = prime * result + ((name == null) ? 0 : name.hashCode());
+ result = prime * result + (author == null ? 0 : author.hashCode());
+ result = prime * result + (description == null ? 0 : description.hashCode());
+ result = prime * result + (name == null ? 0 : name.hashCode());
result = prime * result + version;
return result;
}
@Override
- public boolean equals(Object obj) {
- if(this == obj) return true;
- if(obj == null) return false;
- if(getClass() != obj.getClass()) return false;
+ public boolean equals(final Object obj) {
+ if (this == obj) return true;
+ if (obj == null) return false;
+ if (getClass() != obj.getClass()) return false;
- ComponentDescriptionState other = (ComponentDescriptionState) obj;
+ 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;
}
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
- StringBuilder builder = new StringBuilder();
+ 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/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java
index 6fd6177..efde5c7 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java
@@ -1,12 +1,5 @@
package bjc.utils.components;
-import bjc.utils.data.IHolder;
-import bjc.utils.data.Identity;
-import bjc.utils.funcdata.FunctionalMap;
-import bjc.utils.funcdata.IList;
-import bjc.utils.funcdata.IMap;
-import bjc.utils.funcutils.FileUtils;
-
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
@@ -16,6 +9,13 @@ import java.util.function.Function;
import java.util.logging.Level;
import java.util.logging.Logger;
+import bjc.utils.data.IHolder;
+import bjc.utils.data.Identity;
+import bjc.utils.funcdata.FunctionalMap;
+import bjc.utils.funcdata.IList;
+import bjc.utils.funcdata.IMap;
+import bjc.utils.funcutils.FileUtils;
+
/**
* A component repository that loads its components from files in a directory
*
@@ -47,33 +47,37 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
* @param componentReader
* The function to use to convert files to components
*/
- public FileComponentRepository(File directory, Function<File, ? extends ComponentType> componentReader) {
+ 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())
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");
+ else if (componentReader == null) throw new NullPointerException("Component reader must not be null");
// Initialize our fields
components = new FunctionalMap<>();
sourceDirectory = directory.toPath().toAbsolutePath();
// Marker for making sure we don't skip the parent
- IHolder<Boolean> isFirstDir = new Identity<>(true);
+ final IHolder<Boolean> isFirstDir = new Identity<>(true);
// Predicate to use to traverse all the files in a directory,
// but
// not recurse into sub-directories
- BiPredicate<Path, BasicFileAttributes> firstLevelTraverser = (pth, attr) -> {
- if(attr.isDirectory() && !isFirstDir
- .getValue()) /*
- * Skip directories,
- * they probably have
- * component support
- * files.
- */
+ final BiPredicate<Path, BasicFileAttributes> firstLevelTraverser = (pth, attr) -> {
+ if (attr.isDirectory() && !isFirstDir.getValue()) /*
+ * Skip
+ * directories,
+ * they
+ * probably
+ * have
+ * component
+ * support
+ * files.
+ */
return false;
/*
@@ -94,7 +98,7 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
// failed
return true;
});
- } catch(IOException ioex) {
+ } catch (final IOException ioex) {
CLASS_LOGGER.log(Level.WARNING, ioex, () -> "Error found reading component from file.");
}
}
@@ -105,7 +109,7 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
}
@Override
- public ComponentType getByName(String name) {
+ public ComponentType getByName(final String name) {
return components.get(name);
}
@@ -122,19 +126,19 @@ 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(final Function<File, ? extends ComponentType> componentReader, final Path pth) {
try {
// Try to load the component
- ComponentType component = componentReader.apply(pth.toFile());
+ 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
- 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 {
@@ -143,7 +147,7 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
+ "Only the latest version of the component" + component
+ " will be registered .");
}
- } catch(Exception ex) {
+ } catch (final Exception ex) {
CLASS_LOGGER.log(Level.WARNING, ex, () -> "Error found reading component from file "
+ pth.toString() + ". This component will not be loaded");
}
@@ -151,21 +155,21 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent>
/*
* (non-Javadoc)
- *
+ *
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
- StringBuilder builder = new StringBuilder();
+ 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/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
index f231924..952b375 100644
--- a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
+++ b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java
@@ -7,7 +7,7 @@ package bjc.utils.components;
* @author ben
*
*/
-public interface IDescribedComponent extends Comparable<IDescribedComponent>{
+public interface IDescribedComponent extends Comparable<IDescribedComponent> {
/**
* Get the author of this component
*
@@ -50,16 +50,15 @@ public interface IDescribedComponent extends Comparable<IDescribedComponent>{
default int getVersion() {
return 1;
}
-
-
+
@Override
- default int compareTo(IDescribedComponent o) {
+ default int compareTo(final IDescribedComponent o) {
int res = getName().compareTo(o.getName());
-
- if(res == 0) {
+
+ if (res == 0) {
res = getVersion() - o.getVersion();
}
-
+
return res;
}
} \ No newline at end of file