blob: 980b88c42e9726cebf7bac477ab61a2cd55a9a34 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
package org.zwobble.mammoth.styles;
import org.zwobble.mammoth.documents.Paragraph;
import org.zwobble.mammoth.documents.Style;
import java.util.Optional;
public class ParagraphMatcher {
public static final ParagraphMatcher ANY = new ParagraphMatcher(Optional.empty(), Optional.empty());
public static ParagraphMatcher styleId(String styleId) {
return new ParagraphMatcher(Optional.of(styleId), Optional.empty());
}
public static ParagraphMatcher styleName(String styleName) {
return new ParagraphMatcher(Optional.empty(), Optional.of(styleName));
}
private final Optional<String> styleId;
private final Optional<String> styleName;
public ParagraphMatcher(Optional<String> styleId, Optional<String> styleName) {
this.styleId = styleId;
this.styleName = styleName;
}
public boolean matches(Paragraph paragraph) {
return matchesStyleId(paragraph) && matchesStyleName(paragraph);
}
private boolean matchesStyleId(Paragraph paragraph) {
return matches(styleId, paragraph.getStyle().map(Style::getStyleId));
}
private boolean matchesStyleName(Paragraph paragraph) {
return matches(styleName, paragraph.getStyle().flatMap(Style::getName));
}
private boolean matches(Optional<String> required, Optional<String> actual) {
return required
.map(requiredValue -> actual.map(requiredValue::equals).orElse(false))
.orElse(true);
}
}
|