10eb0fa252dee83ab45005fe449642cd034f68e4
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / CopyHistory.java
1 /*
2  * Copyright (c) 2016 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.parser.spi.meta;
9
10 import com.google.common.annotations.Beta;
11 import com.google.common.annotations.VisibleForTesting;
12 import com.google.common.base.Verify;
13 import org.opendaylight.yangtools.concepts.Immutable;
14
15 @Beta
16 public final class CopyHistory implements Immutable {
17     private static final CopyType[] VALUES = CopyType.values();
18
19     private static final CopyHistory[][] CACHE = new CopyHistory[VALUES.length][];
20     static {
21         /*
22          * Cache size is dependent on number of items in CopyType, it costs N * 2^N objects.
23          * For 4 types that boils down to 4 * 16 = 64 objects.
24          * For 5 types that boils down to 5 * 32 = 160 objects.
25          * For 6 types that boils down to 6 * 64 = 384 objects.
26          *
27          * If we ever hit 6 types, the caching strategy needs to be revisited.
28          */
29         Verify.verify(VALUES.length < 6);
30     }
31
32     private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
33
34     private final short operations;
35     private final short lastOperation;
36
37     private CopyHistory(final int operations, final CopyType lastOperation) {
38         this.operations = (short) operations;
39         this.lastOperation = (short) lastOperation.ordinal();
40     }
41
42     public static CopyHistory original() {
43         return ORIGINAL;
44     }
45
46     public static CopyHistory of(final CopyType copyType, final CopyHistory copyHistory) {
47         return ORIGINAL.append(copyType, copyHistory);
48     }
49
50     private static CopyHistory[] cacheArray(final CopyType lastOperation) {
51         final int ordinal = lastOperation.ordinal();
52         CopyHistory[] ret = CACHE[ordinal];
53         if (ret == null) {
54             synchronized (CACHE) {
55                 ret = CACHE[ordinal];
56                 if (ret == null) {
57                     ret = new CopyHistory[1 << VALUES.length];
58                     CACHE[ordinal] = ret;
59                 }
60             }
61         }
62
63         return ret;
64     }
65
66     private static CopyHistory cacheObject(final CopyType lastOperation, final int operations) {
67         final CopyHistory[] array = cacheArray(lastOperation);
68         CopyHistory ret = array[operations];
69         if (ret == null) {
70             synchronized (array) {
71                 ret = array[operations];
72                 if (ret == null) {
73                     ret = new CopyHistory(operations, lastOperation);
74                     array[operations] = ret;
75                 }
76             }
77         }
78
79         return ret;
80     }
81
82     public boolean contains(final CopyType type) {
83         return (operations & type.bit()) != 0;
84     }
85
86     public CopyType getLastOperation() {
87         return VALUES[lastOperation];
88     }
89
90     @VisibleForTesting
91     CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
92         final int newOperations = operations | toAppend.operations | typeOfCopy.bit();
93         if (newOperations == operations && typeOfCopy.ordinal() == lastOperation) {
94             return this;
95         }
96
97         return cacheObject(typeOfCopy, newOperations);
98     }
99
100     @Override
101     public int hashCode() {
102         return Integer.hashCode(operations | lastOperation << Short.SIZE);
103     }
104
105     @Override
106     public boolean equals(final Object obj) {
107         if (obj == this) {
108             return true;
109         }
110         if (!(obj instanceof CopyHistory)) {
111             return false;
112         }
113         final CopyHistory other = (CopyHistory) obj;
114         return operations == other.operations && lastOperation == other.lastOperation;
115     }
116 }