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

com.softicar.platform.common.container.map.index.IndexHashMap Maven / Gradle / Ivy

Go to download

The SoftiCAR Platform is a lightweight, Java-based library to create interactive business web applications.

There is a newer version: 50.0.0
Show newest version
package com.softicar.platform.common.container.map.index;

import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Set;

/**
 * Implementation of {@link IIndexMap} using {@link HashMap}.
 *
 * @author Oliver Richers
 */
public class IndexHashMap implements IIndexMap {

	private final HashMap map;

	public IndexHashMap() {

		this(Collections.emptyList());
	}

	public IndexHashMap(Collection values) {

		this.map = new HashMap<>();

		int index = 0;
		for (T column: values) {
			map.put(column, index);
			index++;
		}
	}

	@Override
	public int add(T value) {

		var newIndex = map.size();
		var oldIndex = map.putIfAbsent(value, newIndex);
		return oldIndex != null? oldIndex : newIndex;
	}

	@Override
	public Integer getIndex(T value) {

		return map.get(value);
	}

	@Override
	public Set keySet() {

		return Collections.unmodifiableSet(map.keySet());
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy