com.github.TKnudsen.ComplexDataObject.data.keyValueObject.KeyValueObject 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.keyValueObject;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import com.github.TKnudsen.ComplexDataObject.data.complexDataObject.ComplexDataObject;
import com.github.TKnudsen.ComplexDataObject.data.interfaces.IKeyValueProvider;
import com.github.TKnudsen.ComplexDataObject.model.tools.MathFunctions;
/**
*
* Title: KeyValueObject
*
*
*
* Description: Basic data structure for an objects with keys/attributes and
* values/objects.
*
*
*
* Copyright: Copyright (c) 2015-2019
*
*
* @author Juergen Bernard
* @version 1.03
*/
public class KeyValueObject implements IKeyValueProvider, Iterable {
protected long ID;
protected SortedMap attributes = new TreeMap();
public KeyValueObject() {
this.ID = MathFunctions.randomLong();
}
public KeyValueObject(long ID) {
this.ID = ID;
}
public KeyValueObject(Long ID) {
if (ID == null)
throw new IllegalArgumentException("ID was null");
this.ID = ID.longValue();
}
@Override
public long getID() {
return ID;
}
@Override
public int hashCode() {
return Long.hashCode(ID);
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final KeyValueObject> other = (KeyValueObject>) obj;
// if (this.ID != other.getID())
// return false;
return this.hashCode() == other.hashCode() ? true : false;
}
/**
* Is true if the ComplexDataObject and a given Object have identical attributes
* and attribute values. ID, name and description are ignored.
*
* @param obj
* @return
*/
public boolean equalValues(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final ComplexDataObject other = (ComplexDataObject) obj;
Set keys = attributes.keySet();
keys.addAll(other.keySet());
for (String string : keys)
if (!getAttribute(string).equals(other.getAttribute(string)))
return false;
return true;
}
public boolean containsAttribute(String attribute) {
return attributes.containsKey(attribute);
}
@Override
public void add(String attribute, V value) {
attributes.put(attribute, value);
}
@Override
public V getAttribute(String attribute) {
return attributes.get(attribute);
}
@Override
public Set keySet() {
return attributes.keySet();
}
/**
* convenient method identical with keySet();
*
* @return
*/
public Set getAttributes() {
return attributes.keySet();
}
@Override
public V removeAttribute(String attribute) {
if (attributes.get(attribute) != null)
return attributes.remove(attribute);
return null;
}
@Override
public Class> getType(String attribute) {
if (attributes.get(attribute) != null)
return attributes.get(attribute).getClass();
return null;
}
@Override
public Map> getTypes() {
Map> ret = new HashMap<>();
for (String string : attributes.keySet())
if (attributes.get(string) == null)
ret.put(string, null);
else
ret.put(string, attributes.get(string).getClass());
return null;
}
@Override
public Iterator iterator() {
return attributes.keySet().iterator();
}
@Override
public String toString() {
String output = "";
for (String key : attributes.keySet())
output += (toLineString(key) + "\n");
return output;
}
private String toLineString(String attribute) {
String output = "Attribute: " + attribute + "\t";
if (attributes.get(attribute) == null)
output += ("Type: unknown\tValue: null");
else
output += ("Type: " + attributes.get(attribute).getClass() + "\t" + "Value: " + attributes.get(attribute));
return output;
}
}