diff options
Diffstat (limited to 'base/src/main/java/bjc/utils/components/FileComponentRepository.java')
| -rw-r--r-- | base/src/main/java/bjc/utils/components/FileComponentRepository.java | 107 |
1 files changed, 56 insertions, 51 deletions
diff --git a/base/src/main/java/bjc/utils/components/FileComponentRepository.java b/base/src/main/java/bjc/utils/components/FileComponentRepository.java index 113c6dc..82d2770 100644 --- a/base/src/main/java/bjc/utils/components/FileComponentRepository.java +++ b/base/src/main/java/bjc/utils/components/FileComponentRepository.java @@ -22,12 +22,13 @@ import bjc.utils.funcutils.FileUtils; * @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; @@ -39,27 +40,27 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> * 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(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()) { String msg = String.format( "File %s is not a directory. Components can only be read from a directory.", directory); throw new IllegalArgumentException(msg); - } else if(componentReader == null) { + } else if (componentReader == null) { throw new NullPointerException("Component reader must not be null"); } @@ -71,40 +72,40 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> final IHolder<Boolean> isFirstDir = new Identity<>(true); /* - * Predicate to use to traverse all the files in a directory, - * but not recurse into sub-directories. + * Predicate to use to traverse all the files in a directory, but not recurse + * into sub-directories. */ - final BiPredicate<Path, BasicFileAttributes> 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; - }; + final BiPredicate<Path, BasicFileAttributes> 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) -> { - loadComponent(componentReader, pth); - - /* - * Keep loading components, even if this one - * failed. - */ - return true; - }); - } catch(final IOException ioex) { - CLASS_LOGGER.log(Level.WARNING, ioex, () -> "Error found reading component from file."); + FileUtils.traverseDirectory(sourceDirectory, firstLevelTraverser, + (pth, attr) -> { + loadComponent(componentReader, pth); + + /* + * Keep loading components, even if this one failed. + */ + return true; + }); + } catch (final IOException ioex) { + CLASS_LOGGER.log(Level.WARNING, ioex, + () -> "Error found reading component from file."); } } @@ -129,35 +130,39 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> } /* Load a component from a file */ - private void loadComponent(final Function<File, ? extends ComponentType> componentReader, final Path pth) { + private void loadComponent( + final Function<File, ? extends ComponentType> componentReader, + final Path pth) { try { /* Try to load the component. */ 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. + * We only care about the latest version of a component. */ - final 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 { StringBuilder sb = new StringBuilder(); sb.append("Found a duplicate component.\n"); - sb.append("Multiple versions of the same component are not currently supported.\n"); + sb.append( + "Multiple versions of the same component are not currently supported.\n"); sb.append("Only the latest version of the component "); sb.append(component); sb.append(" will be registered."); CLASS_LOGGER.warning(sb.toString()); } - } catch(final Exception ex) { - String msg = String.format("Error found reading component from file %s. It will not be loaded.", + } catch (final Exception ex) { + String msg = String.format( + "Error found reading component from file %s. It will not be loaded.", pth.toString()); CLASS_LOGGER.log(Level.WARNING, ex, () -> msg); @@ -176,13 +181,13 @@ public class FileComponentRepository<ComponentType extends IDescribedComponent> 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); } |
