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

com.scalar.db.common.AbstractResult Maven / Gradle / Ivy

Go to download

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

The newest version!
package com.scalar.db.common;

import com.google.common.base.MoreObjects;
import com.google.common.base.MoreObjects.ToStringHelper;
import com.google.common.base.Suppliers;
import com.google.common.collect.ImmutableMap;
import com.scalar.db.api.Result;
import com.scalar.db.common.error.CoreError;
import com.scalar.db.io.Value;
import com.scalar.db.util.ScalarDbUtils;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.function.Supplier;
import java.util.stream.Collectors;

public abstract class AbstractResult implements Result {

  private final Supplier>> valuesWithDefaultValues;
  private final Supplier hashCode;

  public AbstractResult() {
    valuesWithDefaultValues =
        Suppliers.memoize(
            () ->
                ImmutableMap.copyOf(
                    getColumns().entrySet().stream()
                        .collect(
                            Collectors.toMap(
                                Entry::getKey, e -> ScalarDbUtils.toValue(e.getValue())))));

    hashCode =
        Suppliers.memoize(
            () -> {
              List containedColumnNames = new ArrayList<>(getContainedColumnNames());
              Collections.sort(containedColumnNames);
              Object[] values = new Object[containedColumnNames.size()];
              for (int i = 0; i < containedColumnNames.size(); i++) {
                values[i] = getAsObject(containedColumnNames.get(i));
              }
              return Objects.hash(values);
            });
  }

  protected void checkIfExists(String name) {
    if (!contains(name)) {
      throw new IllegalArgumentException(CoreError.COLUMN_NOT_FOUND.buildMessage(name));
    }
  }

  /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */
  @Deprecated
  @Override
  public Optional> getValue(String columnName) {
    return Optional.ofNullable(valuesWithDefaultValues.get().get(columnName));
  }

  /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */
  @Deprecated
  @Override
  public Map> getValues() {
    return valuesWithDefaultValues.get();
  }

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

  @Override
  public boolean equals(Object o) {
    if (o == this) {
      return true;
    }
    if (!(o instanceof Result)) {
      return false;
    }

    Result other = (Result) o;
    if (!getContainedColumnNames().equals(other.getContainedColumnNames())) {
      return false;
    }
    for (String containedColumnName : getContainedColumnNames()) {
      Object value = getAsObject(containedColumnName);
      Object otherValue = other.getAsObject(containedColumnName);
      if (value == null && otherValue == null) {
        continue;
      }
      if (value == null || otherValue == null) {
        return false;
      }
      if (!value.equals(otherValue)) {
        return false;
      }
    }
    return true;
  }

  @Override
  public String toString() {
    ToStringHelper toStringHelper = MoreObjects.toStringHelper(this);
    getContainedColumnNames().forEach(c -> toStringHelper.add(c, getAsObject(c)));
    return toStringHelper.toString();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy