Module hw06

Class ObjectLoaderPath<ReturnT>

java.lang.Object
provided.utils.loader.impl.ObjectLoaderPath<ReturnT>
Type Parameters:
ReturnT - The type of object loaded by this IObjectLoader
All Implemented Interfaces:
IObjectLoader<ReturnT>

public class ObjectLoaderPath<ReturnT> extends Object implements IObjectLoader<ReturnT>
An IObjectLoader implementation that will walk through multiple locations, defined by multiple classname prefixes, to load an object of the specified type.
Author:
swong
  • Field Details

    • logger

      private ILogger logger
      Logger to use
    • classnamePrefixes

      private ArrayList<String> classnamePrefixes
      List of of the given classname prefixes, which is effectively a path list
    • errorFac

      Function<Object[],​ReturnT> errorFac
      The error factory to use in case the class is never found
  • Constructor Details

    • ObjectLoaderPath

      public ObjectLoaderPath(Function<Object[],​ReturnT> errorFac, String... classnamePrefixes)
      Constructor for the class. The given errorFac is used to generate instances when the loadInstance() method is otherwise unable to do so because of a processing error.
      Parameters:
      errorFac - A factory method that takes the same array of input parameters that loadInstance() takes and returns an instance of ReturnT.
      classnamePrefixes - Vararg of the prefixes to search for the given classnames. The prefixes should complete the fully qualified pathname when pre=pended to the className given to loadInstance(). The prefixes are searched in the order given.
  • Method Details

    • exceptionHandler

      ReturnT exceptionHandler(Exception ex, String fullClassName, Object... params)
      Handler for exceptions or running out of places to search.
      Parameters:
      ex - The Exception to be handled
      fullClassName - The fully qualified class name being searched for
      params - Intended class constructor parameters
      Returns:
      The error object generated by the stored errorFac.
    • loadInstance

      public ReturnT loadInstance(String className, Object... args)
      Instantiates and then delegates to a recursive IObjectLoader instance that goes through the saved list of classname prefixes searching for the partial name, className. If the class is never found, the exception printed and the saved errorFac will be invoked to generate an error object result.
      Specified by:
      loadInstance in interface IObjectLoader<ReturnT>
      Parameters:
      className - The fully-qualified name of the desired class which must be assignable to the ReturnT type.
      args - a varargs of input parameter values for the constructor of className
      Returns:
      An instance of the desired class, as ReturnT