All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.liveramp.commons.Accessors Maven / Gradle / Ivy

package com.liveramp.commons;

import java.util.Iterator;
import java.util.List;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;

public class Accessors {

  /**
   * Use {@link #firstOrEmpty(Iterable)}
   */
  @Deprecated
  public static  Optional firstOrAbsent(Iterable c) {
    Preconditions.checkNotNull(c, "Null iterable");

    Iterator iter = c.iterator();

    if (!iter.hasNext()) {
      return Optional.absent();
    } else {
      return Optional.of(first(c));
    }
  }

  public static  java.util.Optional firstOrEmpty(Iterable c) {
    return java.util.Optional.ofNullable(firstOrAbsent(c).orNull());
  }

  public static  T first(Iterable c) {
    Preconditions.checkNotNull(c, "Null iterable");

    Iterator iter = c.iterator();
    Preconditions.checkArgument(iter.hasNext(), "Empty collection");

    return iter.next();
  }

  public static  T second(Iterable c) {
    Preconditions.checkNotNull(c, "Null iterable");

    Iterator iter = c.iterator();
    Preconditions.checkArgument(iter.hasNext(), "Empty iterable");

    iter.next();
    Preconditions.checkArgument(iter.hasNext(), "Not enough elements");

    return iter.next();
  }

  public static  T only(Iterable c) {
    Preconditions.checkNotNull(c, "Null iterable");

    Iterator iterator = c.iterator();
    Preconditions.checkArgument(iterator.hasNext(), "Empty iterable");

    T val = iterator.next();

    if (iterator.hasNext()) {
      throw new RuntimeException(
          String.format(
              "Iterable has more than one element. First element: %s, Second element: %s, Has third element: %s",
              val,
              iterator.next(),
              iterator.hasNext()
          )
      );
    }

    return val;
  }

  public static  T only(T[] ts) {
    Preconditions.checkNotNull(ts, "Array is null");
    Preconditions.checkArgument(ts.length == 1, String.format("Array has %d elements instead of just 1", ts.length));

    return ts[0];
  }

  public static  Optional onlyOrAbsent(Iterable c) {
    Preconditions.checkNotNull(c, "Null iterable");

    Iterator iterator = c.iterator();

    if (!iterator.hasNext()) {
      return Optional.absent();
    }

    T val = iterator.next();

    if (iterator.hasNext()) {
      throw new IllegalArgumentException(
          String.format(
              "Iterable has more than one element. First element: %s, Second element: %s, Has third element: %s",
              val,
              iterator.next(),
              iterator.hasNext()
          )
      );
    }

    return Optional.of(val);
  }

  public static  java.util.Optional onlyOrEmpty(Iterable c) {
    final Optional maybeOnly = onlyOrAbsent(c);
    if (maybeOnly.isPresent()) {
      return java.util.Optional.of(maybeOnly.get());
    } else {
      return java.util.Optional.empty();
    }
  }

  public static  T last(List c) {
    Preconditions.checkNotNull(c, "Null collection");
    Preconditions.checkArgument(!c.isEmpty(), "Empty collection");
    return c.get(c.size() - 1);
  }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy