Move CACHED_FILE_PATTERN constant to correct location
[yangtools.git] / yang / yang-data-util / src / main / java / org / opendaylight / yangtools / yang / data / util / LeafsetEntryInterner.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 javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.data.api.schema.LeafSetEntryNode;
16 import org.opendaylight.yangtools.yang.model.api.LeafListSchemaNode;
17 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
18 import org.opendaylight.yangtools.yang.model.api.type.BooleanTypeDefinition;
19 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition;
20 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
21 import org.slf4j.Logger;
22 import org.slf4j.LoggerFactory;
23
24 /**
25  * Utility class for sharing instances of {@link LeafSetEntryNode}s which have low cardinality -- e.g. those which hold
26  * boolean or enumeration values. Instances containing attributes are not interned.
27  *
28  * Such objects have cardinality which is capped at the product of QNAMES * TYPE_CARDINALITY, where QNAMES is the total
29  * number of different QNames where the type is used and TYPE_CARDINALITY is the number of possible values for the type.
30  * Boolean has cardinality of 2, enumerations have cardinality equal to the number of enum statements.
31  *
32  * The theory here is that we tend to have a large number (100K+) of entries in a few places, which could end up hogging
33  * the heap retained via the DataTree with duplicate objects (same QName, same value, different object). Using this
34  * utility, such objects will end up reusing the same object, preventing this overhead.
35  */
36 @Beta
37 public final class LeafsetEntryInterner {
38     private static final Logger LOG = LoggerFactory.getLogger(LeafsetEntryInterner.class);
39     private static final LeafsetEntryInterner INSTANCE = new LeafsetEntryInterner();
40     private static final Interner<Object> INTERNER = Interners.newWeakInterner();
41
42     private LeafsetEntryInterner() {
43
44     }
45
46     @SuppressWarnings("static-method")
47     public <T extends LeafSetEntryNode<?>> T intern(@Nonnull final T sample) {
48         if (!sample.getAttributes().isEmpty()) {
49             // Non-empty attributes, do not intern
50             return sample;
51         }
52
53         /*
54          * We do not perform type checks here as they are implied by #forSchema(LeafListSchemaNode). Any misuse can
55          * result in inappropriate candidates being interned, but the alternative would be quite a bit slower.
56          */
57         @SuppressWarnings("unchecked")
58         final T ret = (T) INTERNER.intern(sample);
59         LOG.trace("Interned object %s to %s", sample, ret);
60         return ret;
61     }
62
63     /**
64      * Return a {@link LeafsetEntryInterner} for a particular schema. Interner instances must be used only for leafset
65      * entries for that particular schema, otherwise they may produce unexpected results.
66      *
67      * @param schema Schema of the parent leaf set
68      * @return An interner instance, or null if the leafset's type should not be interned.
69      */
70     @Nullable public static LeafsetEntryInterner forSchema(@Nullable final LeafListSchemaNode schema) {
71         if (schema != null) {
72             final TypeDefinition<?> type = schema.getType();
73             if (type instanceof BooleanTypeDefinition || type instanceof EnumTypeDefinition ||
74                     type instanceof IdentityrefTypeDefinition) {
75                 return INSTANCE;
76             }
77         }
78         return null;
79     }
80 }