diff options
3 files changed, 178 insertions, 0 deletions
diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java new file mode 100644 index 0000000..9dd881c --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -0,0 +1,90 @@ +package bjc.utils.components; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.InputStream; +import java.util.HashMap; +import java.util.Map; +import java.util.function.Function; + +import bjc.utils.funcdata.FunctionalList; + +/** + * A component repository that loads its components from files in a + * directory + * + * @author ben + * + * @param <E> + * The type of component being read in + */ +public class FileComponentRepository<E extends IDescribedComponent> + implements IComponentRepository<E> { + + /** + * The internal storage of components + */ + private Map<String, E> comps; + + /** + * The path that all the components came from + */ + private String sourcePath; + + /** + * Create a new component repository sourcing components from files in + * a directory + * + * @param dir + * The directory to read component files from + * @param reader + * The function to use to convert files to components + */ + public FileComponentRepository(File dir, + Function<InputStream, E> reader) { + comps = new HashMap<>(); + sourcePath = dir.getAbsolutePath(); + + for (File fle : dir.listFiles()) { + if (fle.isDirectory()) { + // Do nothing with directories. They probably contain + // support files for components + } else { + try { + E comp = reader.apply(new FileInputStream(fle)); + + comps.put(comp.getName(), comp); + } catch (FileNotFoundException fnfx) { + System.err.println("Couldn't read component file: " + + fle.getAbsolutePath() + "\nReason: " + + fnfx.getMessage()); + + fnfx.printStackTrace(System.err); + } + } + } + } + + @Override + public FunctionalList<E> getComponentList() { + FunctionalList<E> ret = new FunctionalList<>(); + + comps.forEach((name, comp) -> { + ret.add(comp); + }); + + return ret; + } + + @Override + public Map<String, E> getComponents() { + return comps; + } + + @Override + public String getSource() { + return "Read from directory " + sourcePath + "."; + } + +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java new file mode 100644 index 0000000..a326a78 --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IComponentRepository.java @@ -0,0 +1,35 @@ +package bjc.utils.components; + +import java.util.Map; + +import bjc.utils.funcdata.FunctionalList; + +/** + * A collection of implementations of {@link IDescribedComponent} + * + * @author ben + * + */ +public interface IComponentRepository<E extends IDescribedComponent> { + /** + * Get a list of all the registered componets + * + * @return A list of all the registered components + */ + public FunctionalList<E> getComponentList(); + + /** + * Get all of the components this repository knows about + * + * @return A map from component name to component, containing all of + * the components in the repositorys + */ + public Map<String, E> getComponents(); + + /** + * Get the source from which these components came + * + * @return The source from which these components came + */ + public String getSource(); +} diff --git a/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java new file mode 100644 index 0000000..f1d8b1c --- /dev/null +++ b/BJC-Utils2/src/main/java/bjc/utils/components/IDescribedComponent.java @@ -0,0 +1,53 @@ +package bjc.utils.components; + +/** + * Represents a optional component that has status information associated + * with it + * + * @author ben + * + */ +public interface IDescribedComponent { + /** + * Get the name of this component. + * + * This is the only thing required of all components + * + * @return The name of the component + */ + public String getName(); + + /** + * Get the description of this component + * + * Providing this is optional, with the default being a note that no + * description was provided + * + * @return The description of the component + */ + public default String getDescription() { + return "No description provided."; + } + + /** + * Get the author of this component + * + * Providing this is optional, with "Anonymous" as the default author + * + * @return The author of the component + */ + public default String getAuthor() { + return "Anonymous"; + } + + /** + * Get the version of this component + * + * Providing this is optional, with "1" as the default version + * + * @return The version of this component + */ + public default int getVersion() { + return 1; + } +} |
