Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* Copyright (C) 2013 Florian Frankenberger.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package com.moebiusgames.xdata;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
/**
* Contains data that can be stored as xdata file. Note that you can also store
* data nodes as objects within this data node.
*
* @author Florian Frankenberger
*/
public class DataNode {
private final Map data = new LinkedHashMap<>();
/**
* clears the data node
*/
public void clear() {
this.data.clear();
}
/**
* returns the associated key (if it exists). If the key has no associated
* value but the key has defined a default value then the value is returned
* instead. If the key was defined to be not nullable but is null due to its
* value or because it does not exist, then an IllegalStateException is thrown
*
* @param
* @param key
* @return
*/
public T getObject(DataKey key) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
final Object object = data.get(key.getName());
if (containsKey(key)) {
if (object != null) {
if (!key.getDataClass().isAssignableFrom(object.getClass())) {
throw new IllegalStateException("the type of the key (" + key.getDataClass().getCanonicalName() + ") is not a supertype of the value's class ("
+ object.getClass().getCanonicalName() + ")");
}
}
} else {
if (key.getDefaultValue() != null) {
return key.getDefaultValue();
}
}
if (object == null && !key.allowNull()) {
throw new IllegalStateException("key \"" + key.getName() + "\" does not allow null values but the object is null");
}
return (T) object;
}
public T getMandatoryObject(DataKey key) {
if (!containsKey(key)) {
throw new IllegalStateException("no value for key \"" + key.getName() + "\" found, but was mandatory");
}
return getObject(key);
}
public List getObjectList(ListDataKey key) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
final Object object = data.get(key.getName());
if (object == null && !key.allowNull()) {
return new ArrayList<>();
}
return (List) object;
}
public List getMandatoryObjectList(ListDataKey key) {
if (!containsKey(key)) {
throw new IllegalStateException("no value for list key \"" + key.getName() + "\" found, but was mandatory");
}
return getObjectList(key);
}
/**
* stores an object for the given key
*
* @param
* @param key
* @param object
*/
public void setObject(DataKey key, T object) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
if (object == null && !key.allowNull()) {
throw new IllegalArgumentException("key \"" + key.getName() + "\" disallows null values but object was null");
}
data.put(key.getName(), object);
}
/**
* stores a list of objects for a given key
*
* @param
* @param key a list data key (where isList is set to true)
* @param objects
*/
public void setObjectList(ListDataKey key, List objects) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
if (objects == null && !key.allowNull()) {
throw new IllegalArgumentException("list key \"" + key.getName() + "\" disallows null values but object was null");
}
data.put(key.getName(), deepListCopy(objects));
}
/**
* checks if a value for that key exists (even if this value is null)
*
* @param
* @param key
* @return
*/
public boolean containsKey(Key key) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
return data.containsKey(key.getName());
}
public int getSize() {
return data.size();
}
void replaceObject(String key, Object object) {
this.data.put(key, object);
}
public void clearObject(DataKey> key) {
if (key == null) {
throw new IllegalArgumentException("key must not be null");
}
data.remove(key.getName());
}
/**
* returns a list of raw keys names.
*
* @return
*/
public List getRawKeys() {
return new ArrayList<>(data.keySet());
}
public List