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

com.scalar.database.io.Key Maven / Gradle / Ivy

Go to download

A universal transaction manager that achieves database-agnostic transactions and distributed transactions that span multiple databases

There is a newer version: 3.14.0-alpha.1
Show newest version
package com.scalar.database.io;

import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ComparisonChain;
import com.google.common.collect.Ordering;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable;

/**
 * An abstraction for keys
 *
 * @author Hiroyuki Yamada
 */
@Immutable
public final class Key implements Comparable, Iterable {
  private final List values;

  /**
   * Constructs a {@code Key} with the specified {@link Value}s
   *
   * @param values one or more {@link Value}s which this key is composed of
   */
  public Key(Value... values) {
    checkNotNull(values);
    this.values = new ArrayList<>(values.length);
    Arrays.stream(values).forEach(v -> this.values.add(v));
  }

  /**
   * Constructs a {@code Key} with the specified list of {@link Value}s
   *
   * @param values a list of {@link Value}s which this key is composed of
   */
  public Key(List values) {
    checkNotNull(values);
    this.values = new ArrayList<>(values.size());
    values.forEach(v -> this.values.add(v));
  }

  /**
   * Returns the list of {@code Value} which this key is composed of
   *
   * @return list of {@code Value} which this key is composed of
   */
  @Nonnull
  public List get() {
    return Collections.unmodifiableList(values);
  }

  @Override
  public int hashCode() {
    return values.hashCode();
  }

  /**
   * Indicates whether some other object is "equal to" this object. The other object is considered
   * equal if:
   *
   * 
    *
  • both super class instances are equal and *
  • it is also an {@code Key} and *
  • both instances have the same list of {@code Value}s *
* * @param o an object to be tested for equality * @return {@code true} if the other object is "equal to" this object otherwise {@code false} */ @Override public boolean equals(Object o) { if (o == this) { return true; } if (!(o instanceof Key)) { return false; } Key that = (Key) o; if (values.equals(that.values)) { return true; } return false; } @Override public String toString() { MoreObjects.ToStringHelper helper = MoreObjects.toStringHelper(this); values.forEach(v -> helper.addValue(v)); return helper.toString(); } @Override public Iterator iterator() { return values.iterator(); } @Override public int compareTo(Key o) { return ComparisonChain.start() .compare(values, o.values, Ordering.natural().lexicographical()) .result(); } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy