From 120ac7034bcfa91a02b1b68c0507fd3ef80a0cfa Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 1 Mar 2016 20:43:51 -0500 Subject: Implemented support for component-based shenanigans. By default, only a source for retrieving components from streams is implemented --- .../utils/components/FileComponentRepository.java | 90 ++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java (limited to 'BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java') 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 + * The type of component being read in + */ +public class FileComponentRepository + implements IComponentRepository { + + /** + * The internal storage of components + */ + private Map 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 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 getComponentList() { + FunctionalList ret = new FunctionalList<>(); + + comps.forEach((name, comp) -> { + ret.add(comp); + }); + + return ret; + } + + @Override + public Map getComponents() { + return comps; + } + + @Override + public String getSource() { + return "Read from directory " + sourcePath + "."; + } + +} -- cgit v1.2.3