Class MoreKit

java.lang.Object
com.iohao.net.common.kit.MoreKit

public class MoreKit extends Object
General-purpose utility methods.
Author:
渔民小镇
date:
2023-12-07
  • Constructor Summary

    Constructors
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    execute(Executor executor, Runnable runnable)
    Execute the runnable on the given executor, or run it on the current thread if the executor is null.
    <T> T
    firstNonNull(T first, T second)
    Return the first non-null argument, or throw if both are null.
    <K,T> T
    putIfAbsent(Map<K,T> map, K key, T value)
    Put the value into the map if the key is absent, and return the effective value.

    Methods inherited from class Object

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

    • MoreKit

      public MoreKit()
  • Method Details

    • firstNonNull

      public <T> T firstNonNull(T first, T second)
      Return the first non-null argument, or throw if both are null.
      Type Parameters:
      T - the value type
      Parameters:
      first - the first candidate
      second - the second candidate
      Returns:
      the first non-null value
      Throws:
      NullPointerException - if both arguments are null
    • putIfAbsent

      public <K,T> T putIfAbsent(Map<K,T> map, K key, T value)
      Put the value into the map if the key is absent, and return the effective value.

      If the key already exists, the existing value is returned; otherwise the newly inserted value is returned.

      Type Parameters:
      K - the key type
      T - the value type
      Parameters:
      map - the target map
      key - the key to check
      value - the value to insert if absent
      Returns:
      the existing value for the key, or value if newly inserted
    • execute

      public void execute(Executor executor, Runnable runnable)
      Execute the runnable on the given executor, or run it on the current thread if the executor is null.
      Parameters:
      executor - the executor to use, or null for direct execution
      runnable - the task to execute