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

org.jsimpledb.Tuple2Converter Maven / Gradle / Ivy

The newest version!

/*
 * Copyright (C) 2015 Archie L. Cobbs. All rights reserved.
 */

package org.jsimpledb;

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

import org.jsimpledb.tuple.Tuple2;

/**
 * Converts {@link Tuple2}s.
 *
 * @param  first converted value
 * @param  second converted value
 * @param  first wrapped value
 * @param  second wrapped value
 */
class Tuple2Converter extends Converter, Tuple2> {

    private final Converter value1Converter;
    private final Converter value2Converter;

    Tuple2Converter(Converter value1Converter, Converter value2Converter) {
        Preconditions.checkArgument(value1Converter != null, "null value1Converter");
        Preconditions.checkArgument(value2Converter != null, "null value2Converter");
        this.value1Converter = value1Converter;
        this.value2Converter = value2Converter;
    }

    @Override
    protected Tuple2 doForward(Tuple2 tuple) {
        if (tuple == null)
            return null;
        return new Tuple2(
          this.value1Converter.convert(tuple.getValue1()),
          this.value2Converter.convert(tuple.getValue2()));
    }

    @Override
    protected Tuple2 doBackward(Tuple2 tuple) {
        if (tuple == null)
            return null;
        return new Tuple2(
          this.value1Converter.reverse().convert(tuple.getValue1()),
          this.value2Converter.reverse().convert(tuple.getValue2()));
    }

// Object

    @Override
    public boolean equals(Object obj) {
        if (obj == this)
            return true;
        if (obj == null || obj.getClass() != this.getClass())
            return false;
        final Tuple2Converter that = (Tuple2Converter)obj;
        return this.value1Converter.equals(that.value1Converter)
          && this.value2Converter.equals(that.value2Converter);
    }

    @Override
    public int hashCode() {
        return this.value1Converter.hashCode() ^ this.value2Converter.hashCode();
    }

    @Override
    public String toString() {
        return this.getClass().getSimpleName()
          + "[value1Converter=" + this.value1Converter
          + ",value2Converter=" + this.value2Converter
          + "]";
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy