Skip to content

Commit 617852e

Browse files
authored
Merge pull request #110 from CodesAway/dev-0.13.0
Dev 0.13.0
2 parents 62f8257 + dd9e545 commit 617852e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+1948
-889
lines changed

BEXCodeCompare/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>info.codesaway</groupId>
66
<artifactId>bex</artifactId>
7-
<version>0.12.0</version>
7+
<version>0.13.0</version>
88
<name>Be Enhanced Code Compare (BEϽC)</name>
99
<description>An enhanced code compare, utilities, and code matching</description>
1010
<properties>

BEXCodeCompare/src/main/java/info/codesaway/bex/BEXPair.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import static info.codesaway.bex.BEXSide.LEFT;
44
import static info.codesaway.bex.BEXSide.RIGHT;
55

6+
import java.util.Objects;
67
import java.util.function.BiConsumer;
78
import java.util.function.BiFunction;
89
import java.util.function.BiPredicate;
@@ -99,7 +100,7 @@ public default IntBEXPair mapToInt(final ToIntFunction<T> function) {
99100

100101
/**
101102
* Applies the specified BiFunction, passing {@link #getLeft()} and {@link #getRight()} as arguments
102-
103+
103104
* @param function the BiFunction to apply
104105
* @return the result of applying the BiFunction
105106
*/
@@ -109,7 +110,7 @@ public default <R> R apply(final BiFunction<T, T, R> function) {
109110

110111
/**
111112
* Applies the specified ToIntBiFunction, passing {@link #getLeft()} and {@link #getRight()} as arguments
112-
113+
113114
* @param function the ToIntBiFunction to apply
114115
* @return the result of applying the ToIntBiFunction
115116
*/
@@ -119,7 +120,7 @@ public default int applyAsInt(final ToIntBiFunction<T, T> function) {
119120

120121
/**
121122
* Evaluates the specified BiPredicate, passing {@link #getLeft()} and {@link #getRight()} as arguments
122-
123+
123124
* @param predicate the BiPredicate to apply
124125
* @return <code>true</code> if the predicate matches when applying the arugments; otherwise, <code>false</code>
125126
*/
@@ -279,4 +280,13 @@ public default int compareTo(final BEXPair<T> o) {
279280

280281
return right1.compareTo(o.getRight());
281282
}
283+
284+
/**
285+
* Indicates if the left value equals the right value
286+
* @return <code>true</code> if the left value equals the right value
287+
* @since 0.13
288+
*/
289+
public default boolean hasEqualValues() {
290+
return Objects.equals(this.getLeft(), this.getRight());
291+
}
282292
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package info.codesaway.bex;
2+
3+
/**
4+
* Value with an index.
5+
*
6+
* @param <T> the value's type
7+
* @since 0.13
8+
*/
9+
public interface Indexed<T> {
10+
public int getIndex();
11+
12+
public T getValue();
13+
}
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package info.codesaway.bex;
2+
3+
import java.util.Objects;
4+
5+
/**
6+
* Value with an index.
7+
*
8+
* <p>Instances of this class are immutable if the generic class type is also immutable</p>
9+
*
10+
* @param <T> the value's type
11+
* @since 0.13
12+
*/
13+
public final class IndexedValue<T> implements Indexed<T> {
14+
// Referenced https://github.com/poetix/protonpack/blob/master/src/main/java/com/codepoetics/protonpack/Indexed.java
15+
16+
private final int index;
17+
private final T value;
18+
19+
public IndexedValue(final int index, final T value) {
20+
this.index = index;
21+
this.value = value;
22+
}
23+
24+
@Override
25+
public int getIndex() {
26+
return this.index;
27+
}
28+
29+
@Override
30+
public T getValue() {
31+
return this.value;
32+
}
33+
34+
@Override
35+
public String toString() {
36+
return this.index + " " + this.value;
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return Objects.hash(this.index, this.value);
42+
}
43+
44+
@Override
45+
public boolean equals(final Object obj) {
46+
if (this == obj) {
47+
return true;
48+
}
49+
if (obj == null) {
50+
return false;
51+
}
52+
if (this.getClass() != obj.getClass()) {
53+
return false;
54+
}
55+
IndexedValue<?> other = (IndexedValue<?>) obj;
56+
return this.index == other.index && Objects.equals(this.value, other.value);
57+
}
58+
}

BEXCodeCompare/src/main/java/info/codesaway/bex/IntBEXRange.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,5 +126,4 @@ public boolean equals(final Object obj) {
126126
IntBEXRange other = (IntBEXRange) obj;
127127
return this.end == other.end && this.hasInclusiveEnd == other.hasInclusiveEnd && this.start == other.start;
128128
}
129-
130129
}

BEXCodeCompare/src/main/java/info/codesaway/bex/IntRange.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,33 @@ default int getRight() {
2727

2828
public boolean hasInclusiveEnd();
2929

30+
/**
31+
*
32+
* @return
33+
* @since 0.13
34+
*/
35+
public default int getInclusiveStart() {
36+
return this.hasInclusiveStart() ? this.getStart() : this.getStart() + 1;
37+
}
38+
39+
/**
40+
*
41+
* @return
42+
* @since 0.13
43+
*/
44+
public default int getInclusiveEnd() {
45+
return this.hasInclusiveEnd() ? this.getEnd() : this.getEnd() - 1;
46+
}
47+
48+
/**
49+
*
50+
* @return
51+
* @since 0.13
52+
*/
53+
public default int getCanonicalEnd() {
54+
return this.hasInclusiveEnd() ? this.getEnd() + 1 : this.getEnd();
55+
}
56+
3057
public default boolean contains(final int value) {
3158
return value > this.getStart() && value < this.getEnd()
3259
|| this.hasInclusiveStart() && value == this.getStart()
@@ -38,27 +65,32 @@ public default boolean isEmpty() {
3865
&& !(this.hasInclusiveStart() && this.hasInclusiveEnd());
3966
}
4067

68+
/**
69+
* Indicates if the range is a closed range containing one value
70+
*
71+
* @return <code>true</code> if the range is a closed range containing one value
72+
* @since 0.13
73+
*/
74+
public default boolean isSingleValue() {
75+
return this.getStart() == this.getEnd()
76+
&& this.hasInclusiveStart() && this.hasInclusiveEnd();
77+
}
78+
4179
/**
4280
*
4381
* @return the length of the range
4482
* @since 0.10
4583
*/
4684
public default int length() {
47-
// Logic from canonical
48-
int start = this.hasInclusiveStart() ? this.getStart() : this.getStart() + 1;
49-
int end = this.hasInclusiveEnd() ? this.getEnd() + 1 : this.getEnd();
50-
51-
return end - start;
85+
return this.getCanonicalEnd() - this.getInclusiveStart();
5286
}
5387

5488
public default IntRange canonical() {
5589
if (this.hasInclusiveStart() && !this.hasInclusiveEnd()) {
5690
return this;
5791
}
5892

59-
int start = this.hasInclusiveStart() ? this.getStart() : this.getStart() + 1;
60-
int end = this.hasInclusiveEnd() ? this.getEnd() + 1 : this.getEnd();
61-
return IntBEXRange.of(start, end);
93+
return IntBEXRange.of(this.getInclusiveStart(), this.getCanonicalEnd());
6294
}
6395

6496
/**

BEXCodeCompare/src/main/java/info/codesaway/bex/diff/DiffHelper.java

Lines changed: 28 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import java.util.TreeMap;
3131
import java.util.TreeSet;
3232
import java.util.function.BiFunction;
33+
import java.util.function.BiPredicate;
3334
import java.util.function.Function;
3435
import java.util.function.ToIntFunction;
3536
import java.util.stream.Collectors;
@@ -971,6 +972,8 @@ public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff) {
971972
return combineToDiffBlocks(diff, false);
972973
}
973974

975+
private static final BiPredicate<DiffEdit, DiffEdit> ALWAYS_SHOULD_COMBINE = (x, y) -> true;
976+
974977
/**
975978
* Combines consecutive DiffEdit to form DiffBlock when possible
976979
*
@@ -980,12 +983,32 @@ public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff) {
980983
* @return
981984
*/
982985
public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff, final boolean allowReplacements) {
986+
return combineToDiffBlocks(diff, allowReplacements, ALWAYS_SHOULD_COMBINE);
987+
}
988+
989+
/**
990+
* Combines consecutive DiffEdit to form DiffBlock when possible and <code>shouldCombinePredicate</code> returns true
991+
*
992+
* <p>The returned list will consist of DiffUnit objects (either DiffBlock or DiffEdit)</p>
993+
*
994+
* @param diff
995+
* @param allowReplacements
996+
* @param shouldCombinePredicate indicates whether the two DiffEdits should be combined, if this method determines they can be combined (return <code>false</code> to prevent the combining)
997+
* @return
998+
* @since 0.13
999+
*/
1000+
public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff, final boolean allowReplacements,
1001+
final BiPredicate<DiffEdit, DiffEdit> shouldCombinePredicate) {
9831002
List<DiffUnit> diffBlocks = new ArrayList<>();
9841003

1004+
BiPredicate<DiffEdit, DiffEdit> usedShouldCombinePredicate = shouldCombinePredicate != null
1005+
? shouldCombinePredicate
1006+
: ALWAYS_SHOULD_COMBINE;
1007+
9851008
for (int i = 0; i < diff.size(); i++) {
9861009
DiffEdit diffEdit = diff.get(i);
9871010

988-
if (isNextDiffPartOfBlock(diff, i, allowReplacements)) {
1011+
if (isNextDiffPartOfBlock(diff, i, allowReplacements, usedShouldCombinePredicate)) {
9891012
DiffType blockDiffType = diffEdit.getType();
9901013

9911014
List<DiffEdit> edits = new ArrayList<>();
@@ -997,7 +1020,7 @@ public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff, fina
9971020
if (blockDiffType != REPLACEMENT_BLOCK && !blockDiffType.equals(nextDiffEdit.getType())) {
9981021
blockDiffType = REPLACEMENT_BLOCK;
9991022
}
1000-
} while (isNextDiffPartOfBlock(diff, i, allowReplacements));
1023+
} while (isNextDiffPartOfBlock(diff, i, allowReplacements, usedShouldCombinePredicate));
10011024

10021025
diffBlocks.add(new DiffBlock(blockDiffType, edits));
10031026
} else {
@@ -1017,7 +1040,7 @@ public static List<DiffUnit> combineToDiffBlocks(final List<DiffEdit> diff, fina
10171040
* @return
10181041
*/
10191042
private static boolean isNextDiffPartOfBlock(final List<DiffEdit> diff, final int i,
1020-
final boolean allowReplacements) {
1043+
final boolean allowReplacements, final BiPredicate<DiffEdit, DiffEdit> shouldCombinePredicate) {
10211044

10221045
if (i + 1 >= diff.size()) {
10231046
return false;
@@ -1040,7 +1063,8 @@ private static boolean isNextDiffPartOfBlock(final List<DiffEdit> diff, final in
10401063
}
10411064
}
10421065

1043-
return hasConsecutiveLines(diffEdit, nextDiffEdit, isReplancement);
1066+
return hasConsecutiveLines(diffEdit, nextDiffEdit, isReplancement)
1067+
&& shouldCombinePredicate.test(diffEdit, nextDiffEdit);
10441068
}
10451069

10461070
private static boolean canBePartOfReplacement(final DiffEdit diffEdit) {

BEXCodeCompare/src/main/java/info/codesaway/bex/diff/DiffWithIndex.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
import java.util.Optional;
44

55
import info.codesaway.bex.BEXSide;
6+
import info.codesaway.bex.Indexed;
67

7-
public final class DiffWithIndex {
8+
public final class DiffWithIndex implements Indexed<DiffEdit> {
89
private final DiffEdit diffEdit;
910
private final int index;
1011

@@ -17,6 +18,15 @@ public DiffEdit getDiffEdit() {
1718
return this.diffEdit;
1819
}
1920

21+
/**
22+
* @since 0.13
23+
*/
24+
@Override
25+
public DiffEdit getValue() {
26+
return this.getDiffEdit();
27+
}
28+
29+
@Override
2030
public int getIndex() {
2131
return this.index;
2232
}

0 commit comments

Comments
 (0)