Class ObjectUtility

java.lang.Object
megamek.codeUtilities.ObjectUtility

public class ObjectUtility extends Object
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    static <T> int
    compareNullable(T a, T b, Comparator<? super T> comparator)
    Compares two nullable values using the provided comparator
    static <T> T
    getRandomItem(Collection<? extends T> collection)
    Get a random element out of a collection, with equal probability.
    static <T> T
    getRandomItem(List<? extends T> list)
    Get a random element out of a list, with equal probability.
    static <T> Optional<T>
     
    static <T> T
    nonNull(T first, T second)
    The method is returns the same as a call to the following code: T result = (getFirst() != null) ? getFirst() : getSecond(); with the major difference that getFirst() and getSecond() get evaluated exactly once.
    static <T> T
    nonNull(T first, T second, T... others)
     

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • ObjectUtility

      public ObjectUtility()
  • Method Details

    • nonNull

      @Nullable public static <T> T nonNull(@Nullable T first, @Nullable T second)
      The method is returns the same as a call to the following code: T result = (getFirst() != null) ? getFirst() : getSecond(); with the major difference that getFirst() and getSecond() get evaluated exactly once.

      This means that it doesn't matter if getFirst() is relatively expensive to evaluate or has side effects. It also means that getSecond() gets evaluated regardless if it is needed or not. Since Java guarantees the order of evaluation for arguments to be the same as the order in which they appear (JSR 15.7.4), this makes it more suitable for re-playable procedural generation and similar method calls with side effects.

      Returns:
      the first argument if it's not null, otherwise the second argument
    • nonNull

      @SafeVarargs public static <T> T nonNull(@Nullable T first, @Nullable T second, T... others)
      Returns:
      the first non-null argument, else null if all are null
      See Also:
    • compareNullable

      public static <T> int compareNullable(@Nullable T a, @Nullable T b, Comparator<? super T> comparator)
      Compares two nullable values using the provided comparator
      Type Parameters:
      T - the specified class
      Parameters:
      a - the first value
      b - the second value
      comparator - the comparator to use to compare provided they are both not null
      Returns:
      the comparison sort value
    • getRandomItem

      @Nullable public static <T> T getRandomItem(@Nullable Collection<? extends T> collection)
      Get a random element out of a collection, with equal probability.

      This is the same as calling the following code, only plays nicely with all collections (including ones like Set which don't implement RandomAccess) and deals gracefully with empty collections.

      collection.get(Compute.randomInt(collection.size());

      Parameters:
      collection - the collect to get a random item from, which may be null
      Returns:
      a random item from the collection. Returns null if the collection itself is null or empty. It can also return null if the collection contains null items.
    • getRandomItem

      @Nullable public static <T> T getRandomItem(@Nullable List<? extends T> list)
      Get a random element out of a list, with equal probability.

      This is the same as calling the following code, only deals gracefully with empty lists.

      list.get(Compute.randomInt(list.size());

      Parameters:
      list - the list to get a random item from, which may be null
      Returns:
      a random item from the list. This will be null if the list itself is null or empty, and can be null if the list contains null items.
    • getRandomItemSafe

      public static <T> Optional<T> getRandomItemSafe(List<T> list)