From 61fd71f69e080790da722e0e03b71ecd7c2538a2 Mon Sep 17 00:00:00 2001 From: bculkin2442 Date: Tue, 10 May 2016 16:02:45 -0400 Subject: General update --- .../utils/components/FileComponentRepository.java | 127 ++++++++++++--------- 1 file changed, 71 insertions(+), 56 deletions(-) (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 index e05afc0..182549c 100644 --- a/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -6,42 +6,36 @@ import java.nio.file.Path; import java.nio.file.attribute.BasicFileAttributes; import java.util.function.BiPredicate; 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.FunctionalList; import bjc.utils.funcdata.FunctionalMap; -import bjc.utils.funcdata.IFunctionalList; -import bjc.utils.funcdata.IFunctionalMap; +import bjc.utils.funcdata.IList; +import bjc.utils.funcdata.IMap; import bjc.utils.funcutils.FileUtils; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - /** * A component repository that loads its components from files in a * directory * * @author ben * - * @param + * @param * The type of component being read in */ -public class FileComponentRepository - implements IComponentRepository { +public class FileComponentRepository + implements IComponentRepository { + // 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 = LoggerFactory - .getLogger(FileComponentRepository.class); + // The internal storage of components + private IMap components; - /** - * The internal storage of components - */ - private IFunctionalMap components; - - /** - * The path that all the components came from - */ - private Path sourceDirectory; + // The path that all the components came from + private Path sourceDirectory; /** * Create a new component repository sourcing components from files in @@ -57,33 +51,47 @@ public class FileComponentRepository * The function to use to convert files to components */ public FileComponentRepository(File directory, - Function componentReader) { + Function componentReader) { + // Make sure we have valid arguments 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"); } + // Initialize our fields components = new FunctionalMap<>(); - sourceDirectory = directory.toPath().toAbsolutePath(); + // Marker for making sure we don't skip the parent IHolder isFirstDir = new Identity<>(true); - BiPredicate 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; - } - - return true; - }; - + // Predicate to use to traverse all the files in a directory, but + // not recurse into sub-directories + BiPredicate firstLevelTraverser = + (pth, attr) -> { + if (attr.isDirectory() && !isFirstDir.getValue()) { + + /* + * Skip directories, they probably have component + * support files. + */ + return false; + } + + /* + * Don't skip the first directory, that's the parent + * directory + */ + isFirstDir.replace(false); + + return true; + }; + + // Try reading components try { FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, (pth, attr) -> { @@ -93,28 +101,23 @@ public class FileComponentRepository return true; }); } catch (IOException ioex) { - CLASS_LOGGER.warn("Error found reading component from file.", - ioex); + CLASS_LOGGER.log(Level.WARNING, ioex, + () -> "Error found reading component from file."); } } @Override - public E getComponentByName(String name) { + public ComponentType getByName(String name) { return components.get(name); } @Override - public IFunctionalList getComponentList() { - IFunctionalList returnedList = new FunctionalList<>(); - - components - .forEach((name, component) -> returnedList.add(component)); - - return returnedList; + public IList getList() { + return components.valueList(); } @Override - public IFunctionalMap getComponents() { + public IMap getAll() { return components; } @@ -123,26 +126,38 @@ public class FileComponentRepository return "Components read from directory " + sourceDirectory + "."; } - private void loadComponent(Function componentReader, + /* + * Load a component from a file + */ + private void loadComponent( + Function componentReader, Path pth) { try { - E component = componentReader.apply(pth.toFile()); + // Try to load the component + ComponentType 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); + // 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); + } } else { - CLASS_LOGGER.warn("Found a duplicate component.\n" + CLASS_LOGGER.warning("Found a duplicate component.\n" + "Multiple versions of the same component are not currently supported.\n" - + "The component" + component - + " will not be registered ."); + + "Only the latest version of the component" + + component + " will be registered ."); } } catch (Exception ex) { - CLASS_LOGGER.warn("Error found reading component from file " - + pth.toString() - + ". This component will not be loaded", ex); + 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 -- cgit v1.2.3