Skip to content

Commit a911104

Browse files
Refactored to remove multiple calls of getSourceTexts() api (#1137)
1 parent 622ed5a commit a911104

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

webmagic-core/src/main/java/us/codecraft/webmagic/selector/AbstractSelectable.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import java.util.ArrayList;
55
import java.util.List;
6+
67
import org.apache.commons.collections4.CollectionUtils;
78

89
/**
@@ -55,11 +56,12 @@ public Selectable jsonPath(String jsonPath) {
5556

5657
@Override
5758
public String get() {
58-
if (CollectionUtils.isNotEmpty(all())) {
59-
return all().get(0);
60-
} else {
61-
return null;
62-
}
59+
List<String> sourceTexts = all();
60+
if (CollectionUtils.isNotEmpty(sourceTexts)) {
61+
return sourceTexts.get(0);
62+
}
63+
return null;
64+
6365
}
6466

6567
@Override
@@ -91,8 +93,9 @@ public Selectable replace(String regex, String replacement) {
9193
}
9294

9395
public String getFirstSourceText() {
94-
if (getSourceTexts() != null && getSourceTexts().size() > 0) {
95-
return getSourceTexts().get(0);
96+
List<String> sourceTexts = getSourceTexts();
97+
if (CollectionUtils.isNotEmpty(sourceTexts)) {
98+
return sourceTexts.get(0);
9699
}
97100
return null;
98101
}
@@ -104,6 +107,6 @@ public String toString() {
104107

105108
@Override
106109
public boolean match() {
107-
return getSourceTexts() != null && getSourceTexts().size() > 0;
110+
return CollectionUtils.isNotEmpty(getSourceTexts());
108111
}
109112
}

0 commit comments

Comments
 (0)