2 * Copyright (c) 2018 Pantheon Technologies, s.r.o. and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.common;
9 import static java.util.Objects.requireNonNull;
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;
17 * A opportunistically-caching {@link DerivedString}. Canonical name is cached at first encounter.
19 * @param <T> derived string type
20 * @author Robert Varga
24 public abstract class CachingDerivedString<T extends CachingDerivedString<T>> extends DerivedString<T> {
25 private static final long serialVersionUID = 1L;
27 private transient volatile @Nullable String str;
29 @SuppressFBWarnings("NP_STORE_INTO_NONNULL_FIELD")
30 protected CachingDerivedString() {
34 protected CachingDerivedString(final String str) {
35 this.str = requireNonNull(str);
39 public final String toCanonicalString() {
41 return (local = this.str) != null ? local : (str = computeCanonicalString());
45 * Return the canonical string representation of this object's value.
47 * @return Canonical string
49 protected abstract String computeCanonicalString();