Rename binding-runtime-dynamic to binding-loader
[yangtools.git] / common / 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 com.google.common.annotations.Beta;
12 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
13 import java.io.Serial;
14 import org.eclipse.jdt.annotation.NonNullByDefault;
15 import org.eclipse.jdt.annotation.Nullable;
16
17 /**
18  * A opportunistically-caching {@link DerivedString}. Canonical name is cached at first encounter.
19  *
20  * @param <T> derived string type
21  * @author Robert Varga
22  */
23 @Beta
24 @NonNullByDefault
25 public abstract class CachingDerivedString<T extends CachingDerivedString<T>> extends DerivedString<T> {
26     @Serial
27     private static final long serialVersionUID = 1L;
28
29     private transient volatile @Nullable String str;
30
31     @SuppressFBWarnings("NP_STORE_INTO_NONNULL_FIELD")
32     protected CachingDerivedString() {
33         this.str = null;
34     }
35
36     protected CachingDerivedString(final String str) {
37         this.str = requireNonNull(str);
38     }
39
40     @Override
41     public final String toCanonicalString() {
42         String local;
43         return (local = this.str) != null ? local : (str = computeCanonicalString());
44     }
45
46     /**
47      * Return the canonical string representation of this object's value.
48      *
49      * @return Canonical string
50      */
51     protected abstract String computeCanonicalString();
52 }