{"record":{"id":"1523d87a9512ef34","repo":"apache/cassandra","slug":"unsupportedoperationexception","errorCode":null,"errorMessage":"UnsupportedOperationException","messagePattern":"UnsupportedOperationException","errorType":"exception","errorClass":"java.lang.UnsupportedOperationException","httpStatus":null,"severity":"warning","filePath":"src/java/org/apache/cassandra/concurrent/ManyToOneConcurrentLinkedQueue.java","lineNumber":241,"sourceCode":"            }\n            else\n            {\n                // check for tail updates after two hops\n                p = (p != t && t != (t = tail)) ? t : q;\n            }\n        }\n    }\n\n    @Override\n    public boolean contains(Object o)\n    {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public Iterator<E> iterator()\n    {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public Object[] toArray()\n    {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public <T> T[] toArray(T[] a)\n    {\n        throw new UnsupportedOperationException();\n    }\n\n    @Override\n    public boolean containsAll(Collection<?> c)\n    {\n        throw new UnsupportedOperationException();","sourceCodeStart":223,"sourceCodeEnd":259,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/concurrent/ManyToOneConcurrentLinkedQueue.java#L223-L259","documentation":"ManyToOneConcurrentLinkedQueue is a specialized many-producer/single-consumer queue that supports only the mutation/inspection primitives it needs (offer, poll, peek, etc.). iterator() is intentionally unimplemented and always throws UnsupportedOperationException, because traversal would violate its single-consumer design and lock-free implementation.","triggerScenarios":"Calling iterator() on the queue — directly, via for-each loops (for (E e : queue)), via streams (queue.stream()), or via Collection-copying helpers like new ArrayList<>(queue) and Guava/Collectors helpers that use Spliterator/iteration.","commonSituations":"Debug/monitoring code that dumps queue contents; tests iterating the queue to assert contents; generic code accepting Collection<E> and iterating it; converting the queue with copy constructors.","solutions":["Drain the queue with poll() in a loop instead of iterating.","Snapshot contents yourself: repeatedly poll into a list if you can afford consuming the elements.","Catch UnsupportedOperationException around iteration and fall back to drain-based inspection.","If full Collection semantics are needed, use ConcurrentLinkedQueue or ArrayBlockingQueue instead."],"exampleFix":"// before\nfor (E e : queue) { inspect(e); } // throws UnsupportedOperationException\n// after\nList<E> drained = new ArrayList<>();\nE e;\nwhile ((e = queue.poll()) != null) drained.add(e);","handlingStrategy":"try-catch","validationCode":"// avoid iteration entirely; drain instead\nList<E> snapshot = new ArrayList<>();\nE e;\nwhile ((e = queue.poll()) != null) snapshot.add(e);","typeGuard":"boolean supportsIteration(Collection<E> c) { return !(c instanceof ManyToOneConcurrentLinkedQueue); }","tryCatchPattern":"try {\n    for (E e : queue) inspect(e);\n} catch (UnsupportedOperationException e) {\n    // fall back to drain-based inspection\n}","preventionTips":["Never use for-each/streams on ManyToOneConcurrentLinkedQueue.","Drain with poll() to inspect contents.","Do not pass it as Collection<E> to generic utilities.","Use ConcurrentLinkedQueue if iteration is required."],"tags":["queue","unsupported-operation","collection"],"backgroundTag":"unsupported-operation","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}