stanfordnlp/CoreNLP · error · UnsupportedOperationException

retainAll not implemented

Error message

retainAll not implemented

What it means

Sentinel UnsupportedOperationException from IntervalTree.retainAll(Collection<?> c): the interval-tree-backed collection implements removeAll by delegating to per-element remove, but intersection-based retention (keeping only elements also in c) was never implemented, so the method throws instead of returning a wrong result. It fires whenever generic collection code calls retainAll on an IntervalTree.

Solutions

  1. Implement retention yourself: iterate the tree, collect items not in c, and remove them via removeAll(Collection) or individual remove calls
  2. Copy the contents into a HashSet and apply retainAll there if tree structure is not needed afterwards
  3. Avoid APIs that require retainAll on IntervalTree

Example fix

// before
tree.retainAll(keep); // throws
// after
List<T> toRemove = new ArrayList<>();
for (T t : tree) if (!keep.contains(t)) toRemove.add(t);
tree.removeAll(toRemove);
Defensive patterns

Strategy: try-catch

Validate before calling

if (collection instanceof edu.stanford.nlp.util.IntervalTree) { /* use manual retain loop instead of retainAll */ }

Try / catch

try { tree.retainAll(c); } catch (UnsupportedOperationException e) { /* fall back to iterative removal */ }

Prevention

When it happens

Trigger: Calling retainAll(...) on any IntervalTree instance, e.g. when using it where a generic Collection is expected and the code invokes bulk retention.

Common situations: Passing an IntervalTree to library code that calls retainAll as part of set operations; assuming full java.util.Collection support.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/165c25488fae8caa. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/util/IntervalTree.java:197

      }
      if (curIter != null && curIter.hasNext()) {
        return curIter.next();
      } else return null;
    }
  }

  @Override
  public boolean removeAll(Collection<?> c) {
    boolean modified = false;
    for (Object t:c) {
      if (remove(t)) { modified = true; }
    }
    return modified;
  }

  @Override
  public boolean retainAll(Collection<?> c) {
    throw new UnsupportedOperationException("retainAll not implemented");
  }

  @Override
  public boolean contains(Object o) {
    try {
      return contains((T) o);
    } catch (ClassCastException ex) {
      return false;
    }
  }

  @Override
  public boolean remove(Object o) {
    try {
      return remove((T) o);
    } catch (ClassCastException ex) {
      return false;
    }

View on GitHub (pinned to 1b7edd19c4)