Copyright (c) 2013 Lorenz Lehmann
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Sof...
If you think the Android project sodf listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
Java Source Code
package lal.sodf.framework.ontology;
//fromwww.java2s.comimport java.util.LinkedList;
/**
* A node that has one or multiple values attached to it
* @author Lorenz Lehmann
*
*/publicclass KeyValueNode extends Node{
/** The list of values, enclosed in this node */private LinkedList<String> values = new LinkedList<String>();
/** Create a new KeyValueNode with one value */public KeyValueNode(KeyNode _parent, String _key, String _value) {
super(_parent, _key);
addValue(_value);
}
/** Create a new KeyValueNode with one value */public KeyValueNode(String _key, String _value) {
super(_key);
addValue(_value);
}
/** Create a new KeyValueNode with multiple values */public KeyValueNode(KeyNode _parent, String _key, String[] _values) {
super(_parent, _key);
addValue(_values);
}
/** Create a new KeyValueNode with multiple values */public KeyValueNode(String _key, String[] _values) {
super(_key);
addValue(_values);
}
/** Get all the values within this node */public String[] getValues(){
return values.toArray(new String[values.size()]);
}
/** Get the first value of this node. If the node only has one value this will be returned */public String getFirstValue(){
return values.getFirst();
}
/**
* Add a value to the list of values
* @param _value The value to be added
* @return True if the value was added, false if the value is already in the list
*/publicboolean addValue(String _value){
//only add the value if it does not exist in the value list
if (values.contains(_value)) return false;
//otherwise just add it
values.add(_value);
return true;
}
/**
* Add a number of values to the list of values
* @param _values The value to be added
* @return The list of values, which were not added to the list because they were already in it. The array has size 0 if all values were added.
*/public String[] addValue(String[] _values){
LinkedList<String> notAdded = new LinkedList<String>();
for (int i = 0; i < _values.length; i++){
//if the value cannot be added move it to another list to inform the user which values did not get added
if (addValue(_values[i]) == false){
notAdded.add(_values[i]);
}
}
//check first if anything was rejected at all
if (notAdded.size() <= 0) returnnew String[0];
return notAdded.toArray(new String[notAdded.size()]);
}
/**
* Remove a value from the list of values. A KeyValueNode must always contain at least one value!
* @param _value The value to be removed
* @return True if the value was removed. False if the value was not found or if this is the only value in the list.
*/publicboolean removeValue(String _value){
if (values.size()<=1) return false;//always at least one value
return values.remove(_value);
}
/**
* Check if a given value is a value of this node
* @return True if this value is within this node, else false
*/publicboolean containsValue(String _value){
return values.contains(_value);
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return"KeyValueNode [values=" + values + ", key=" + key + "]";
}
}