Skip to content

Commit

Permalink
no natural sort for ComparableMark
Browse files Browse the repository at this point in the history
  • Loading branch information
antalk2 committed Jul 10, 2021
1 parent 7fa8501 commit b31cfd7
Showing 1 changed file with 13 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Objects;

import org.jabref.model.openoffice.uno.NoDocumentException;
import org.jabref.model.openoffice.uno.UnoScreenRefresh;
Expand Down Expand Up @@ -73,7 +72,7 @@ public static <T> List<RangeSortable<T>> visualSort(List<RangeSortable<T>> input
input.getIndexInPosition(),
input));
}
Collections.sort(set);
Collections.sort(set, RangeSortVisual::compareTopToBottomLeftToRight);

if (set.size() != inputSize) {
throw new IllegalStateException("visualSort: set.size() != inputSize");
Expand Down Expand Up @@ -115,6 +114,17 @@ private static Point findPositionOfTextRange(XTextRange range, XTextViewCursor c
return cursor.getPosition();
}

private static int compareTopToBottomLeftToRight(ComparableMark a, ComparableMark b) {

if (a.position.Y != b.position.Y) {
return a.position.Y - b.position.Y;
}
if (a.position.X != b.position.X) {
return a.position.X - b.position.X;
}
return a.indexInPosition - b.indexInPosition;
}

/**
* A reference mark name paired with its visual position.
*
Expand All @@ -123,7 +133,7 @@ private static Point findPositionOfTextRange(XTextRange range, XTextViewCursor c
*
* Used for sorting reference marks by their visual positions.
*/
private static class ComparableMark<T> implements Comparable<ComparableMark<T>> {
private static class ComparableMark<T> {

private final Point position;
private final int indexInPosition;
Expand All @@ -135,42 +145,10 @@ public ComparableMark(Point position, int indexInPosition, T content) {
this.content = content;
}

@Override
public int compareTo(ComparableMark other) {

if (position.Y != other.position.Y) {
return position.Y - other.position.Y;
}
if (position.X != other.position.X) {
return position.X - other.position.X;
}
return indexInPosition - other.indexInPosition;
}

@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}

if (o instanceof ComparableMark) {
ComparableMark other = (ComparableMark) o;
return ((this.position.X == other.position.X)
&& (this.position.Y == other.position.Y)
&& (this.indexInPosition == other.indexInPosition)
&& Objects.equals(this.content, other.content));
}
return false;
}

public T getContent() {
return content;
}

@Override
public int hashCode() {
return Objects.hash(position, indexInPosition, content);
}
}

}

0 comments on commit b31cfd7

Please sign in to comment.