Skip to content

Commit 022f1fb

Browse files
committed
Make Scanner interface less silly
1 parent 4e8dc54 commit 022f1fb

File tree

5 files changed

+56
-70
lines changed

5 files changed

+56
-70
lines changed

src/main/java/org/nibor/autolink/LinkExtractor.java

Lines changed: 5 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -83,47 +83,13 @@ public LinkExtractor build() {
8383
}
8484
}
8585

86-
private static class LinkSpanImpl implements LinkSpan {
87-
88-
private final LinkType linkType;
89-
private final int beginIndex;
90-
private final int endIndex;
91-
92-
private LinkSpanImpl(LinkType linkType, int beginIndex, int endIndex) {
93-
this.linkType = linkType;
94-
this.beginIndex = beginIndex;
95-
this.endIndex = endIndex;
96-
}
97-
98-
@Override
99-
public LinkType getType() {
100-
return linkType;
101-
}
102-
103-
@Override
104-
public int getBeginIndex() {
105-
return beginIndex;
106-
}
107-
108-
@Override
109-
public int getEndIndex() {
110-
return endIndex;
111-
}
112-
113-
@Override
114-
public String toString() {
115-
return "Link{type=" + getType() + ", beginIndex=" + beginIndex + ", endIndex=" + endIndex + "}";
116-
}
117-
}
118-
11986
private class LinkIterator implements Iterator<LinkSpan> {
12087

12188
private final CharSequence input;
12289

12390
private LinkSpan next = null;
12491
private int index = 0;
12592
private int rewindIndex = 0;
126-
private int[] result = new int[2];
12793

12894
public LinkIterator(CharSequence input) {
12995
this.input = input;
@@ -160,11 +126,11 @@ private void setNext() {
160126
while (index < length) {
161127
Scanner scanner = trigger(input.charAt(index));
162128
if (scanner != null) {
163-
boolean found = scanner.scan(input, index, rewindIndex, result);
164-
if (found) {
165-
next = new LinkSpanImpl(scanner.getLinkType(), result[0], result[1]);
166-
rewindIndex = result[1];
167-
index = result[1];
129+
LinkSpan link = scanner.scan(input, index, rewindIndex);
130+
if (link != null) {
131+
next = link;
132+
index = link.getEndIndex();
133+
rewindIndex = index;
168134
break;
169135
} else {
170136
index++;

src/main/java/org/nibor/autolink/internal/EmailScanner.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.nibor.autolink.internal;
22

3+
import org.nibor.autolink.LinkSpan;
34
import org.nibor.autolink.LinkType;
45

56
/**
@@ -10,27 +11,20 @@
1011
public class EmailScanner implements Scanner {
1112

1213
@Override
13-
public LinkType getLinkType() {
14-
return LinkType.EMAIL;
15-
}
16-
17-
@Override
18-
public boolean scan(CharSequence input, int triggerIndex, int rewindIndex, int[] result) {
14+
public LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex) {
1915
int beforeAt = triggerIndex - 1;
2016
int first = findFirst(input, beforeAt, rewindIndex);
2117
if (first == -1) {
22-
return false;
18+
return null;
2319
}
2420

2521
int afterAt = triggerIndex + 1;
2622
int last = findLast(input, afterAt);
2723
if (last == -1) {
28-
return false;
24+
return null;
2925
}
3026

31-
result[0] = first;
32-
result[1] = last + 1;
33-
return true;
27+
return new LinkSpanImpl(LinkType.EMAIL, first, last + 1);
3428
}
3529

3630
// See "Local-part" in RFC 5321, plus extensions in RFC 6531
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.nibor.autolink.internal;
2+
3+
import org.nibor.autolink.LinkSpan;
4+
import org.nibor.autolink.LinkType;
5+
6+
public class LinkSpanImpl implements LinkSpan {
7+
8+
private final LinkType linkType;
9+
private final int beginIndex;
10+
private final int endIndex;
11+
12+
public LinkSpanImpl(LinkType linkType, int beginIndex, int endIndex) {
13+
this.linkType = linkType;
14+
this.beginIndex = beginIndex;
15+
this.endIndex = endIndex;
16+
}
17+
18+
@Override
19+
public LinkType getType() {
20+
return linkType;
21+
}
22+
23+
@Override
24+
public int getBeginIndex() {
25+
return beginIndex;
26+
}
27+
28+
@Override
29+
public int getEndIndex() {
30+
return endIndex;
31+
}
32+
33+
@Override
34+
public String toString() {
35+
return "Link{type=" + getType() + ", beginIndex=" + beginIndex + ", endIndex=" + endIndex + "}";
36+
}
37+
38+
}
Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,17 @@
11
package org.nibor.autolink.internal;
22

3-
import org.nibor.autolink.LinkType;
3+
import org.nibor.autolink.LinkSpan;
44

55
public interface Scanner {
66

7-
/**
8-
* @return the type of link that this scanner tries to match
9-
*/
10-
LinkType getLinkType();
11-
127
/**
138
* @param input input text
149
* @param triggerIndex the index at which the trigger character for this scanner was
1510
* @param rewindIndex the index that can maximally be rewound to (either the very first character of the input or
1611
* the character after the last matched link)
17-
* @param result when a match was successful (true is returned), the beginIndex (inclusive) and endIndex (exclusive)
1812
* need to be set to be set here
19-
* @return true if matched, false otherwise
13+
* @return the matched link, or {@code null} if no link matched
2014
*/
21-
boolean scan(CharSequence input, int triggerIndex, int rewindIndex, int[] result);
15+
LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex);
2216

2317
}

src/main/java/org/nibor/autolink/internal/UrlScanner.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package org.nibor.autolink.internal;
22

3+
import org.nibor.autolink.LinkSpan;
34
import org.nibor.autolink.LinkType;
45

56
/**
@@ -10,28 +11,21 @@
1011
public class UrlScanner implements Scanner {
1112

1213
@Override
13-
public LinkType getLinkType() {
14-
return LinkType.URL;
15-
}
16-
17-
@Override
18-
public boolean scan(CharSequence input, int triggerIndex, int rewindIndex, int[] result) {
14+
public LinkSpan scan(CharSequence input, int triggerIndex, int rewindIndex) {
1915
int length = input.length();
2016
int afterSlashSlash = triggerIndex + 3;
2117
if (afterSlashSlash >= length || input.charAt(triggerIndex + 1) != '/' || input.charAt(triggerIndex + 2) != '/') {
22-
return false;
18+
return null;
2319
}
2420

2521
int first = findFirst(input, triggerIndex - 1, rewindIndex);
2622
if (first == -1) {
27-
return false;
23+
return null;
2824
}
2925

3026
int last = findLast(input, afterSlashSlash);
3127

32-
result[0] = first;
33-
result[1] = last + 1;
34-
return true;
28+
return new LinkSpanImpl(LinkType.URL, first, last + 1);
3529
}
3630

3731
// See "scheme" in RFC 3986

0 commit comments

Comments
 (0)