com.github.TKnudsen.ComplexDataObject.data.DataContainer Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of complex-data-object Show documentation
Show all versions of complex-data-object Show documentation
A library that models real-world objects in Java, referred to as ComplexDataObjects. Other features: IO and preprocessing of ComplexDataObjects.
The newest version!
package com.github.TKnudsen.ComplexDataObject.data;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.TreeMap;
import com.github.TKnudsen.ComplexDataObject.data.interfaces.IKeyValueProvider;
/**
*
* Stores and manages collections of IDObjects. A DataSchema manages the
* keys/attributes of the collection iff the objects are instanceOf
* IKeyValueProvider.
*
*
*
* Copyright: Copyright (c) 2015-2020
*
*
* @author Juergen Bernard
* @version 1.04
*/
public class DataContainer> implements Iterable {
private LinkedHashMap objectsMap = new LinkedHashMap();
protected Map> attributeValues = new TreeMap>();
protected DataSchema dataSchema;
public DataContainer(DataSchema dataSchema) {
this.dataSchema = dataSchema;
}
public DataContainer(Map objectsMap) {
this.objectsMap.putAll(objectsMap);
dataSchema = new DataSchema();
for (Long ID : objectsMap.keySet())
extendDataSchema(objectsMap.get(ID));
for (String attribute : getAttributeNames())
calculateEntities(attribute);
}
public DataContainer(Iterable objects) {
dataSchema = new DataSchema();
for (T object : objects) {
objectsMap.put(object.getID(), object);
extendDataSchema(object);
}
for (String attribute : getAttributeNames())
calculateEntities(attribute);
}
/**
* adds a new object. updates data schema and attribute values.
*
* @param object
* @return
*/
public boolean add(T object) {
objectsMap.put(object.getID(), object);
extendDataSchema(object);
for (String attribute : getAttributeNames())
calculateEntities(attribute);
return true;
}
protected final void extendDataSchema(T object) {
if (object instanceof IKeyValueProvider) {
IKeyValueProvider> keyValueProvider = (IKeyValueProvider>) object;
for (String string : keyValueProvider.keySet())
if (!dataSchema.contains(string) && keyValueProvider.getAttribute(string) != null)
dataSchema.add(string, keyValueProvider.getAttribute(string).getClass());
}
}
/**
* Introduces or updates a new attribute.
*
*
*
* @param attribute the attribute name
* @param type the expected data type.
* @param defaultValue the default value in case the attribute is missing from a
* data object.
* @return the data schema instance for call-chaining.
*/
public DataSchema addAttribute(String attribute, Class type, A defaultValue) {
dataSchema.add(attribute, type, defaultValue);
Iterator objectIterator = iterator();
while (objectIterator.hasNext()) {
T next = objectIterator.next();
if (next.getAttribute(attribute) == null)
next.add(attribute, defaultValue);
}
return dataSchema;
}
/**
* Remove functionality. For test purposes. Maybe this functionality will be
* removed sometime.
*
* @param object
* @return
*/
public boolean remove(T object) {
if (object == null)
return false;
long id = object.getID();
if (!objectsMap.containsKey(id))
return false;
for (String attribute : attributeValues.keySet()) {
if (attributeValues.get(attribute) != null)
attributeValues.get(attribute).remove(id);
}
objectsMap.remove(id);
return true;
}
/**
* Removes an attribute from the container and the set of objects.
*
* @param attribute the attribute name.
* @return the data schema instance for call-chaining.
*/
public DataSchema remove(String attribute) {
Iterator iterator = iterator();
while (iterator.hasNext()) {
T o = iterator.next();
o.removeAttribute(attribute);
}
return dataSchema.remove(attribute);
}
@Override
public Iterator iterator() {
return objectsMap.values().iterator();
}
public Boolean isNumeric(String attribute) {
if (!dataSchema.contains(attribute)) {
System.err.println("ComplexDataContainer.isNumeric(attribute): attribute does not exist.");
return false;
}
if (Number.class.isAssignableFrom(dataSchema.getType(attribute)))
return true;
return false;
}
public Boolean isBoolean(String attribute) {
if (!dataSchema.contains(attribute))
return false;
if (Boolean.class.isAssignableFrom(dataSchema.getType(attribute)))
return true;
return false;
}
public Collection getAttributeNames() {
return dataSchema.getAttributeNames();
}
public Map getAttributeValues(String attribute) {
return attributeValues.get(attribute);
}
public Collection