Make CopyHistory implement CopyableNode
[yangtools.git] / yang / yang-parser-spi / src / main / java / org / opendaylight / yangtools / yang / parser / spi / meta / CopyHistory.java
index 10eb0fa252dee83ab45005fe449642cd034f68e4..5b039d6e239ee417a7acb7111dcf37000ab2123e 100644 (file)
@@ -9,14 +9,19 @@ package org.opendaylight.yangtools.yang.parser.spi.meta;
 
 import com.google.common.annotations.Beta;
 import com.google.common.annotations.VisibleForTesting;
+import com.google.common.base.MoreObjects;
 import com.google.common.base.Verify;
+import java.util.Arrays;
+import java.util.stream.Collectors;
 import org.opendaylight.yangtools.concepts.Immutable;
+import org.opendaylight.yangtools.yang.model.api.CopyableNode;
 
 @Beta
-public final class CopyHistory implements Immutable {
+public final class CopyHistory implements Immutable, CopyableNode {
     private static final CopyType[] VALUES = CopyType.values();
 
     private static final CopyHistory[][] CACHE = new CopyHistory[VALUES.length][];
+
     static {
         /*
          * Cache size is dependent on number of items in CopyType, it costs N * 2^N objects.
@@ -30,6 +35,10 @@ public final class CopyHistory implements Immutable {
     }
 
     private static final CopyHistory ORIGINAL = cacheObject(CopyType.ORIGINAL, CopyType.ORIGINAL.bit());
+    private static final int IS_ADDED_BY_USES_BITS =
+        CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_USES.bit();
+    private static final int IS_AUGMENTING_BITS =
+        CopyType.ADDED_BY_USES_AUGMENTATION.bit() | CopyType.ADDED_BY_AUGMENTATION.bit();
 
     private final short operations;
     private final short lastOperation;
@@ -79,6 +88,8 @@ public final class CopyHistory implements Immutable {
         return ret;
     }
 
+    @VisibleForTesting
+    // FIXME: 7.0.0: hide this method
     public boolean contains(final CopyType type) {
         return (operations & type.bit()) != 0;
     }
@@ -87,6 +98,18 @@ public final class CopyHistory implements Immutable {
         return VALUES[lastOperation];
     }
 
+    @Override
+    @Deprecated
+    public boolean isAugmenting() {
+        return (operations & IS_AUGMENTING_BITS) != 0;
+    }
+
+    @Override
+    @Deprecated
+    public boolean isAddedByUses() {
+        return (operations & IS_ADDED_BY_USES_BITS) != 0;
+    }
+
     @VisibleForTesting
     CopyHistory append(final CopyType typeOfCopy, final CopyHistory toAppend) {
         final int newOperations = operations | toAppend.operations | typeOfCopy.bit();
@@ -113,4 +136,12 @@ public final class CopyHistory implements Immutable {
         final CopyHistory other = (CopyHistory) obj;
         return operations == other.operations && lastOperation == other.lastOperation;
     }
+
+    @Override
+    public String toString() {
+        return MoreObjects.toStringHelper(this).add("lastOperation", getLastOperation())
+                .add("operations", Arrays.stream(VALUES).filter(value -> (value.bit() & operations) != 0)
+                    .collect(Collectors.toList()))
+                .toString();
+    }
 }