use of org.apache.stanbol.entityhub.servicesapi.util.TextIterator in project stanbol by apache.
the class InMemoryRepresentation method removeNaturalText.
@SuppressWarnings("unchecked")
@Override
public void removeNaturalText(String field, String text, String... languages) {
if (field == null) {
throw new IllegalArgumentException("The parsed field MUST NOT be NULL");
} else if (field.isEmpty()) {
throw new IllegalArgumentException("The parsed field MUST NOT be Empty");
}
Object values = representation.get(field);
if (values == null) {
return;
}
if (values instanceof Collection<?>) {
int removed = 0;
for (Iterator<Text> it = new TextIterator(valueFactory, ((Collection<Object>) values).iterator(), languages); it.hasNext(); ) {
//go to the next element
Text label = it.next();
if (text.equals(label.getText())) {
//and remove it
it.remove();
removed++;
}
}
if (removed > 0) {
//if some elements where removed
//check if there is only a singe or no elements left for the field
int size = ((Collection<Object>) values).size();
if (size == 1) {
representation.put(field, ((Collection<Object>) values).iterator().next());
} else if (size < 1) {
representation.remove(field);
}
}
} else if (text.equals(getNaturalLanguageValue(values, languages))) {
representation.remove(field);
}
//else there is a single value that does not fit -> nothing todo
}
Aggregations