Do not pretty-print body class
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / LeafInterner.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. 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.data.util;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.collect.Interner;
12 import com.google.common.collect.Interners;
13 import java.util.Optional;
14 import org.eclipse.jdt.annotation.NonNull;
15 import org.eclipse.jdt.annotation.Nullable;
16 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
17 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
18 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.EmptyTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
22 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
23 import org.opendaylight.yangtools.yang.model.api.type.Int8TypeDefinition;
24 import org.opendaylight.yangtools.yang.model.api.type.Uint8TypeDefinition;
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27
28 /**
29  * Utility class for sharing instances of {@link LeafNode}s which have low cardinality-- e.g. those which hold
30  * boolean or enumeration values. Instances containing attributes are not interned.
31  *
32  * <p>
33  * Such objects have cardinality which is capped at the product of QNAMES * TYPE_CARDINALITY, where QNAMES is the total
34  * number of different QNames where the type is used and TYPE_CARDINALITY is the number of possible values for the type.
35  * Boolean has cardinality of 2, enumerations have cardinality equal to the number of enum statements.
36  *
37  * <p>
38  * The theory here is that we tend to have a large number (100K+) of entries in a few places, which could end up hogging
39  * the heap retained via the DataTree with duplicate objects (same QName, same value, different object). Using this
40  * utility, such objects will end up reusing the same object, preventing this overhead.
41  */
42 @Beta
43 public final class LeafInterner {
44     private static final Logger LOG = LoggerFactory.getLogger(LeafInterner.class);
45     private static final Interner<Object> INTERNER = Interners.newWeakInterner();
46
47     private LeafInterner() {
48         // Hidden on purpose
49     }
50
51     private static <T extends LeafNode<?>> T intern(final T sample) {
52         @SuppressWarnings("unchecked")
53         final T ret = (T) INTERNER.intern(sample);
54         LOG.trace("Interned object {} to {}", sample, ret);
55         return ret;
56     }
57
58     /**
59      * Return a {@link LeafInterner} for a particular schema. Interner instances must not be reused for leaves of
60      * different types, otherwise they may produce unexpected results.
61      *
62      * @param schema The leaf node's schema
63      * @return An interner instance, if applicable
64      */
65     public static <T extends LeafNode<?>> @NonNull Optional<Interner<T>> forSchema(
66             final @Nullable LeafSchemaNode schema) {
67         if (schema != null && isLowCardinality(schema.getType())) {
68             return Optional.of(LeafInterner::intern);
69         }
70
71         return Optional.empty();
72     }
73
74     private static boolean isLowCardinality(final TypeDefinition<?> type) {
75         return type instanceof BooleanTypeDefinition
76                 || type instanceof EmptyTypeDefinition
77                 || type instanceof EnumTypeDefinition
78                 || type instanceof IdentityrefTypeDefinition
79                 || type instanceof Int8TypeDefinition
80                 || type instanceof Uint8TypeDefinition;
81     }
82 }