Refactored to remove multiple calls of getSourceTexts() api (#1137)

master
Harikrishna 2023-11-24 17:39:32 +05:30 committed by GitHub
parent 622ed5a17f
commit a911104076
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 8 deletions

View File

@ -3,6 +3,7 @@ package us.codecraft.webmagic.selector;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.collections4.CollectionUtils; import org.apache.commons.collections4.CollectionUtils;
/** /**
@ -55,11 +56,12 @@ public abstract class AbstractSelectable implements Selectable {
@Override @Override
public String get() { public String get() {
if (CollectionUtils.isNotEmpty(all())) { List<String> sourceTexts = all();
return all().get(0); if (CollectionUtils.isNotEmpty(sourceTexts)) {
} else { return sourceTexts.get(0);
return null; }
} return null;
} }
@Override @Override
@ -91,8 +93,9 @@ public abstract class AbstractSelectable implements Selectable {
} }
public String getFirstSourceText() { public String getFirstSourceText() {
if (getSourceTexts() != null && getSourceTexts().size() > 0) { List<String> sourceTexts = getSourceTexts();
return getSourceTexts().get(0); if (CollectionUtils.isNotEmpty(sourceTexts)) {
return sourceTexts.get(0);
} }
return null; return null;
} }
@ -104,6 +107,6 @@ public abstract class AbstractSelectable implements Selectable {
@Override @Override
public boolean match() { public boolean match() {
return getSourceTexts() != null && getSourceTexts().size() > 0; return CollectionUtils.isNotEmpty(getSourceTexts());
} }
} }