{"record":{"id":"47ba0e9d8e0b5da8","repo":"stanfordnlp/CoreNLP","slug":"span-tostring-contains-otherspan-oth","errorCode":null,"errorMessage":"Span \" + toString() + \" contains otherSpan \" + otherSpan + \" (or vice versa)","messagePattern":"Span \" \\+ toString\\(\\) \\+ \" contains otherSpan \" \\+ otherSpan \\+ \" \\(or vice versa\\)","errorType":"exception","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/edu/stanford/nlp/ie/machinereading/structure/Span.java","lineNumber":127,"sourceCode":"  public boolean contains(Span otherSpan) {\n    return this.start <= otherSpan.start && otherSpan.end <= this.end;\n  }\n  \n  /**\n   * Returns true if i is inside this span.  Note that the start is inclusive and the end is exclusive.\n   */\n  public boolean contains(int i) {\n    return this.start <= i && i < this.end;\n  }\n\n  /**\n   * Returns true if this span ends before the otherSpan starts.\n   * \n   * @throws IllegalArgumentException if either span contains the other span\n   */\n  public boolean isBefore(Span otherSpan) {\n    if (this.contains(otherSpan) || otherSpan.contains(this)) {\n      throw new IllegalArgumentException(\"Span \" + toString() + \" contains otherSpan \" + otherSpan + \" (or vice versa)\");\n    }\n    return this.end <= otherSpan.start;\n  }\n\n  /**\n   * Returns true if this span starts after the otherSpan's end.\n   * \n   * @throws IllegalArgumentException if either span contains the other span\n   */\n  @SuppressWarnings(\"UnusedDeclaration\")\n  public boolean isAfter(Span otherSpan) {\n    if (this.contains(otherSpan) || otherSpan.contains(this)) {\n      throw new IllegalArgumentException(\"Span \" + toString() + \" contains otherSpan \" + otherSpan + \" (or vice versa)\");\n    }\n    return this.start >= otherSpan.end;\n  }\n\n  /**","sourceCodeStart":109,"sourceCodeEnd":145,"githubUrl":"https://github.com/stanfordnlp/CoreNLP/blob/1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a/src/edu/stanford/nlp/ie/machinereading/structure/Span.java#L109-L145","documentation":"Span.isBefore(Span) compares spans that are assumed to be strictly ordered (this ends before other starts). If either span contains the other, ordering is undefined, so the method throws IllegalArgumentException naming both spans. It is also called internally by Span.distance.","triggerScenarios":"Calling spanA.isBefore(spanB) (directly or via Span.distance) where spanA.contains(spanB) or spanB.contains(spanA), e.g. overlapping/identical token spans like [3,7] and [5,9] or [3,7] and [3,7].","commonSituations":"Computing distances between extracted relation mentions whose entity spans overlap; comparing annotation spans produced from different tokenizations; accidentally passing the same span twice.","solutions":["Check containment first (a.contains(b) || b.contains(a)) and handle overlapping spans separately before calling isBefore","Trim/narrow one of the spans so they are disjoint before comparison","Use raw start/end arithmetic instead of isBefore when containment is legitimate in your data","Compare start/end fields directly with explicit semantics for the overlap case"],"exampleFix":"// before\nint d = spanA.distance(spanB); // throws when one span contains the other\n// after\nint d = (spanA.contains(spanB) || spanB.contains(spanA)) ? 0 : spanA.distance(spanB);","handlingStrategy":"validation","validationCode":"if (a.contains(b) || b.contains(a)) { /* skip or treat distance as 0 */ } else { d = a.distance(b); }","typeGuard":"static boolean orderable(Span a, Span b) { return !a.contains(b) && !b.contains(a); }","tryCatchPattern":"try { return a.isBefore(b); } catch (IllegalArgumentException e) { return false; } // containment case handled explicitly elsewhere","preventionTips":["Deduplicate and filter nested spans before pairwise ordering","Add a containment pre-check before any distance/isBefore call","Document that Span ordering methods require disjoint spans"],"tags":["java","span","precondition-violation"],"backgroundTag":"invalid-state-transition","analyzedSha":"1b7edd19c4d0d7b1f13a2591425b9b60a0b1af7a","analyzedAt":"2026-09-10T02:24:07.274Z","contentChangedAt":"2026-09-10T02:24:07.274Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}