com.github.TKnudsen.ComplexDataObject.data.complexDataObject.ComplexDataObjectTools 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.
package com.github.TKnudsen.ComplexDataObject.data.complexDataObject;
/**
*
* Title: ComplexDataObjectTools
*
*
*
* Description: Provides little helpers for the work with ComplexDataObjects.
*
*
*
* Copyright: Copyright (c) 2015-2016
*
*
* @author Juergen Bernard
* @version 1.01
*/
public class ComplexDataObjectTools {
public static ComplexDataObject clone(ComplexDataObject object) {
if (object == null)
return null;
ComplexDataObject newObject = new ComplexDataObject(object.getName(), object.getDescription());
for (String string : object) {
newObject.add(string, object.getAttribute(string));
}
return newObject;
}
/**
* Merges ComplexDataObjects. Conflicting attributes are defined by the last
* occurrence in the input data.
*
* @param objects
* @return
*/
public static ComplexDataObject mergeObjects(Iterable objects) {
ComplexDataObject mergedObject = new ComplexDataObject();
for (ComplexDataObject object : objects) {
for (String attribute : object.keySet()) {
if (mergedObject.getAttribute(attribute) != null)
System.out.println("overwriting attribute " + attribute + "(" + object.getAttribute(attribute) + "->" + mergedObject.getAttribute(attribute) + ")");
mergedObject.add(attribute, object.getAttribute(attribute));
}
if (object.getName() != null)
mergedObject.setName(object.getName());
if (object.getDescription() != null)
mergedObject.setDescription(object.getDescription());
}
return mergedObject;
}
}