Move CACHED_FILE_PATTERN constant to correct location
[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 javax.annotation.Nonnull;
14 import javax.annotation.Nullable;
15 import org.opendaylight.yangtools.yang.data.api.AttributesContainer;
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.EnumTypeDefinition;
21 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
22 import org.slf4j.Logger;
23 import org.slf4j.LoggerFactory;
24
25 /**
26  * Utility class for sharing instances of {@link LeafNode}s which have low cardinality-- e.g. those which hold
27  * boolean or enumeration values. Instances containing attributes are not interned.
28  *
29  * Such objects have cardinality which is capped at the product of QNAMES * TYPE_CARDINALITY, where QNAMES is the total
30  * number of different QNames where the type is used and TYPE_CARDINALITY is the number of possible values for the type.
31  * Boolean has cardinality of 2, enumerations have cardinality equal to the number of enum statements.
32  *
33  * The theory here is that we tend to have a large number (100K+) of entries in a few places, which could end up hogging
34  * the heap retained via the DataTree with duplicate objects (same QName, same value, different object). Using this
35  * utility, such objects will end up reusing the same object, preventing this overhead.
36  */
37 @Beta
38 public abstract class LeafInterner {
39     private static final class Noop extends LeafInterner {
40         @Override
41         public <T extends LeafNode<?>> T intern(final T sample) {
42             return sample;
43         }
44     }
45
46     private static final class Weak extends LeafInterner {
47         private static final Logger LOG = LoggerFactory.getLogger(Weak.class);
48         private static final Interner<Object> INTERNER = Interners.newWeakInterner();
49
50         @Override
51         public <T extends LeafNode<?>> T intern(final T sample) {
52             if (!((AttributesContainer) sample).getAttributes().isEmpty()) {
53                 // Non-empty attributes, do not intern
54                 return sample;
55             }
56
57             // All checks completed, intern the sample
58             @SuppressWarnings("unchecked")
59             final T ret = (T) INTERNER.intern(sample);
60             LOG.trace("Interned object %s to %s", sample, ret);
61             return ret;
62         }
63     }
64
65     private static final LeafInterner NOOP = new Noop();
66     private static final LeafInterner WEAK = new Weak();
67
68     LeafInterner() {
69
70     }
71
72     /**
73      * Return a {@link LeafInterner} for a particular schema. Interner instances must not be reused for leaves of
74      * different types, otherwise they may produce unexpected results.
75      *
76      * @param schema The leaf node's schema
77      * @return An interner instance
78      */
79     @Nonnull public static LeafInterner forSchema(@Nullable final LeafSchemaNode schema) {
80         if (schema != null) {
81             final TypeDefinition<?> type = schema.getType();
82             if (type instanceof BooleanTypeDefinition || type instanceof EnumTypeDefinition ||
83                     type instanceof IdentityrefTypeDefinition) {
84                 return WEAK;
85             }
86         }
87
88         return NOOP;
89     }
90
91     public abstract <T extends LeafNode<?>> T intern(final T sample);
92 }