2 * Copyright (c) 2016 Cisco Systems, Inc. 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.parser.spi.meta;
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;
16 public final class CopyHistory implements Immutable {
17 private static final CopyType[] VALUES = CopyType.values();
19 private static final CopyHistory[][] CACHE = new CopyHistory[VALUES.length][];
23 * Cache size is dependent on number of items in CopyType, it costs N * 2^N objects.
24 * For 4 types that boils down to 4 * 16 = 64 objects.
25 * For 5 types that boils down to 5 * 32 = 160 objects.
26 * For 6 types that boils down to 6 * 64 = 384 objects.
28 * If we ever hit 6 types, the caching strategy needs to be revisited.
30 Verify.verify(VALUES.length < 6);
33 private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
35 private final short operations;
36 private final short lastOperation;
38 private CopyHistory(final int operations, final CopyType lastOperation) {
39 this.operations = (short) operations;
40 this.lastOperation = (short) lastOperation.ordinal();
43 public static CopyHistory original() {
47 public static CopyHistory of(final CopyType copyType, final CopyHistory copyHistory) {
48 return ORIGINAL.append(copyType, copyHistory);
51 private static CopyHistory[] cacheArray(final CopyType lastOperation) {
52 final int ordinal = lastOperation.ordinal();
53 CopyHistory[] ret = CACHE[ordinal];
55 synchronized (CACHE) {
58 ret = new CopyHistory[1 << VALUES.length];
67 private static CopyHistory cacheObject(final CopyType lastOperation, final int operations) {
68 final CopyHistory[] array = cacheArray(lastOperation);
69 CopyHistory ret = array[operations];
71 synchronized (array) {
72 ret = array[operations];
74 ret = new CopyHistory(operations, lastOperation);
75 array[operations] = ret;
83 public boolean contains(final CopyType type) {
84 return (operations & type.bit()) != 0;
87 public CopyType getLastOperation() {
88 return VALUES[lastOperation];
92 CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
93 final int newOperations = operations | toAppend.operations | typeOfCopy.bit();
94 if (newOperations == operations && typeOfCopy.ordinal() == lastOperation) {
98 return cacheObject(typeOfCopy, newOperations);
102 public int hashCode() {
103 return Integer.hashCode(operations | lastOperation << Short.SIZE);
107 public boolean equals(final Object obj) {
111 if (!(obj instanceof CopyHistory)) {
114 final CopyHistory other = (CopyHistory) obj;
115 return operations == other.operations && lastOperation == other.lastOperation;