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

io.permazen.ConvertedIndex Maven / Gradle / Ivy

There is a newer version: 5.1.0
Show 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.Index;
import io.permazen.tuple.Tuple2;
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 Index}es.
 *
 * @param  value type of this index
 * @param  target type of this index
 * @param  value type of wrapped index
 * @param  target type of wrapped index
 */
class ConvertedIndex implements Index {

    private final Index index;
    private final Converter valueConverter;
    private final Converter targetConverter;

    ConvertedIndex(Index index, Converter valueConverter, Converter targetConverter) {
        Preconditions.checkArgument(index != null, "null index");
        Preconditions.checkArgument(valueConverter != null, "null valueConverter");
        Preconditions.checkArgument(targetConverter != null, "null targetConverter");
        this.index = index;
        this.valueConverter = valueConverter;
        this.targetConverter = targetConverter;
    }

    @Override
    public NavigableSet> asSet() {
        return new ConvertedNavigableSet, Tuple2>(this.index.asSet(),
          new Tuple2Converter(this.valueConverter, this.targetConverter));
    }

    @Override
    public NavigableMap> asMap() {
        return new ConvertedNavigableMap, WV, NavigableSet>(this.index.asMap(),
          this.valueConverter, new NavigableSetConverter(this.targetConverter));
    }

    @Override
    public Index withValueBounds(Bounds bounds) {
        return this.convert(this.index.withValueBounds(ConvertedIndex.convert(bounds, this.valueConverter)));
    }

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

    private Index convert(Index boundedIndex) {
        return boundedIndex == this.index ? this :
          new ConvertedIndex(boundedIndex, this.valueConverter, this.targetConverter);
    }

    static  Bounds convert(Bounds bounds, Converter converter) {
        final T2 lowerBound = bounds.hasLowerBound() ? converter.convert(bounds.getLowerBound()) : null;
        final T2 upperBound = bounds.hasUpperBound() ? converter.convert(bounds.getUpperBound()) : null;
        return new Bounds(lowerBound, bounds.getLowerBoundType(), upperBound, bounds.getUpperBoundType());
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy