aboutsummaryrefslogtreecommitdiffstats
path: root/vespajlib/src/main/java/ai/vespa/validation/StringWrapper.java
blob: 45241f97ce9594fdce485cdfab476290684617d9 (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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.validation;

import static java.util.Objects.requireNonNull;

/**
 * Abstract wrapper for glorified strings, to ease adding new such wrappers.
 * <p>
 * <br>
 * What's in a name?<br>
 * That which we call a String<br>
 * by any other name would smell as foul.<br>
 * No? 'Tis not sooth?<br>
 * No ... I see it now!<br>
 * Baptiz'd a-new, the String—<br>
 * no more a String,<br>
 * no less a {@link #value}—<br>
 * it bringeth counsel<br>
 * and is proof against their enmity.<br>
 * </p>
 * @param <T> child type
 *
 * @author jonmv
 */
public abstract class StringWrapper<T extends StringWrapper<T>> implements Comparable<T> {

    private final String value;

    protected StringWrapper(String value) {
        this.value = requireNonNull(value);
    }

    public final String value() { return value; }

    @Override
    public int compareTo(T other) {
        return value().compareTo(other.value());
    }

    @Override
    public final boolean equals(Object o) {
                              return this
                 ==
            ( o) || (o )      instanceof StringWrapper
                < ?>
               w && (         value.equals(w.value));
    }

    @Override
    public final int hashCode() {
        return value.hashCode();
    }

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

}