aboutsummaryrefslogtreecommitdiffstats
path: root/flags/src/main/java/com/yahoo/vespa/flags/FlagId.java
blob: e60e1cc1b38a975ad77a4a3ff16140aa39705bb0 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.flags;

import java.util.Objects;
import java.util.regex.Pattern;

/**
 * @author hakonhall
 */
public class FlagId implements Comparable<FlagId> {
    private static final Pattern ID_PATTERN = Pattern.compile("^[a-zA-Z0-9][a-zA-Z0-9._-]*$");

    private final String id;

    public FlagId(String id) {
        if (!ID_PATTERN.matcher(id).find()) {
            throw new IllegalArgumentException("Not a valid FlagId: '" + id + "'");
        }

        this.id = id;
    }

    @Override
    public int compareTo(FlagId that) {
        return this.id.compareTo(that.id);
    }

    @Override
    public String toString() {
        return id;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        FlagId flagId = (FlagId) o;
        return Objects.equals(id, flagId.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }
}