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

com.opencredo.concourse.data.tuples.TupleKey Maven / Gradle / Ivy

The newest version!
package com.opencredo.concourse.data.tuples;

import com.google.common.annotations.VisibleForTesting;

import java.util.Objects;

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

/**
 * A key that can be used to retrieve a value directly from a tuple, in a type-safe way.
 * @param 
 */
public class TupleKey {

    private final TupleSchema schema;
    private final String name;
    private final int index;

    TupleKey(TupleSchema schema, String name, int index) {
        this.schema = schema;
        this.name = name;
        this.index = index;
    }

    @SuppressWarnings("unchecked")
    T get(Object[] values) {
        return (T) values[index];
    }

    void set(Object[] values, Object value) {
        values[index] = value;
    }

    boolean belongsToSchema(TupleSchema schema) {
        return this.schema.equals(schema);
    }

    /**
     * Create a tuple builder that will place the supplied value in the slot referenced by this key.
     * @param value The value to add to the tuple.
     * @return The tuple builder.
     */
    public TupleKeyValue of(T value) {
        checkNotNull(value, "value must not be null");
        return new TupleKeyValue(this, value);
    }

    @VisibleForTesting
    public String getName() {
        return name;
    }

    @Override
    public boolean equals(Object o) {
        return this == o
                || (o instanceof TupleKey
                    && ((TupleKey) o).schema.equals(schema)
                    && ((TupleKey) o).index == index);
    }

    @Override
    public int hashCode() {
        return Objects.hash(schema, index);
    }

    @Override
    public String toString() {
        return "TupleKey<" + name + ">";
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy