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