aboutsummaryrefslogtreecommitdiff
path: root/src/main/java/org/zwobble/mammoth/internal/styles/DocumentElementMatching.java
blob: a0c5aec31b7733f4674fb9ec07f1cde444512c04 (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
package org.zwobble.mammoth.internal.styles;

import org.zwobble.mammoth.internal.documents.Style;

import java.util.Optional;
import java.util.function.BiPredicate;

class DocumentElementMatching {
    static boolean matchesStyle(Optional<String> styleId, Optional<String> styleName, Optional<Style> style) {
        return matchesStyleId(styleId, style) && matchesStyleName(styleName, style);
    }

    private static boolean matchesStyleId(Optional<String> styleId, Optional<Style> style) {
        return matches(styleId, style.map(Style::getStyleId), Object::equals);
    }

    private static boolean matchesStyleName(Optional<String> styleName, Optional<Style> style) {
        return matches(styleName, style.flatMap(Style::getName), String::equalsIgnoreCase);
    }

    static <T> boolean matches(Optional<T> required, Optional<T> actual, BiPredicate<T, T> areEqual) {
        return required
            .map(requiredValue -> actual.map(actualValue -> areEqual.test(requiredValue, actualValue)).orElse(false))
            .orElse(true);
    }
}