Interface Or<G,​B>

  • All Known Implementing Classes:
    Or.Bad, Or.Good

    public interface Or<G,​B>
    `Or` represents the presence of a successful outcome, or an error. Contrast this with Option which represents the presence or absence of a value. Option.Some and Or.Good are just about identical. Unlike Option.None, Bad contains an error code or value. Bill Venners, Scalactic, SuperSafe, and Functional Error Handling talk at SF Scala 2015-02-24 convinced me that Or is friendlier than Either. This class is based on Bill Venners' Or. I did not make Every, One, and Many subclasses, figuring that you can make an Or<GoodType,ImList<BadType>> if you expect that. Bill makes the point that there are still some reasons to throw exceptions, but he says to "Throw exceptions at developers, not at code" meaning that if there's code in your program that can recover from the issue, use a functional return type (like Or). Throw exceptions for things a program can't handle without developer intervention. Any errors are my own. This implementation is more like a sealed trait (in Kotlin or Scala) than a simple OneOf2 union type. This makes it a little less general, and more meaningful to use.
    • Nested Class Summary

      Nested Classes 
      Modifier and Type Interface Description
      static class  Or.Bad<G,​B>
      Represents the presence of a Bad value (and absence of a Good).
      static class  Or.Good<G,​B>
      Represents the presence of a Good value (and absence of a Bad).
    • Method Summary

      All Methods Static Methods Instance Methods Abstract Methods 
      Modifier and Type Method Description
      B bad()
      Returns the bad value if this is a Bad, or throws an exception if this is a Good.
      static <G,​B>
      @NotNull Or<G,​B>
      bad​(B bad)
      Construct a new Bad from the given object.
      G good()
      Returns the good value if this is a Good, or throws an exception if this is a Bad.
      static <G,​B>
      @NotNull Or<G,​B>
      good​(G good)
      Construct a new Good from the given object.
      boolean isBad()
      Returns true if this Or has a bad value.
      boolean isGood()
      Returns true if this Or has a good value.
      <R> R match​(@NotNull Fn1<G,​R> fg, @NotNull Fn1<B,​R> fb)
      Exactly one of these functions will be executed - determined by whether this is a Good or a Bad.
    • Method Detail

      • good

        @NotNull
        static <G,​B> @NotNull Or<G,​B> good​(G good)
        Construct a new Good from the given object.
      • bad

        @NotNull
        static <G,​B> @NotNull Or<G,​B> bad​(B bad)
        Construct a new Bad from the given object.
      • isGood

        boolean isGood()
        Returns true if this Or has a good value.
      • isBad

        boolean isBad()
        Returns true if this Or has a bad value.
      • good

        G good()
        Returns the good value if this is a Good, or throws an exception if this is a Bad.
      • bad

        B bad()
        Returns the bad value if this is a Bad, or throws an exception if this is a Good.
      • match

        <R> R match​(@NotNull
                    @NotNull Fn1<G,​R> fg,
                    @NotNull
                    @NotNull Fn1<B,​R> fb)
        Exactly one of these functions will be executed - determined by whether this is a Good or a Bad.
        Parameters:
        fg - the function to be executed if this OneOf stores the first type.
        fb - the function to be executed if this OneOf stores the second type.
        Returns:
        the return value of whichever function is executed.