package com.red.circle.order.domain.tuple;

import lombok.Getter;
import lombok.NoArgsConstructor;

import java.io.Serializable;

/**
 * 键值对结构.
 *
 * @author pengliang
 * @since 2019/10/30 15:11
 */
@Getter
@NoArgsConstructor
public final class KeyValuePair<K, V> implements Serializable {

  private static final long serialVersionUID = 1L;

  private K key;
  private V value;

  private KeyValuePair(K key, V value) {
    this.key = key;
    this.value = value;
  }

  public static <K, V> KeyValuePair<K, V> of(K k, V v) {
    return new KeyValuePair<>(k, v);
  }

  public static KeyValuePair<String, String> splitFirstKeyVal(String sourceStr, String regex) {
    if (sourceStr == null || sourceStr.isBlank()) {
      return of("", "");
    }

    if (regex == null || regex.isBlank()) {
      throw new IllegalArgumentException("regex isBlank.");
    }

    String[] sources = sourceStr.split(regex);

    if (sources.length == 1) {
      return of(sources[0], null);
    }

    if (sources.length < 2) {
      return of("", "");
    }

    return of(sources[0], sources[1]);
  }
}
