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][];
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.
27 * If we ever hit 6 types, the caching strategy needs to be revisited.
29 Verify.verify(VALUES.length < 6);
32 private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
34 private final short operations;
35 private final short lastOperation;
37 private CopyHistory(final int operations, final CopyType lastOperation) {
38 this.operations = (short) operations;
39 this.lastOperation = (short) lastOperation.ordinal();
42 public static CopyHistory original() {
46 public static CopyHistory of(final CopyType copyType, final CopyHistory copyHistory) {
47 return ORIGINAL.append(copyType, copyHistory);
50 private static CopyHistory[] cacheArray(final CopyType lastOperation) {
51 final int ordinal = lastOperation.ordinal();
52 CopyHistory[] ret = CACHE[ordinal];
54 synchronized (CACHE) {
57 ret = new CopyHistory[1 << VALUES.length];
66 private static CopyHistory cacheObject(final CopyType lastOperation, final int operations) {
67 final CopyHistory[] array = cacheArray(lastOperation);
68 CopyHistory ret = array[operations];
70 synchronized (array) {
71 ret = array[operations];
73 ret = new CopyHistory(operations, lastOperation);
74 array[operations] = ret;
82 public boolean contains(final CopyType type) {
83 return (operations & type.bit()) != 0;
86 public CopyType getLastOperation() {
87 return VALUES[lastOperation];
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) {
97 return cacheObject(typeOfCopy, newOperations);
101 public int hashCode() {
102 return Integer.hashCode(operations | lastOperation << Short.SIZE);
106 public boolean equals(final Object obj) {
110 if (!(obj instanceof CopyHistory)) {
113 final CopyHistory other = (CopyHistory) obj;
114 return operations == other.operations && lastOperation == other.lastOperation;