{"record":{"id":"05ae6a040c42a216","repo":"apache/pulsar","slug":"cannot-compare-with-other-getclass","errorCode":null,"errorMessage":"Cannot compare with ${other.getClass()}","messagePattern":"Cannot compare with (.+?)","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/MessageIdV5.java","lineNumber":338,"sourceCode":"        int count = buf.getInt();\n        Map<Long, MessageId> out = new HashMap<>(count);\n        for (int i = 0; i < count; i++) {\n            long segId = buf.getLong();\n            int idLen = buf.getInt();\n            if (idLen < 0 || idLen > buf.remaining()) {\n                throw new IOException(\"Invalid MessageIdV5 data: bad inner id length\");\n            }\n            byte[] idBytes = new byte[idLen];\n            buf.get(idBytes);\n            out.put(segId, MessageId.fromByteArray(idBytes));\n        }\n        return out;\n    }\n\n    @Override\n    public int compareTo(org.apache.pulsar.client.api.v5.MessageId other) {\n        if (!(other instanceof MessageIdV5 o)) {\n            throw new IllegalArgumentException(\"Cannot compare with \" + other.getClass());\n        }\n        int cmp = Long.compare(this.segmentId, o.segmentId);\n        if (cmp != 0) {\n            return cmp;\n        }\n        return this.v4MessageId.compareTo(o.v4MessageId);\n    }\n\n    @Override\n    public boolean equals(Object obj) {\n        if (this == obj) {\n            return true;\n        }\n        if (!(obj instanceof MessageIdV5 o)) {\n            return false;\n        }\n        return segmentId == o.segmentId && v4MessageId.equals(o.v4MessageId);\n    }","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-client-v5/src/main/java/org/apache/pulsar/client/impl/v5/MessageIdV5.java#L320-L356","documentation":"MessageIdV5.compareTo only supports comparing against other MessageIdV5 instances. If passed any other implementation of org.apache.pulsar.client.api.v5.MessageId, it throws IllegalArgumentException('Cannot compare with <class>') because segment-ordering semantics are undefined across implementations.","triggerScenarios":"Mixing message id types in the same collection (e.g. sorting a List mixing MessageIdV5 and another v5 MessageId implementation, or a sentinel/stub id), then calling Collections.sort or compareTo.","commonSituations":"Collecting ids from two client APIs into one set; a test helper returning a mock v5 MessageId; a custom MessageId implementation from a fork being compared with stock MessageIdV5.","solutions":["Ensure all ids being compared are MessageIdV5 instances produced by this client.","Convert or unwrap other implementations to MessageIdV5 before comparing.","In tests, build expected values via MessageIdV5 (or its factory methods) rather than mocks.","If you need mixed comparison, write a comparator that handles each type explicitly instead of relying on compareTo."],"exampleFix":"// before\nids.sort(Comparator.naturalOrder()); // list mixes id types\n// after\nids.sort((a, b) -> {\n    if (a instanceof MessageIdV5 a5 && b instanceof MessageIdV5 b5) return a5.compareTo(b5);\n    throw new IllegalStateException(\"Mixed id types in collection\");\n});","handlingStrategy":"type-guard","validationCode":"if (!(other instanceof MessageIdV5)) {\n    throw new IllegalArgumentException(\"compareTo requires MessageIdV5, got \"\n            + other.getClass().getName());\n}","typeGuard":"static boolean isComparableV5(org.apache.pulsar.client.api.v5.MessageId id) {\n    return id instanceof MessageIdV5;\n}","tryCatchPattern":"try {\n    cmp = a.compareTo(b);\n} catch (IllegalArgumentException e) {\n    log.error(\"Mixed message id types in ordered collection: {}\", e.getMessage());\n}","preventionTips":["Keep collections of ordered ids homogeneous — only MessageIdV5 instances.","Convert foreign id implementations to MessageIdV5 at ingestion boundaries.","Use real MessageIdV5 instances (not mocks) in tests that exercise ordering."],"tags":["message-id","comparison","illegal-argument"],"backgroundTag":"incomparable-message-id-types","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}