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

io.permazen.util.ConvertedMapEntry 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.util;

import com.google.common.base.Converter;

import java.util.AbstractMap;
import java.util.Map;

/**
 * Converted {@link Map.Entry}.
 *
 * @param  key type of this entry
 * @param  value type of this entry
 * @param  key type of wrapped entry
 * @param  value type of wrapped entry
 */
@SuppressWarnings("serial")
class ConvertedMapEntry extends AbstractMap.SimpleEntry {

    private final Converter keyConverter;
    private final Converter valueConverter;
    private final Map.Entry wentry;

    ConvertedMapEntry(Converter keyConverter, Converter valueConverter, Map.Entry wentry) {
        super(wentry.getKey() != null ? keyConverter.reverse().convert(wentry.getKey()) : null,
          wentry.getValue() != null ? valueConverter.reverse().convert(wentry.getValue()) : null);
        this.keyConverter = keyConverter;
        this.valueConverter = valueConverter;
        this.wentry = wentry;
    }

    public Converter getKeyConverter() {
        return this.keyConverter;
    }

    public Converter getValueConverter() {
        return this.valueConverter;
    }

    @Override
    public K getKey() {
        final WK key = this.wentry.getKey();
        return key != null ? this.keyConverter.reverse().convert(key) : null;
    }

    @Override
    public V getValue() {
        final WV value = this.wentry.getValue();
        return value != null ? this.valueConverter.reverse().convert(value) : null;
    }

    @Override
    public V setValue(V value) {
        final WV prev = this.wentry.setValue(value != null ? this.valueConverter.convert(value) : null);
        return prev != null ? this.valueConverter.reverse().convert(prev) : null;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy