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

io.polaris.core.map.CaseInsensitiveMap Maven / Gradle / Ivy

There is a newer version: 3.2.1
Show newest version
package io.polaris.core.map;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;

/**
 * @author Qt
 * @since 1.8
 */
@SuppressWarnings({"unchecked", "rawtypes"})
public class CaseInsensitiveMap extends TransformMap {
	private static final Function TRANSFORMER_UPPER_CASE = key -> (key instanceof CharSequence ? key.toString().toUpperCase() : key);
	private static final Function TRANSFORMER_LOWER_CASE = key -> (key instanceof CharSequence ? key.toString().toLowerCase() : key);

	public CaseInsensitiveMap(Supplier> supplier) {
		super(supplier, TRANSFORMER_UPPER_CASE);
	}

	public CaseInsensitiveMap() {
		super(new LinkedHashMap<>(), TRANSFORMER_UPPER_CASE);
	}

	public CaseInsensitiveMap(Supplier> supplier, boolean upperCase) {
		super(supplier, upperCase ? TRANSFORMER_UPPER_CASE : TRANSFORMER_LOWER_CASE);
	}


	public CaseInsensitiveMap(boolean upperCase) {
		super(new LinkedHashMap<>(), upperCase ? TRANSFORMER_UPPER_CASE : TRANSFORMER_LOWER_CASE);
	}

	public CaseInsensitiveMap(Supplier> supplier, Map raw) {
		this(supplier);
		addRawData(raw);
	}

	public CaseInsensitiveMap(Map raw) {
		this();
		addRawData(raw);
	}

	public CaseInsensitiveMap(Supplier> supplier, Map raw, boolean upperCase) {
		this(supplier, upperCase);
		addRawData(raw);
	}

	public CaseInsensitiveMap(Map raw, boolean upperCase) {
		this(upperCase);
		addRawData(raw);
	}

	private void addRawData(Map raw) {
		for (Entry entry : raw.entrySet()) {
			putIfAbsent(entry.getKey(), entry.getValue());
		}
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy