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

com.scalar.db.storage.cosmos.ValueBinder 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
Show newest version
package com.scalar.db.storage.cosmos;

import com.scalar.db.io.BigIntColumn;
import com.scalar.db.io.BlobColumn;
import com.scalar.db.io.BooleanColumn;
import com.scalar.db.io.ColumnVisitor;
import com.scalar.db.io.DoubleColumn;
import com.scalar.db.io.FloatColumn;
import com.scalar.db.io.IntColumn;
import com.scalar.db.io.TextColumn;
import java.util.Base64;
import java.util.function.Consumer;
import javax.annotation.concurrent.NotThreadSafe;

/**
 * A visitor class to bind {@code Value}s to a {@link StringBuilder}.
 *
 * @author Yuji Ito
 */
@NotThreadSafe
public final class ValueBinder implements ColumnVisitor {
  private Consumer consumer;

  public ValueBinder() {}

  public void set(Consumer consumer) {
    this.consumer = consumer;
  }

  @Override
  public void visit(BooleanColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(IntColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(BigIntColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(FloatColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(DoubleColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(TextColumn column) {
    consumer.accept(column.getValueAsObject());
  }

  @Override
  public void visit(BlobColumn column) {
    if (column.hasNullValue()) {
      consumer.accept(null);
    } else {
      consumer.accept(Base64.getEncoder().encodeToString(column.getBlobValueAsBytes()));
    }
  }
}