Package generic

Interface Unique


public interface Unique
Some utilities for when singleton collections are expected
  • Method Summary

    Static Methods
    Modifier and Type
    Method
    Description
    static <T> T
    Assert that at most one element is in an iterable and get that element or null
    static <T> T
    assertAtMostOne(T[] arr)
     
    static <T> T
    Assert that exactly one element is in an iterable and get that element
    static <T> T
    Assert that exactly one element is in a stream and get that element
  • Method Details

    • assertAtMostOne

      static <T> T assertAtMostOne(T[] arr)
    • assertOne

      static <T> T assertOne(Iterable<T> col)
      Assert that exactly one element is in an iterable and get that element
      Type Parameters:
      T - the type of element
      Parameters:
      col - the iterable
      Returns:
      the element
      Throws:
      AssertionError - if no element or many elements exist in the iterable
    • assertOne

      static <T> T assertOne(Stream<T> st)
      Assert that exactly one element is in a stream and get that element
      Type Parameters:
      T - the type of element
      Parameters:
      st - the stream
      Returns:
      the element
      Throws:
      AssertionError - if no element or many elements exist in the stream
    • assertAtMostOne

      static <T> T assertAtMostOne(Iterable<T> col)
      Assert that at most one element is in an iterable and get that element or null
      Type Parameters:
      T - the type of element
      Parameters:
      col - the iterable
      Returns:
      the element or null if empty
      Throws:
      AssertionError - if many elements exist in the iterable