Class Problem

java.lang.Object
nz.org.riskscape.problem.Problem
All Implemented Interfaces:
Iterable<Problem>, Problems
Direct Known Subclasses:
Unchecked

public class Problem extends Object implements Problems

Class used for describing errors and other issues with user defined input. Problems are hierarchical, which is used to 'target' where issues are found, e.g. a problem with your function that is in your model that is in your project might have three levels, e.g. There was a problem in your project... -> There was a problem with your model... -> The function 'hazard-calculator' does not exist

This code is going through a 'transition' phase as it moves away from free form messages to something more structured and linked to the i18n system. Over time, we should: * purge all the code related to default messages * use annotations to apply structure to the various args, e.g. an 'expected' and 'actual' argument

See Problems for tips on constructing Problems.

  • Field Details

  • Constructor Details

    • Problem

      @Deprecated public Problem(Problem.Severity severity, String message)
      Deprecated.
      use the ProblemCode-based constructor instead
    • Problem

      @Deprecated public Problem(Problem.Severity severity, String message, Object[] args, Throwable t)
      Deprecated.
      use the ProblemCode-based constructor instead
    • Problem

      @Deprecated public Problem(List<Problem> children, Problem.Severity severity, String message, Object[] args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • Problem

      public Problem(Problem.Severity severity, ProblemCode code, Object... args)

      Same as error(ProblemCode, Object...), but where you need to use a different severity, e.g. INFO, WARNING, etc.

    • Problem

      public Problem(List<Problem> children, ProblemCode code, Object... args)
    • Problem

      public Problem(Problem.Severity severity, String defaultMessageFormat, Object[] arguments, Throwable exception, ProblemCode code, nz.org.riskscape.problem.Problem.AffectedMetadata affected, Problems children)

      Creates a new Problem instance.

      Parameters:
      severity -
      defaultMessageFormat - Stores the unformatted message string that will be used as the default message (i.e. if the i18n lookup fails to find a better message). Note that this is in String format (rather than MessageFormat format).
      arguments -
      exception -
      code -
      affected - Basic metadata about what this specific problem affects, e.g. is it a problem with a Bookmark, or Parameter, or Expression, etc.
      children -
  • Method Details

    • wrapAffectedObject

      public static nz.org.riskscape.problem.Problem.AffectedMetadata wrapAffectedObject(Object thing)
    • info

      @Deprecated public static Problem info(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • warning

      @Deprecated public static Problem warning(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • warning

      @Deprecated public static Problem warning(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • error

      @Deprecated public static Problem error(String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • error

      @Deprecated public static Problem error(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • fatal

      @Deprecated public static Problem fatal(Throwable t, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead
    • composite

      @Deprecated public static Problem composite(List<Problem> children, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead

      Builds a Problem that contains the child Problems.

      The level of the the returned Problem is the most severe from those of the children.

      Parameters:
      children - the problems the new Problem should wrap
    • composite

      @Deprecated public static Problem composite(Problem.Severity severity, List<Problem> children, String message, Object... args)
      Deprecated.
      use the ProblemCode-based constructor instead

      Builds a Problem that contains the child Problems, forcing a particular severity for the created parent.

      Parameters:
      children - the problems the new Problem should wrap
    • error

      public static Problem error(ProblemCode code, Object... args)

      Create an problem with Problem.Severity.ERROR with a particular ProblemCode and message arguments.

    • max

      public static Problem.Severity max(Collection<? extends Problem> children)
    • hasErrors

      public static boolean hasErrors(Collection<? extends Problem> problems)
    • throwIfErrors

      public static <T extends Throwable> void throwIfErrors(List<Problem> problems, Function<List<Problem>,T> supplier) throws T
      Throws:
      T extends Throwable
    • withChildren

      public Problem withChildren(Problems... moreChildren)
    • withChildren

      public Problem withChildren(List<? extends Problem> moreChildren)

      Copy this Problem but add in more children.

    • setChildren

      public Problem setChildren(List<Problem> newChildren)

      Copy this Problem but replace the children.

    • withSeverity

      public Problem withSeverity(Problem.Severity sev)

      Copy this Problem but with a new severity

    • withException

      public Problem withException(Throwable t)

      Copy this Problem but with a new exception

    • affecting

      public Problem affecting(Class<?> clazz, String name)

      Copy this Problem but add details of the specific thing that the problem affects. E.g. the specific Parameter or pipeline Step affected, etc. Note that there are several variants as in some cases we may know the class and name but not have an object, whereas other cases may have an object but no name.

    • affecting

      public Problem affecting(Object thing)
    • isError

      public boolean isError()
    • isFatal

      public boolean isFatal()
    • hasChildren

      public boolean hasChildren()
    • getMessage

      public String getMessage()
    • getDefaultMessage

      public String getDefaultMessage()
    • getAffectedName

      public String getAffectedName()
      Returns:
      the name of the thing affected by this Problem (or an empty String if there is no such context associated with it)
    • getAffectedObject

      public Optional<Object> getAffectedObject()
      Returns:
      the thing affected by this Problem, e.g. the Parameter object. Returns Optional.empty() if there is no specific object affected.
    • getAffectedClass

      public Class<?> getAffectedClass()
      Returns:
      the class of thing affected by this Problem, e.g. Parameter.class. Returns Object.class if this is unknown.
    • affects

      public boolean affects(Class<?> superclass)
      Returns:
      true if the Problem affects the given class or superclass
    • getAffected

      public <T> Optional<T> getAffected(Class<T> classOfThing)

      With Problems, there is usually something this is most affected by the Problem, e.g. a Parameter or a File or a Bookmark. This checks if this Problem affects the class given, and if so, returns that thing. E.g. 'I know this Problem affects a File, gimme the File'.

    • filterAffected

      public static List<Problem> filterAffected(Collection<? extends Problem> problems, Class<?> contextClass)

      Filters the list of Problems (and their children) based on the affected class, e.g. find any/all problems that affect a Parameter, Step, etc. A flattened list is returned, i.e. it may be a mix of parents and children, regardless of where they occur in the problem hierarchy.

      Returns:
      a list of Problems that affect the given class. Or an empty list, if no Problems affect the given class.
    • debugString

      public static String debugString(Problem origin)
      Returns:
      a string that gives some idea of the structure of this problem and its children, but without requiring any i18n resources to give actual user-facing error messages
    • debugString

      public static String debugString(List<Problem> problems)

      Variation of debugString(Problem) that dumps out a list of problems

    • getChildren

      public List<Problem> getChildren()
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • findAnyException

      public Throwable findAnyException()

      Finds any exception info related to this problem, including exceptions that may have been caught by child/nested Problems

    • iterator

      public Iterator<Problem> iterator()
      Specified by:
      iterator in interface Iterable<Problem>
    • toProblem

      public Optional<Problem> toProblem()
      Description copied from interface: Problems

      Convert this Problems in to a single problem, or empty if this is Problems.NONE

      Specified by:
      toProblem in interface Problems
    • stream

      public Stream<Problem> stream()
      Specified by:
      stream in interface Problems
      Returns:
      a stream over any individual Problems
    • toList

      public List<Problem> toList()
      Description copied from interface: Problems

      Convert this Problems in to a (possibly empty) list of Problems

      Specified by:
      toList in interface Problems
      Returns:
    • isEmpty

      public boolean isEmpty()
      Specified by:
      isEmpty in interface Problems
      Returns:
      true if this Problems is NONE
    • isPresent

      public boolean isPresent()
      Specified by:
      isPresent in interface Problems
      Returns:
      true if this Problems will yield one or more Problems
    • equals

      public boolean equals(Object o)
      Overrides:
      equals in class Object
    • canEqual

      protected boolean canEqual(Object other)
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getSeverity

      public Problem.Severity getSeverity()
      Specified by:
      getSeverity in interface Problems
      Returns:
      the maximum severity of any Problems, or INFO if empty.
    • getArguments

      public Object[] getArguments()
    • getException

      public Throwable getException()
    • getCode

      public ProblemCode getCode()