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

io.permazen.ConvertedIndex2 Maven / Gradle / Ivy

The newest version!

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

package io.permazen;

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

import io.permazen.index.Index1;
import io.permazen.index.Index2;
import io.permazen.tuple.Tuple2;
import io.permazen.tuple.Tuple3;
import io.permazen.util.Bounds;
import io.permazen.util.ConvertedNavigableMap;
import io.permazen.util.ConvertedNavigableSet;

import java.util.NavigableMap;
import java.util.NavigableSet;

/**
 * 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> asMapOfIndex1() {
        return new ConvertedNavigableMap, WV1, Index1>(this.index.asMapOfIndex1(),
          this.value1Converter, new Index1Converter(this.value2Converter, this.targetConverter));
    }

    @Override
    public Index1 asIndex1() {
        return new ConvertedIndex1<>(this.index.asIndex1(), this.value1Converter, this.value2Converter);
    }

    @Override
    public Index2 withValue1Bounds(Bounds bounds) {
        return this.convert(this.index.withValue1Bounds(ConvertedIndex1.convert(bounds, this.value1Converter)));
    }

    @Override
    public Index2 withValue2Bounds(Bounds bounds) {
        return this.convert(this.index.withValue2Bounds(ConvertedIndex1.convert(bounds, this.value2Converter)));
    }

    @Override
    public Index2 withTargetBounds(Bounds bounds) {
        return this.convert(this.index.withTargetBounds(ConvertedIndex1.convert(bounds, this.targetConverter)));
    }

    private Index2 convert(Index2 boundedIndex) {
        return boundedIndex == this.index ? this :
          new ConvertedIndex2(boundedIndex,
           this.value1Converter, this.value2Converter, this.targetConverter);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy