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.MoreObjects;
13 import com.google.common.base.Verify;
14 import java.util.Arrays;
15 import java.util.stream.Collectors;
16 import org.opendaylight.yangtools.concepts.Immutable;
17 import org.opendaylight.yangtools.yang.model.api.CopyableNode;
20 public final class CopyHistory implements Immutable, CopyableNode {
21 private static final CopyType[] VALUES = CopyType.values();
23 private static final CopyHistory[][] CACHE = new CopyHistory[VALUES.length][];
27 * Cache size is dependent on number of items in CopyType, it costs N * 2^N objects.
28 * For 4 types that boils down to 4 * 16 = 64 objects.
29 * For 5 types that boils down to 5 * 32 = 160 objects.
30 * For 6 types that boils down to 6 * 64 = 384 objects.
32 * If we ever hit 6 types, the caching strategy needs to be revisited.
34 Verify.verify(VALUES.length < 6);
37 private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
38 private static final int IS_ADDED_BY_USES_BITS =
39 CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_USES.bit();
40 private static final int IS_AUGMENTING_BITS =
41 CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_AUGMENTATION.bit();
43 private final short operations;
44 private final short lastOperation;
46 private CopyHistory(final int operations, final CopyType lastOperation) {
47 this.operations = (short) operations;
48 this.lastOperation = (short) lastOperation.ordinal();
51 public static CopyHistory original() {
55 public static CopyHistory of(final CopyType copyType, final CopyHistory copyHistory) {
56 return ORIGINAL.append(copyType, copyHistory);
59 private static CopyHistory[] cacheArray(final CopyType lastOperation) {
60 final int ordinal = lastOperation.ordinal();
61 CopyHistory[] ret = CACHE[ordinal];
63 synchronized (CACHE) {
66 ret = new CopyHistory[1 << VALUES.length];
75 private static CopyHistory cacheObject(final CopyType lastOperation, final int operations) {
76 final CopyHistory[] array = cacheArray(lastOperation);
77 CopyHistory ret = array[operations];
79 synchronized (array) {
80 ret = array[operations];
82 ret = new CopyHistory(operations, lastOperation);
83 array[operations] = ret;
91 public CopyType getLastOperation() {
92 return VALUES[lastOperation];
97 public boolean isAugmenting() {
98 return (operations & IS_AUGMENTING_BITS) != 0;
103 public boolean isAddedByUses() {
104 return (operations & IS_ADDED_BY_USES_BITS) != 0;
108 boolean contains(final CopyType type) {
109 return (operations & type.bit()) != 0;
113 CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
114 final int newOperations = operations | toAppend.operations | typeOfCopy.bit();
115 if (newOperations == operations && typeOfCopy.ordinal() == lastOperation) {
119 return cacheObject(typeOfCopy, newOperations);
123 public int hashCode() {
124 return Integer.hashCode(operations | lastOperation << Short.SIZE);
128 public boolean equals(final Object obj) {
132 if (!(obj instanceof CopyHistory)) {
135 final CopyHistory other = (CopyHistory) obj;
136 return operations == other.operations && lastOperation == other.lastOperation;
140 public String toString() {
141 return MoreObjects.toStringHelper(this).add("lastOperation", getLastOperation())
142 .add("operations", Arrays.stream(VALUES).filter(value -> (value.bit() & operations) != 0)
143 .collect(Collectors.toList()))