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

org.jsimpledb.ConvertedIndex2 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 java.util.NavigableMap;
import java.util.NavigableSet;

import org.jsimpledb.index.Index;
import org.jsimpledb.index.Index2;
import org.jsimpledb.tuple.Tuple2;
import org.jsimpledb.tuple.Tuple3;
import org.jsimpledb.util.ConvertedNavigableMap;
import org.jsimpledb.util.ConvertedNavigableSet;

/**
 * Converter for {@link Index2}s.
 *
 * @param  first value type of this index
 * @param  second value type of this index
 * @param  target type of this index
 * @param  first value type of wrapped index
 * @param  second value type of wrapped index
 * @param  target type of wrapped index
 */
class ConvertedIndex2 implements Index2 {

    private final Index2 index;
    private final Converter value1Converter;
    private final Converter value2Converter;
    private final Converter targetConverter;

    ConvertedIndex2(Index2 index,
      Converter value1Converter, Converter value2Converter, Converter targetConverter) {
        Preconditions.checkArgument(index != null, "null index");
        Preconditions.checkArgument(value1Converter != null, "null value1Converter");
        Preconditions.checkArgument(value2Converter != null, "null value2Converter");
        Preconditions.checkArgument(targetConverter != null, "null targetConverter");
        this.index = index;
        this.value1Converter = value1Converter;
        this.value2Converter = value2Converter;
        this.targetConverter = targetConverter;
    }

    @Override
    public NavigableSet> asSet() {
        return new ConvertedNavigableSet, Tuple3>(this.index.asSet(),
          new Tuple3Converter(this.value1Converter, this.value2Converter, this.targetConverter));
    }

    @Override
    public NavigableMap, NavigableSet> asMap() {
        return new ConvertedNavigableMap, NavigableSet, Tuple2, NavigableSet>(this.index.asMap(),
          new Tuple2Converter(this.value1Converter, this.value2Converter),
          new NavigableSetConverter(this.targetConverter));
    }

    @Override
    public NavigableMap> asMapOfIndex() {
        return new ConvertedNavigableMap, WV1, Index>(this.index.asMapOfIndex(),
          this.value1Converter, new IndexConverter(this.value2Converter, this.targetConverter));
    }

    @Override
    public Index asIndex() {
        return new ConvertedIndex<>(this.index.asIndex(), this.value1Converter, this.value2Converter);
    }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy