/*
 * Copyright (c) 2004-2017, by Alex Leigh.
 * All rights reserved.
 *
 * THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE
 * The copyright notice above does not evidence any
 * actual or intended publication of such source code.
 */

package lc.mecha.util;

import java.util.HashMap;

public class SequentialDictionary<V> {
    private final HashMap<V, String> keys = new HashMap<>();
    private final HashMap<String, V> values = new HashMap<>();

    private final String prefix;

    public SequentialDictionary(final String prefix) {
        this.prefix = prefix;
    }

    public V getValue(final String key) {
        return values.get(key);
    }

    /**
     * MT-safe: unsafe
     *
     * @param value
     * @return
     */
    public String getKey(final V value) {
        String key = keys.get(value);
        if (key == null) {
            key = prefix + Integer.toString(keys.size());
            keys.put(value, key);
            values.put(key, value);
        }
        return key;
    }

    @Override
    public String toString() {
        return "SequentialDictionary{" +
                "keys=" + keys +
                ", prefix='" + prefix + '\'' +
                '}';
    }
}
