Add yang.common.DerivedString class
[yangtools.git] / yang / yang-common / src / main / java / org / opendaylight / yangtools / yang / common / CachingDerivedString.java
1 /*
2  * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.yangtools.yang.common;
9 import static java.util.Objects.requireNonNull;
10
11 import org.eclipse.jdt.annotation.NonNullByDefault;
12 import org.eclipse.jdt.annotation.Nullable;
13
14 /**
15  * A opportunistically-caching {@link DerivedString}. Canonical name is cached at first encounter.
16  *
17  * @param <T> derived string type
18  * @author Robert Varga
19  */
20 @NonNullByDefault
21 public abstract class CachingDerivedString<T extends CachingDerivedString<T>> extends DerivedString<T> {
22     private static final long serialVersionUID = 1L;
23
24     private transient volatile @Nullable String str;
25
26     protected CachingDerivedString() {
27
28     }
29
30     protected CachingDerivedString(final String str) {
31         this.str = requireNonNull(str);
32     }
33
34     @Override
35     public final String toCanonicalString() {
36         String local;
37         return (local = this.str) != null ? local : (str = computeCanonicalString());
38     }
39
40     /**
41      * Return the canonical string representation of this object's value.
42      *
43      * @return Canonical string
44      */
45     protected abstract String computeCanonicalString();
46
47 }