summaryrefslogtreecommitdiffhomepage
path: root/app/src/main/java/com/wireguard/config/Attribute.java
blob: d61cc744357df55fdbebea741c20cabe3c51fb15 (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
45
46
47
48
49
/*
 * Copyright © 2018 WireGuard LLC. All Rights Reserved.
 * SPDX-License-Identifier: Apache-2.0
 */

package com.wireguard.config;

import android.text.TextUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import java9.util.Optional;

public final class Attribute {
    private static final Pattern LINE_PATTERN = Pattern.compile("(\\w+)\\s*=\\s*([^\\s#][^#]*)");
    private static final Pattern LIST_SEPARATOR = Pattern.compile("\\s*,\\s*");

    private final String key;
    private final String value;

    private Attribute(final String key, final String value) {
        this.key = key;
        this.value = value;
    }

    public static String join(final Iterable<?> values) {
        return TextUtils.join(", ", values);
    }

    public static Optional<Attribute> parse(final CharSequence line) {
        final Matcher matcher = LINE_PATTERN.matcher(line);
        if (!matcher.matches())
            return Optional.empty();
        return Optional.of(new Attribute(matcher.group(1), matcher.group(2)));
    }

    public static String[] split(final CharSequence value) {
        return LIST_SEPARATOR.split(value);
    }

    public String getKey() {
        return key;
    }

    public String getValue() {
        return value;
    }
}