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

de.rpgframework.genericrpg.data.DataStorage Maven / Gradle / Ivy

package de.rpgframework.genericrpg.data;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author prelle
 *
 */
public class DataStorage implements DataProvider {

	private String type;
	private Map items;

	//-------------------------------------------------------------------
	public DataStorage(String type) {
		this.type = type;
		items = new HashMap();
	}

	//-------------------------------------------------------------------
	/**
	 * @see de.rpgframework.genericrpg.data.DataProvider#getSupportedDataType()
	 */
	@Override
	public String getSupportedDataType() {
		return type;
	}

	//-------------------------------------------------------------------
	/**
	 * @see de.rpgframework.genericrpg.data.DataProvider#getItem(java.lang.String)
	 */
	@Override
	public V getItem(String key) {
		V ret = items.get(key);
		if (ret!=null) return ret;
		for (V tmp : items.values()) {
			if (tmp instanceof IReferenceResolver) {
				ret = ((IReferenceResolver)tmp).resolveItem(key);
				if (ret!=null) return ret;
			}
		}
		return null;
	}

	//-------------------------------------------------------------------
	/**
	 * @see de.rpgframework.genericrpg.data.DataProvider#getItem(java.lang.String)
	 */
	@Override
	public V getItem(String key, String lang) {
		V ret = items.get(key);
		if (ret!=null) {
			V ret2 = (lang!=null)?ret.getLanguageAlternative(lang):null;
			return (ret2!=null) ? ret2 : ret;
		}

		for (V tmp : items.values()) {
			if (tmp instanceof IReferenceResolver) {
				ret = ((IReferenceResolver)tmp).resolveItem(key);
				if (ret!=null) return ret;
			}
		}
		return null;
	}

	//-------------------------------------------------------------------
	/**
	 * @see de.rpgframework.genericrpg.data.DataProvider#getItems()
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List getItems() {
		List all = new ArrayList();
		for (V tmp : items.values()) {
			all.add(tmp);
			all.addAll((Collection) tmp.getLanguageAlternatives());
		}
		return all;
	}

	//-------------------------------------------------------------------
	public List getKeys() {
		return new ArrayList<>(items.keySet());
	}

	//-------------------------------------------------------------------
	public void add(V value) {
		if (value==null)
			throw new NullPointerException();
		if (!value.getTypeString().equals(type))
			throw new IllegalArgumentException("Trying to add a '"+value.getTypeString()+"' to a database of '"+type+"'");

		items.put(value.getId(), value);
	}

	//-------------------------------------------------------------------
	public void add(V value, DataItem parent) {
		if (value==null)
			throw new NullPointerException("Value");
		if (parent==null)
			throw new NullPointerException("parent DataItem");
//		if (!value.getTypeString().equals(type))
//			throw new IllegalArgumentException("Trying to add a '"+value.getTypeString()+"' to a database of '"+type+"'");

		items.put(parent.getId()+"/"+value.getId(), value);
	}

	//-------------------------------------------------------------------
	public V remove(String key) {
		return items.remove(key);
	}

	//-------------------------------------------------------------------
	public void clear() {
		items.clear();
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy