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

info.unterrainer.commons.jreutils.collections.DataTable Maven / Gradle / Ivy

There is a newer version: 0.3.15
Show newest version
package info.unterrainer.commons.jreutils.collections;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;
import java.util.function.Function;

import lombok.Getter;
import lombok.experimental.Accessors;

/**
 * A synchronized data-structure acting as a table. You may create indexes for
 * various columns of this table.
 */
@Accessors(fluent = true)
public class DataTable {

	private Class clazz;
	private int maxEntries;
	@Getter
	private DataQueue queue;

	private HashMap> keySuppliers = new HashMap<>();
	private HashMap> maps = new HashMap<>();

	public DataTable(final Class clazz, final int maxEntries) {
		this.clazz = clazz;
		this.maxEntries = maxEntries;
		queue = new DataQueue<>(maxEntries);
	}

	@SuppressWarnings("unchecked")
	public  DataTable addIndex(final String name, final Function keySupplier) {
		keySuppliers.put(name, (Function) keySupplier);
		maps.put(name, new DataMap(maxEntries));
		return this;
	}

	public synchronized T peek() {
		return queue.peek();
	}

	public synchronized T poll() {
		T e = queue.poll();
		for (String name : keySuppliers.keySet()) {
			Function func = keySuppliers.get(name);
			DataMap map = maps.get(name);
			map.remove(func.apply(e));
		}
		return e;
	}

	public synchronized  T get(final String name, final K key) {
		return maps.get(name).get(key);
	}

	public synchronized void add(final T element) {
		queue.offer(element);
		for (String name : keySuppliers.keySet()) {
			Function func = keySuppliers.get(name);
			DataMap map = maps.get(name);
			Object key = func.apply(element);
			map.put(key, element);
		}
	}

	@SuppressWarnings("unchecked")
	public synchronized  Set keySet(final String name) {
		return (Set) maps.get(name).keySet();
	}

	public synchronized Collection values(final String name) {
		return maps.get(name).values();
	}

	public synchronized boolean containsValue(final String name, final T value) {
		return maps.get(name).containsValue(value);
	}

	public synchronized  boolean containsKey(final String name, final K value) {
		return maps.get(name).containsKey(value);
	}

	public synchronized void clear() {
		queue.clear();
		for (String name : keySuppliers.keySet())
			maps.get(name).clear();
	}

	@SuppressWarnings("unchecked")
	public synchronized DataTable load(T[] backingArray) {
		if (backingArray == null)
			backingArray = (T[]) Array.newInstance(clazz, 0);

		queue = new DataQueue<>(maxEntries, backingArray);

		for (String name : keySuppliers.keySet()) {
			DataMap map = new DataMap<>(maxEntries);
			maps.put(name, map);
			Function func = keySuppliers.get(name);
			for (T s : backingArray)
				map.put(func.apply(s), s);
		}
		return this;
	}

	@SuppressWarnings("unchecked")
	public synchronized T[] toArray() {
		T[] zeroArray = (T[]) Array.newInstance(clazz, 0);
		return queue.getListClone().toArray(zeroArray);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy