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

org.openstreetmap.atlas.utilities.compression.LongDictionary Maven / Gradle / Ivy

There is a newer version: 7.0.8
Show newest version
package org.openstreetmap.atlas.utilities.compression;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * Simple dictionary encoding for {@link String}s
 *
 * @author matthieun
 * @param 
 *            The type to encode. Typically called word
 */
public class LongDictionary implements Serializable
{
    private static final long serialVersionUID = 6060400113166584385L;
    private final Map wordToIndex;
    private final Map indexToWord;

    private long index = 0;

    public LongDictionary()
    {
        this.wordToIndex = new HashMap<>();
        this.indexToWord = new HashMap<>();
    }

    public synchronized long add(final Type word)
    {
        if (this.wordToIndex.containsKey(word))
        {
            return this.wordToIndex.get(word);
        }
        this.wordToIndex.put(word, this.index);
        this.indexToWord.put(this.index, word);
        return this.index++;
    }

    public Type word(final long index)
    {
        return this.indexToWord.get(index);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy