diff options
Diffstat (limited to 'BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java')
| -rw-r--r-- | BJC-Utils2/src/main/java/bjc/utils/components/FileComponentRepository.java | 85 |
1 files changed, 37 insertions, 48 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 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 |
