Skip to content

Commit db4ed7f

Browse files
authored
Merge pull request #85 from CodesAway/dev-0.10
Lots of changes for 0.10 release
2 parents bdf5ee8 + be1fc97 commit db4ed7f

File tree

19 files changed

+587
-51
lines changed

19 files changed

+587
-51
lines changed

BEXCodeCompare/pom.xml

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
<modelVersion>4.0.0</modelVersion>
55
<groupId>info.codesaway</groupId>
66
<artifactId>bex</artifactId>
7-
<version>0.9.1</version>
8-
<name>BEX Code Compare</name>
9-
<description>Be Enhanced Code Compare</description>
7+
<version>0.10.0</version>
8+
<name>Be Enhanced Code Compare (BEϽC)</name>
9+
<description>An enhanced code compare, utilities, and code matching</description>
1010
<properties>
1111
<maven.compiler.source>1.8</maven.compiler.source>
1212
<maven.compiler.target>1.8</maven.compiler.target>
@@ -49,7 +49,6 @@
4949
<scope>test</scope>
5050
</dependency>
5151
</dependencies>
52-
5352
<build>
5453
<plugins>
5554
<plugin>
@@ -170,4 +169,4 @@
170169
<url>https://oss.sonatype.org/service/local/staging/deploy/maven2/</url>
171170
</repository>
172171
</distributionManagement>
173-
</project>
172+
</project>

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

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,12 @@
1818
* Pair of left and right values, which are of the same type
1919
*
2020
* <p>Instances of this class are immutable if generic class type is also immutable</p>
21+
*
22+
* <p>Instances of this class are Comparable if generic class type is also Comparable</p>
2123
* @param <T> the type of pair
2224
* @since 0.3
2325
*/
24-
public interface BEXPair<T> {
26+
public interface BEXPair<T> extends Comparable<BEXPair<T>> {
2527
/**
2628
* Gets the left value in the pair
2729
* @return the left value
@@ -97,7 +99,7 @@ public default IntBEXPair mapToInt(final ToIntFunction<T> function) {
9799

98100
/**
99101
* Applies the specified BiFunction, passing {@link #getLeft()} and {@link #getRight()} as arguments
100-
102+
101103
* @param function the BiFunction to apply
102104
* @return the result of applying the BiFunction
103105
*/
@@ -107,7 +109,7 @@ public default <R> R apply(final BiFunction<T, T, R> function) {
107109

108110
/**
109111
* Applies the specified ToIntBiFunction, passing {@link #getLeft()} and {@link #getRight()} as arguments
110-
112+
111113
* @param function the ToIntBiFunction to apply
112114
* @return the result of applying the ToIntBiFunction
113115
*/
@@ -117,7 +119,7 @@ public default int applyAsInt(final ToIntBiFunction<T, T> function) {
117119

118120
/**
119121
* Evaluates the specified BiPredicate, passing {@link #getLeft()} and {@link #getRight()} as arguments
120-
122+
121123
* @param predicate the BiPredicate to apply
122124
* @return <code>true</code> if the predicate matches when applying the arugments; otherwise, <code>false</code>
123125
*/
@@ -256,4 +258,25 @@ public default void acceptWithSide(final BiConsumer<T, BEXSide> consumer) {
256258
public default String toString(final String format) {
257259
return String.format(format, this.getLeft(), this.getRight());
258260
}
261+
262+
/**
263+
* Compares the BEXPair first by the left element then the right element
264+
* @throws ClassCastException if the entries do not implement Comparable or are not mutually Comparable
265+
* @since 0.10
266+
*/
267+
@Override
268+
public default int compareTo(final BEXPair<T> o) {
269+
@SuppressWarnings("unchecked")
270+
Comparable<? super T> left1 = (Comparable<? super T>) this.getLeft();
271+
272+
@SuppressWarnings("unchecked")
273+
Comparable<? super T> right1 = (Comparable<? super T>) this.getRight();
274+
275+
int compare = left1.compareTo(o.getLeft());
276+
if (compare != 0) {
277+
return compare;
278+
}
279+
280+
return right1.compareTo(o.getRight());
281+
}
259282
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ private BEXPairs() {
99
throw new UnsupportedOperationException();
1010
}
1111

12+
/**
13+
*
14+
* @param <T> the type
15+
* @param left the left value
16+
* @param right the right value
17+
* @return a BEXPair with the specified left / right values
18+
* @since 0.10
19+
*/
20+
public static <T> BEXPair<T> bexPair(final T left, final T right) {
21+
return new BEXPairValue<>(left, right);
22+
}
23+
1224
public static <K, V> BEXPair<V> mapGet(final BEXPair<? extends Map<K, V>> mapPair,
1325
final BEXPair<K> keyPair) {
1426
return mapPair.mapWithSide((m, side) -> m.get(keyPair.get(side)));

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ private IntBEXRange(final int start, final int end, final boolean hasInclusiveEn
2929
if (end < start) {
3030
throw new IllegalArgumentException(String.format("Invalid range start = %d, end = %d", start, end));
3131
}
32+
33+
if (end == start && !hasInclusiveStart && !hasInclusiveEnd) {
34+
// Mimics Guava's Range class and throws exception
35+
throw new IllegalArgumentException(String.format("Invalid range (%d, %d)", start, end));
36+
}
3237
}
3338

3439
/**

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,19 @@ public default boolean isEmpty() {
3838
&& !(this.hasInclusiveStart() && this.hasInclusiveEnd());
3939
}
4040

41+
/**
42+
*
43+
* @return the length of the range
44+
* @since 0.10
45+
*/
46+
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;
52+
}
53+
4154
public default IntRange canonical() {
4255
if (this.hasInclusiveStart() && !this.hasInclusiveEnd()) {
4356
return this;

BEXCodeCompare/src/main/java/info/codesaway/bex/matching/BEXMatchResult.java

Lines changed: 80 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,48 +5,107 @@
55
import java.util.Map.Entry;
66
import java.util.Set;
77

8-
import info.codesaway.bex.IntPair;
8+
import info.codesaway.bex.IntBEXRange;
99

1010
public interface BEXMatchResult {
1111
public BEXPattern pattern();
1212

1313
public String text();
1414

15-
public IntPair startEndPair();
15+
/**
16+
*
17+
* @return
18+
* @throws IllegalStateException
19+
* If no match has yet been attempted,
20+
* or if the previous match operation failed
21+
*/
22+
public IntBEXRange range();
1623

24+
/**
25+
*
26+
* @return
27+
* @throws IllegalStateException
28+
* If no match has yet been attempted,
29+
* or if the previous match operation failed
30+
*/
1731
public default int start() {
18-
return this.startEndPair().getLeft();
32+
return this.range().getStart();
1933
}
2034

35+
/**
36+
*
37+
* @return
38+
* @throws IllegalStateException
39+
* If no match has yet been attempted,
40+
* or if the previous match operation failed
41+
*/
2142
public default int end() {
22-
return this.startEndPair().getRight();
43+
return this.range().getEnd();
2344
}
2445

46+
/**
47+
*
48+
* @return
49+
* @throws IllegalStateException
50+
* If no match has yet been attempted,
51+
* or if the previous match operation failed
52+
*/
2553
public default String group() {
26-
return getSubstring(this.text(), this.startEndPair());
54+
return getSubstring(this.text(), this.range());
2755
}
2856

29-
public IntPair startEndPair(String group);
57+
/**
58+
*
59+
* @param group
60+
* @return
61+
* @throws IllegalStateException
62+
* If no match has yet been attempted,
63+
* or if the previous match operation failed
64+
*/
65+
public IntBEXRange range(String group);
3066

67+
/**
68+
*
69+
* @param group
70+
* @return
71+
* @throws IllegalStateException
72+
* If no match has yet been attempted,
73+
* or if the previous match operation failed
74+
*/
3175
public default int start(final String group) {
32-
return this.startEndPair(group).getLeft();
76+
return this.range(group).getStart();
3377
}
3478

79+
/**
80+
*
81+
* @param group
82+
* @throws IllegalStateException
83+
* If no match has yet been attempted,
84+
* or if the previous match operation failed
85+
*/
3586
public default int end(final String group) {
36-
return this.startEndPair(group).getRight();
87+
return this.range(group).getEnd();
3788
}
3889

90+
/**
91+
*
92+
* @param group
93+
* @return
94+
* @throws IllegalStateException
95+
* If no match has yet been attempted,
96+
* or if the previous match operation failed
97+
*/
3998
public default String group(final String group) {
4099
if (group.equals("*")) {
41100
return this.group();
42101
}
43102

44-
IntPair startEndPair = this.startEndPair(group);
45-
if (startEndPair.getLeft() == -1 || startEndPair.getRight() == -1) {
103+
IntBEXRange range = this.range(group);
104+
if (range.getStart() == -1 || range.getEnd() == -1) {
46105
return null;
47106
}
48107

49-
return getSubstring(this.text(), startEndPair);
108+
return getSubstring(this.text(), range);
50109
}
51110

52111
/**
@@ -61,10 +120,20 @@ public default String group(final String group) {
61120
*
62121
* @throws IllegalArgumentException
63122
* If the group is not specified in the pattern
123+
* @throws IllegalStateException
124+
* If no match has yet been attempted,
125+
* or if the previous match operation failed
64126
*/
65127
public default String get(final String group) {
66128
return this.group(group);
67129
}
68130

131+
/**
132+
*
133+
* @return
134+
* @throws IllegalStateException
135+
* If no match has yet been attempted,
136+
* or if the previous match operation failed
137+
*/
69138
public Set<Entry<String, String>> entrySet();
70139
}

0 commit comments

Comments
 (0)