Skip to content

Commit

Permalink
Use streams
Browse files Browse the repository at this point in the history
  • Loading branch information
mangstadt committed Jan 7, 2024
1 parent 43d6406 commit a9417fd
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/main/java/ezvcard/io/html/HCardElement.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

import static ezvcard.util.StringUtils.NEWLINE;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
Expand Down Expand Up @@ -80,11 +80,13 @@ public String attr(String name) {
* @return the absolute URL or empty string if it doesn't exist
*/
public String absUrl(String name) {
String url = element.absUrl(name); //returns empty string for some protocols like "tel:" and "data:", but not for "http:" or "mailto:"
if (url.isEmpty()) {
url = element.attr(name);
}
return url;
/*
* Returns empty string for some protocols like "tel:" and "data:", but
* not for "http:" or "mailto:"
*/
String url = element.absUrl(name);

return url.isEmpty() ? element.attr(name) : url;
}

/**
Expand Down Expand Up @@ -132,25 +134,23 @@ public String firstValue(String cssClass) {
* @return the hCard values
*/
public List<String> allValues(String cssClass) {
Elements elements = element.getElementsByClass(cssClass);
List<String> values = new ArrayList<>(elements.size());
for (Element element : elements) {
values.add(value(element));
}
return values;
//@formatter:off
return element.getElementsByClass(cssClass).stream()
.map(this::value)
.collect(Collectors.toList());
//@formatter:on
}

/**
* Gets all type values (for example, "home" and "cell" for the "tel" type).
* @return the type values (in lower-case)
*/
public List<String> types() {
List<String> types = allValues("type");
List<String> lowerCaseTypes = new ArrayList<>(types.size());
for (String type : types) {
lowerCaseTypes.add(type.toLowerCase());
}
return lowerCaseTypes;
//@formatter:off
return allValues("type").stream()
.map(String::toLowerCase)
.collect(Collectors.toList());
//@formatter:on
}

/**
Expand All @@ -159,7 +159,7 @@ public List<String> types() {
*/
public void append(String text) {
boolean first = true;
String lines[] = text.split("\\r\\n|\\n|\\r");
String[] lines = text.split("\\r\\n|\\n|\\r");
for (String line : lines) {
if (!first) {
//replace newlines with "<br>" tags
Expand Down

0 comments on commit a9417fd

Please sign in to comment.