diff --git a/src/main/java/org/jabref/model/openoffice/ootext/OOFormat.java b/src/main/java/org/jabref/model/openoffice/ootext/OOFormat.java index 4bfba9e5baf..f502922f818 100644 --- a/src/main/java/org/jabref/model/openoffice/ootext/OOFormat.java +++ b/src/main/java/org/jabref/model/openoffice/ootext/OOFormat.java @@ -73,8 +73,8 @@ public static OOText paragraph(OOText ootext) { /** * Format an OO cross-reference showing the target's page number as label to a reference mark. */ - public static OOText formatReferenceToPageNumberOfReferenceMark(String referencMarkName) { - String string = String.format("", referencMarkName); + public static OOText formatReferenceToPageNumberOfReferenceMark(String referenceMarkName) { + String string = String.format("", referenceMarkName); return OOText.fromString(string); } } diff --git a/src/main/java/org/jabref/model/openoffice/ootext/OOText.java b/src/main/java/org/jabref/model/openoffice/ootext/OOText.java index ad0b9fb448d..f5142e8dc2e 100644 --- a/src/main/java/org/jabref/model/openoffice/ootext/OOText.java +++ b/src/main/java/org/jabref/model/openoffice/ootext/OOText.java @@ -17,7 +17,7 @@ private OOText(String data) { this.data = data; } - /* null input is passed through */ + /** @return null for null input, otherwise the argument wrapped into a new OOText */ public static OOText fromString(String string) { if (string == null) { return null; @@ -25,7 +25,7 @@ public static OOText fromString(String string) { return new OOText(string); } - /* null input is passed through */ + /** @return null for null input, otherwise the string inside the argument */ public static String toString(OOText ootext) { if (ootext == null) { return null; @@ -38,7 +38,6 @@ public String toString() { return data; } - /* Object.equals */ @Override public boolean equals(Object object) { diff --git a/src/main/java/org/jabref/model/openoffice/ootext/OOTextIntoOO.java b/src/main/java/org/jabref/model/openoffice/ootext/OOTextIntoOO.java index 0758d515446..caa78f07d2c 100644 --- a/src/main/java/org/jabref/model/openoffice/ootext/OOTextIntoOO.java +++ b/src/main/java/org/jabref/model/openoffice/ootext/OOTextIntoOO.java @@ -17,6 +17,7 @@ import org.jabref.model.openoffice.uno.UnoCast; import org.jabref.model.openoffice.uno.UnoCrossRef; import org.jabref.model.openoffice.util.OOPair; +import org.jabref.model.strings.StringUtil; import com.sun.star.awt.FontSlant; import com.sun.star.awt.FontStrikeout; @@ -181,7 +182,7 @@ public static void write(XTextDocument doc, XTextCursor position, OOText ootext) String endTagName = m.group(1); String startTagName = m.group(2); String attributeListPart = m.group(3); - boolean isStartTag = (endTagName == null) || "".equals(endTagName); + boolean isStartTag = StringUtil.isNullOrEmpty(endTagName); String tagName = isStartTag ? startTagName : endTagName; Objects.requireNonNull(tagName); @@ -231,7 +232,7 @@ public static void write(XTextDocument doc, XTextCursor position, OOText ootext) switch (key) { case "oo:ParaStyleName": //

- if (value != null && !value.equals("")) { + if (!StringUtil.isNullOrEmpty(value)) { if (setParagraphStyle(cursor, value)) { // Presumably tested already: LOGGER.debug(String.format("oo:ParaStyleName=\"%s\" failed", value)); @@ -689,7 +690,7 @@ private static List> setCharStrikeout(short value) { // CharStyleName private static List> setCharStyleName(String value) { List> settings = new ArrayList<>(); - if (value != null && value != "") { + if (!StringUtil.isNullOrEmpty(value)) { settings.add(new OOPair<>(CHAR_STYLE_NAME, value)); } else { LOGGER.warn("setCharStyleName: received null or empty value"); @@ -708,7 +709,7 @@ private static List> setCharLocale(Locale value) { * Locale from string encoding: language, language-country or language-country-variant */ private static List> setCharLocale(String value) { - if (value == null || "".equals(value)) { + if (StringUtil.isNullOrEmpty(value)) { throw new java.lang.IllegalArgumentException("setCharLocale \"\" or null"); } String[] parts = value.split("-"); diff --git a/src/main/java/org/jabref/model/openoffice/rangesort/RangeSort.java b/src/main/java/org/jabref/model/openoffice/rangesort/RangeSort.java index 1b4fc327598..d5024d6d8e2 100644 --- a/src/main/java/org/jabref/model/openoffice/rangesort/RangeSort.java +++ b/src/main/java/org/jabref/model/openoffice/rangesort/RangeSort.java @@ -12,21 +12,29 @@ import com.sun.star.text.XText; import com.sun.star.text.XTextRangeCompare; +/** + * RangeSort provides sorting based on XTextRangeCompare, which only provides comparison + * between XTextRange values within the same XText. + */ public class RangeSort { - /* - * Sort within a partition + /** + * Compare two RangeHolders (using RangeHolder.getRange()) within an XText. + * + * Note: since we only look at the ranges, this comparison is generally not consistent with + * `equals` on the RangeHolders. Probably should not be used for key comparison in + * TreeMap<RangeHolder> or Set<RangeHolder> + * */ - public static class HolderComparatorWithinPartition implements Comparator { - XTextRangeCompare cmp; + private final XTextRangeCompare cmp; HolderComparatorWithinPartition(XText text) { cmp = UnoCast.cast(XTextRangeCompare.class, text).get(); } - /* + /** * Assumes a and b belong to the same XText as cmp. */ @Override @@ -35,7 +43,7 @@ public int compare(RangeHolder a, RangeHolder b) { } } - /* + /** * Sort a list of RangeHolder values known to share the same getText(). * * Note: RangeHolder.getRange() is called many times. @@ -48,10 +56,9 @@ public static void sortWithinPartition(List rangeHold rangeHolders.sort(new HolderComparatorWithinPartition(text)); } - /* - * Partitioning + /** + * Represent a partitioning of RangeHolders by XText */ - public static class RangePartitions { private final Map> partitions; @@ -61,11 +68,7 @@ public RangePartitions() { public void add(V holder) { XText partitionKey = holder.getRange().getText(); - List partition = partitions.get(partitionKey); - if (partition == null) { - partition = new ArrayList<>(); - partitions.put(partitionKey, partition); - } + List partition = partitions.computeIfAbsent(partitionKey, _key -> new ArrayList<>()); partition.add(holder); } @@ -74,6 +77,9 @@ public List> getPartitions() { } } + /** + * Partition RangeHolders by the corresponding XText. + */ public static RangePartitions partitionRanges(List holders) { RangePartitions result = new RangePartitions<>(); for (V holder : holders) { @@ -82,7 +88,7 @@ public static RangePartitions partitionRanges(List return result; } - /* + /** * Note: RangeHolder.getRange() is called many times. */ public static RangePartitions partitionAndSortRanges(List holders) { diff --git a/src/main/java/org/jabref/model/openoffice/rangesort/RangeSortVisual.java b/src/main/java/org/jabref/model/openoffice/rangesort/RangeSortVisual.java index e971e7ea67c..1f39427cf43 100644 --- a/src/main/java/org/jabref/model/openoffice/rangesort/RangeSortVisual.java +++ b/src/main/java/org/jabref/model/openoffice/rangesort/RangeSortVisual.java @@ -4,11 +4,9 @@ import java.util.Collections; import java.util.List; -import org.jabref.model.openoffice.uno.NoDocumentException; import org.jabref.model.openoffice.uno.UnoScreenRefresh; import com.sun.star.awt.Point; -import com.sun.star.lang.WrappedTargetException; import com.sun.star.text.XTextDocument; import com.sun.star.text.XTextRange; import com.sun.star.text.XTextViewCursor; @@ -38,10 +36,7 @@ public class RangeSortVisual { */ public static List> visualSort(List> inputs, XTextDocument doc, - FunctionalTextViewCursor fcursor) - throws - WrappedTargetException, - NoDocumentException { + FunctionalTextViewCursor fcursor) { final int inputSize = inputs.size(); @@ -60,27 +55,19 @@ public static List> visualSort(List> input } fcursor.restore(doc); - if (positions.size() != inputSize) { - throw new IllegalStateException("visualSort: positions.size() != inputSize"); - } - // order by position - ArrayList>> set = new ArrayList<>(inputSize); + ArrayList>> comparableMarks = new ArrayList<>(inputSize); for (int i = 0; i < inputSize; i++) { RangeSortable input = inputs.get(i); - set.add(new ComparableMark<>(positions.get(i), + comparableMarks.add(new ComparableMark<>(positions.get(i), input.getIndexInPosition(), input)); } - Collections.sort(set, RangeSortVisual::compareTopToBottomLeftToRight); - - if (set.size() != inputSize) { - throw new IllegalStateException("visualSort: set.size() != inputSize"); - } + Collections.sort(comparableMarks, RangeSortVisual::compareTopToBottomLeftToRight); // collect ordered result - List> result = new ArrayList<>(set.size()); - for (ComparableMark> mark : set) { + List> result = new ArrayList<>(comparableMarks.size()); + for (ComparableMark> mark : comparableMarks) { result.add(mark.getContent()); }