Skip to content

Commit

Permalink
Refactor code: Replace redundant conditions and enhance readability a…
Browse files Browse the repository at this point in the history
…cross several classes.
  • Loading branch information
marevol committed Oct 12, 2024
1 parent 88955e3 commit 4f8ca86
Show file tree
Hide file tree
Showing 20 changed files with 36 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@
public class EsDataService extends AbstractCrawlerService implements DataService<EsAccessResult> {

public EsDataService(final EsCrawlerConfig crawlerConfig) {
this.index = crawlerConfig.getDataIndex();
index = crawlerConfig.getDataIndex();
setNumberOfShards(crawlerConfig.getDataShards());
setNumberOfReplicas(crawlerConfig.getDataReplicas());
}

public EsDataService(final String name, final String type) {
this.index = name + "." + type;
index = name + "." + type;
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ public class EsUrlFilterService extends AbstractCrawlerService implements UrlFil
protected int maxLoadSize = 10000;

public EsUrlFilterService(final EsCrawlerConfig crawlerConfig) {
this.index = crawlerConfig.getFilterIndex();
index = crawlerConfig.getFilterIndex();
setNumberOfShards(crawlerConfig.getFilterShards());
setNumberOfReplicas(crawlerConfig.getFilterReplicas());
}

public EsUrlFilterService(final String name, final String type) {
this.index = name + "." + type;
index = name + "." + type;
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ public class EsUrlQueueService extends AbstractCrawlerService implements UrlQueu
protected int maxCrawlingQueueSize = 100;

public EsUrlQueueService(final EsCrawlerConfig crawlerConfig) {
this.index = crawlerConfig.getQueueIndex();
index = crawlerConfig.getQueueIndex();
setNumberOfShards(crawlerConfig.getQueueShards());
setNumberOfReplicas(crawlerConfig.getQueueReplicas());
}

public EsUrlQueueService(final String name, final String type) {
this.index = name + "." + type;
index = name + "." + type;
}

@PostConstruct
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,11 @@ public long getAccessCount() {
}

public long incrementAndGetAccessCount() {
return this.accessCount.incrementAndGet();
return accessCount.incrementAndGet();
}

public long decrementAndGetAccessCount() {
return this.accessCount.decrementAndGet();
return accessCount.decrementAndGet();
}

public CrawlerStatus getStatus() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ protected void storeChildUrl(final String childUrl, final String parentUrl, fina
}

protected boolean isValid(final UrlQueue<?> urlQueue) {
if ((urlQueue == null) || StringUtil.isBlank(urlQueue.getUrl())
|| (crawlerContext.getMaxDepth() >= 0 && urlQueue.getDepth() > crawlerContext.getMaxDepth())) {
if (urlQueue == null || StringUtil.isBlank(urlQueue.getUrl())
|| crawlerContext.getMaxDepth() >= 0 && urlQueue.getDepth() > crawlerContext.getMaxDepth()) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ boolean matches(final String path) {
try {
final int pos = path.indexOf('/', 6);
final URL uri = new URL(pos == -1 ? path : path.substring(0, pos));
if (!"ftp".equals(uri.getProtocol()) || (StringUtil.isNotBlank(server) && !server.equals(uri.getHost()))) {
if (!"ftp".equals(uri.getProtocol()) || StringUtil.isNotBlank(server) && !server.equals(uri.getHost())) {
return false;
}
int p = uri.getPort();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ protected FTPClient getClient(final FtpInfo info) throws IOException {
validateRequest(ftpClient);

final FtpAuthentication auth = ftpAuthenticationHolder.get(info.toUrl());
if ((auth != null) && !ftpClient.login(auth.getUsername(), auth.getPassword())) {
if (auth != null && !ftpClient.login(auth.getUsername(), auth.getPassword())) {
throw new CrawlerLoginFailureException("Login Failure: " + auth.getUsername() + " for " + info.toUrl());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void setValue(final String value) {
}

public boolean isValid() {
if (StringUtil.isBlank(name) || (value == null)) {
if (StringUtil.isBlank(name) || value == null) {
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ protected ComponentDef(final T instance, final Consumer<T> initializer, final St
}

protected T get() {
final T component = this.instance == null ? ClassUtil.newInstance(cls) : this.instance;
final T component = instance == null ? ClassUtil.newInstance(cls) : instance;
final BeanDesc beanDesc = BeanDescFactory.getBeanDesc(component.getClass());
for (final FieldDesc fieldDesc : beanDesc.getFieldDescs()) {
final Resource annotation = fieldDesc.getField().getAnnotation(Resource.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ public boolean equals(final Object obj) {
if (this == obj) {
return true;
}
if ((obj == null) || (getClass() != obj.getClass())) {
if (obj == null || getClass() != obj.getClass()) {
return false;
}
final RequestData other = (RequestData) obj;
if (!Objects.equals(metaData, other.metaData) || (method != other.method) || !Objects.equals(url, other.url)) {
if (!Objects.equals(metaData, other.metaData) || method != other.method || !Objects.equals(url, other.url)) {
return false;
}
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ public InputStream getResponseBody() {
}

public void setResponseBody(final byte[] responseBody) {
this.responseBodyBytes = responseBody;
responseBodyBytes = responseBody;
}

public void setResponseBody(final File responseBody, final boolean isTemporary) {
this.responseBodyFile = responseBody;
this.isTemporaryFile = isTemporary;
responseBodyFile = responseBody;
isTemporaryFile = isTemporary;
}

public String getCharSet() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,8 @@ public ExtractData extract() {
if (contentLength > maxContentLength) {
throw new MaxLengthExceededException(
"The content length (" + contentLength + " byte) is over " + maxContentLength + " byte.");
} else if (contentLength == 0) {
}
if (contentLength == 0) {
if (logger.isDebugEnabled()) {
logger.debug("The content length is 0.");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
import java.util.Properties;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

import org.apache.commons.io.output.ByteArrayOutputStream;
Expand Down Expand Up @@ -161,7 +160,7 @@ protected ExtractData getText(final InputStream inputStream, final Map<String, S
if (isByteStream) {
inputStream.mark(0);
tempFile = null;
contentLength = (long) ((ByteArrayInputStream) inputStream).available();
contentLength = ((ByteArrayInputStream) inputStream).available();
} else {
try {
tempFile = File.createTempFile("tikaExtractor-", ".out");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public RobotsTxt parse(final InputStream stream, final String charsetName) {
// ignore
}
}
} else if (((value = getValue(SITEMAP_RECORD, line)) != null) && (value.length() > 0)) {
} else if ((value = getValue(SITEMAP_RECORD, line)) != null && value.length() > 0) {
robotsTxt.addSitemap(value);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ protected boolean isValid(final InputStream in, final boolean recursive) {
}

final String preloadDate = new String(bytes, Constants.UTF_8);
if ((preloadDate.indexOf("<urlset") >= 0) || (preloadDate.indexOf("<sitemapindex") >= 0)
|| (preloadDate.startsWith("http://") || preloadDate.startsWith("https://"))) {
if (preloadDate.indexOf("<urlset") >= 0 || preloadDate.indexOf("<sitemapindex") >= 0 || preloadDate.startsWith("http://")
|| preloadDate.startsWith("https://")) {
// XML Sitemaps
return true;
}
Expand Down Expand Up @@ -240,7 +240,7 @@ public void endElement(final String uri, final String localName, final String qN
sitemapUrl.setChangefreq(buf.toString().trim());
buf = null;
}
} else if (PRIORITY_ELEMENT.equals(qName) && (buf != null)) {
} else if (PRIORITY_ELEMENT.equals(qName) && buf != null) {
sitemapUrl.setPriority(buf.toString().trim());
buf = null;
}
Expand Down Expand Up @@ -315,7 +315,7 @@ public void endElement(final String uri, final String localName, final String qN
sitemapFile.setLoc(buf.toString().trim());
buf = null;
}
} else if ("lastmod".equals(qName) && (buf != null)) {
} else if ("lastmod".equals(qName) && buf != null) {
sitemapFile.setLastmod(buf.toString().trim());
buf = null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ public DefaultIntervalController() {
}

public DefaultIntervalController(final Map<String, Long> params) {
Long millis;

millis = params.get("delayMillisAfterProcessing");
Long millis = params.get("delayMillisAfterProcessing");
if (millis != null) {
delayMillisAfterProcessing = millis;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ public void setAllRequired(final boolean allRequired) {

@Override
public boolean equals(final Object obj) {
if ((obj instanceof final RegexRule rule)
&& (allRequired == rule.isAllRequired() && defaultRule == rule.isDefaultRule() && regexMap.equals(rule.regexMap))) {
if (obj instanceof final RegexRule rule && allRequired == rule.isAllRequired() && defaultRule == rule.isDefaultRule()
&& regexMap.equals(rule.regexMap)) {
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ public boolean isTrimSpace() {
* @param trimSpace the trimSpace to set
*/
public void setTrimSpace(final boolean trimSpace) {
this.trimSpaceEnabled = trimSpace;
trimSpaceEnabled = trimSpace;
}

/**
Expand Down Expand Up @@ -495,8 +495,8 @@ private String getNamespaceForPrefix(final String prefix, final Node namespaceCo
namespace = "http://www.w3.org/XML/1998/namespace";
} else {
int type;
while ((null != parent) && (null == namespace)
&& (((type = parent.getNodeType()) == Node.ELEMENT_NODE) || (type == Node.ENTITY_REFERENCE_NODE))) {
while (null != parent && null == namespace
&& ((type = parent.getNodeType()) == Node.ELEMENT_NODE || type == Node.ENTITY_REFERENCE_NODE)) {
if (type == Node.ELEMENT_NODE) {
if (parent.getNodeName().indexOf(prefix + ":") == 0) {
return parent.getNamespaceURI();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ public boolean isTrimSpace() {
}

public void setTrimSpace(final boolean trimSpace) {
this.trimSpaceEnabled = trimSpace;
trimSpaceEnabled = trimSpace;
}

public String getCharsetName() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public String execute() {
}
alphanumSize = 0;
symbolSize = 0;
} else if ((c >= '0' && c <= '9') || (c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) {
} else if (c >= '0' && c <= '9' || c >= 'A' && c <= 'Z' || c >= 'a' && c <= 'z') {
if (duplicateTermRemoved && symbolSize > 0) {
isSpace = removeLastDuplication(buf, symbolSize, isSpace, termCache);
}
Expand All @@ -97,7 +97,7 @@ public String execute() {
}
isSpace = false;
symbolSize = 0;
} else if ((c >= '!' && c <= '/') || (c >= ':' && c <= '@') || (c >= '[' && c <= '`') || (c >= '{' && c <= '~')) {
} else if (c >= '!' && c <= '/' || c >= ':' && c <= '@' || c >= '[' && c <= '`' || c >= '{' && c <= '~') {
if (duplicateTermRemoved && alphanumSize > 0) {
isSpace = removeLastDuplication(buf, alphanumSize, isSpace, termCache);
}
Expand Down

0 comments on commit 4f8ca86

Please sign in to comment.