BUG-865: remove BitImpl 80/42780/6
authorRobert Varga <rovarga@cisco.com>
Fri, 29 Jul 2016 12:30:40 +0000 (14:30 +0200)
committerTony Tkacik <ttkacik@cisco.com>
Mon, 1 Aug 2016 09:19:11 +0000 (09:19 +0000)
Introduce a BitBuilder and remove public BitImpl class.

Change-Id: Icf6297192cba21773a89b87bae817ebb0eebad9e
Signed-off-by: Robert Varga <rovarga@cisco.com>
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitBuilder.java [new file with mode: 0644]
yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitImpl.java [moved from yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BitImpl.java with 71% similarity]
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java [deleted file]
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/BitImplTest.java [new file with mode: 0644]
yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java

diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitBuilder.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitBuilder.java
new file mode 100644 (file)
index 0000000..7e70996
--- /dev/null
@@ -0,0 +1,75 @@
+/*
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util.type;
+
+import com.google.common.annotations.Beta;
+import com.google.common.base.Preconditions;
+import com.google.common.collect.ImmutableList;
+import java.util.Collection;
+import java.util.List;
+import org.opendaylight.yangtools.concepts.Builder;
+import org.opendaylight.yangtools.concepts.Mutable;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
+import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
+
+/**
+ * Utility builder for {@link Bit} instances.
+ *
+ * @author Robert Varga
+ */
+@Beta
+public final class BitBuilder implements Builder<Bit>, Mutable {
+    private final SchemaPath schemaPath;
+    private final Long position;
+
+    private List<UnknownSchemaNode> unknownSchemaNodes = ImmutableList.of();
+    private Status status = Status.CURRENT;
+    private String description;
+    private String reference;
+
+    private BitBuilder(final SchemaPath schemaPath, final Long position) {
+        this.schemaPath = Preconditions.checkNotNull(schemaPath);
+        this.position = Preconditions.checkNotNull(position);
+    }
+
+    public static BitBuilder create(final SchemaPath schemaPath, final Long position) {
+        return new BitBuilder(schemaPath, position);
+    }
+
+    public BitBuilder setDescription(final String description) {
+        this.description = description;
+        return this;
+    }
+
+    public BitBuilder setReference(final String reference) {
+        this.reference = reference;
+        return this;
+    }
+
+    public BitBuilder setStatus(final Status status) {
+        this.status = Preconditions.checkNotNull(status);
+        return this;
+    }
+
+    public BitBuilder setUnknownSchemaNodes(final Collection<UnknownSchemaNode> unknownSchemaNodes) {
+        this.unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodes);
+        return this;
+    }
+
+    public BitBuilder setUnknownSchemaNodes(final UnknownSchemaNode... unknownSchemaNodes) {
+        this.unknownSchemaNodes = ImmutableList.copyOf(unknownSchemaNodes);
+        return this;
+    }
+
+    @Override
+    public Bit build() {
+        return new BitImpl(schemaPath, position, description, reference, status, unknownSchemaNodes);
+    }
+}
similarity index 71%
rename from yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BitImpl.java
rename to yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitImpl.java
index 1b03a65734654f1f197d5c7f13a0d7bedb722713..f7edfe22cf665bb6c951ae4cf1e5c989d12a469b 100644 (file)
@@ -1,14 +1,13 @@
 /*
- * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
+ * Copyright (c) 2016 Cisco Systems, Inc. and others.  All rights reserved.
  *
  * This program and the accompanying materials are made available under the
  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
  * and is available at http://www.eclipse.org/legal/epl-v10.html
  */
-package org.opendaylight.yangtools.yang.model.util;
+package org.opendaylight.yangtools.yang.model.util.type;
 
 import com.google.common.base.Preconditions;
-import com.google.common.collect.ImmutableList;
 import java.util.List;
 import java.util.Objects;
 import org.opendaylight.yangtools.concepts.Immutable;
@@ -16,36 +15,29 @@ import org.opendaylight.yangtools.yang.common.QName;
 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
 import org.opendaylight.yangtools.yang.model.api.Status;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
-import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 
-public final class BitImpl implements BitsTypeDefinition.Bit, Immutable {
-    private final Long position;
-    private final QName qname;
+final class BitImpl implements Bit, Immutable {
+    private final List<UnknownSchemaNode> unknownNodes;
     private final SchemaPath schemaPath;
     private final String description;
     private final String reference;
     private final Status status;
-    private final List<UnknownSchemaNode> unknownNodes;
+    private final Long position;
 
-    public BitImpl(final Long position, final QName qname, final SchemaPath schemaPath, final String description,
+    BitImpl(final SchemaPath schemaPath, final Long position, final String description,
             final String reference, final Status status, final List<UnknownSchemaNode> unknownNodes) {
-        this.position = Preconditions.checkNotNull(position, "Position should not be null");
-        this.qname = Preconditions.checkNotNull(qname, "QName should not be null");
         this.schemaPath = Preconditions.checkNotNull(schemaPath, "Schema Path should not be null");
+        this.position = Preconditions.checkNotNull(position, "Position should not be null");
         this.description = description;
         this.reference = reference;
-        this.status = status;
-        if (unknownNodes != null) {
-            this.unknownNodes = unknownNodes;
-        } else {
-            this.unknownNodes = ImmutableList.of();
-        }
+        this.status = Preconditions.checkNotNull(status);
+        this.unknownNodes = Preconditions.checkNotNull(unknownNodes);
     }
 
     @Override
     public QName getQName() {
-        return qname;
+        return schemaPath.getLastComponent();
     }
 
     @Override
@@ -80,14 +72,14 @@ public final class BitImpl implements BitsTypeDefinition.Bit, Immutable {
 
     @Override
     public String getName() {
-        return qname.getLocalName();
+        return getQName().getLocalName();
     }
 
     @Override
     public int hashCode() {
         final int prime = 31;
         int result = 1;
-        result = prime * result + qname.hashCode();
+        result = prime * result + getQName().hashCode();
         result = prime * result + schemaPath.hashCode();
         result = prime * result + position.hashCode();
         result = prime * result + unknownNodes.hashCode();
@@ -106,12 +98,12 @@ public final class BitImpl implements BitsTypeDefinition.Bit, Immutable {
             return false;
         }
         Bit other = (Bit) obj;
-        return Objects.equals(qname, other.getQName()) && Objects.equals(schemaPath, other.getPath());
+        return Objects.equals(schemaPath, other.getPath());
     }
 
     @Override
     public String toString() {
-        return Bit.class.getSimpleName() + "[name=" + qname.getLocalName() + ", position=" + position + "]";
+        return Bit.class.getSimpleName() + "[name=" + getQName().getLocalName() + ", position=" + position + "]";
     }
 
 }
diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java
deleted file mode 100644 (file)
index 274bc0d..0000000
+++ /dev/null
@@ -1,114 +0,0 @@
-/*
- * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
- *
- * This program and the accompanying materials are made available under the
- * terms of the Eclipse Public License v1.0 which accompanies this distribution,
- * and is available at http://www.eclipse.org/legal/epl-v10.html
- */
-package org.opendaylight.yangtools.yang.model.util;
-
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.fail;
-
-import java.net.URI;
-import java.net.URISyntaxException;
-import java.util.ArrayList;
-import java.util.Collections;
-import java.util.Date;
-import java.util.List;
-
-import org.junit.Test;
-import org.opendaylight.yangtools.yang.common.QName;
-import org.opendaylight.yangtools.yang.model.api.SchemaPath;
-import org.opendaylight.yangtools.yang.model.api.Status;
-
-public class BitImplTest {
-
-    @Test
-    // We're testing equals()
-    @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
-    public void test() {
-
-        // hashCode method test
-        URI uriA = null;
-        URI uriA1 = null;
-        URI uriA2 = null;
-        URI uriB = null;
-        URI uriB1 = null;
-        URI uriB2 = null;
-        try {
-            uriA = new URI("some:uriA");
-            uriA1 = new URI("some:uriA1");
-            uriA2 = new URI("some:uriA2");
-            uriB = new URI("some:uriB");
-            uriB1 = new URI("some:uriB1");
-            uriB2 = new URI("some:uriB2");
-        } catch (URISyntaxException e) {
-            fail("Not all required uri variables were instantiated.");
-        }
-        QName qnameA = QName.create(uriA, new Date(5000000), "some name");
-
-        QName qnameA1 = QName.create(uriA1, new Date(6000000), "some nameA1");
-        QName qnameA2 = QName.create(uriA2, new Date(7000000), "some nameA2");
-        List<QName> qnamesA = new ArrayList<>();
-        qnamesA.add(qnameA1);
-        qnamesA.add(qnameA2);
-        SchemaPath schemaPathA = SchemaPath.create(qnamesA, true);
-
-        QName qnameB = QName.create(uriB, new Date(5000000), "some name");
-
-        QName qnameB1 = QName.create(uriB1, new Date(6000000), "some nameB1");
-        QName qnameB2 = QName.create(uriB2, new Date(7000000), "some nameB2");
-        List<QName> qnamesB = new ArrayList<>();
-        qnamesB.add(qnameB1);
-        qnamesB.add(qnameB2);
-        SchemaPath schemaPathB = SchemaPath.create(qnamesB, true);
-
-        BitImpl biB;
-        BitImpl biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-
-        assertEquals("biA should equals to itsefl", biA, biA);
-        assertFalse("biA shouldn't equal to null", biA.equals(null));
-        assertFalse("biA shouldn't equal to object of other type", biA.equals("str"));
-
-        biA = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
-        assertEquals("biA should equal to biB", biA, biB);
-
-        biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameB, schemaPathA, "description", "reference", Status.CURRENT, null);
-        assertFalse("biA shouldn't equal to biB", biA.equals(biB));
-
-        // // test schemaPath
-        biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
-        assertFalse("biA shouldn't equal to biB", biA.equals(biB));
-
-        biA = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
-        assertEquals("biA should equal to biB", biA, biB);
-
-        biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameA, schemaPathB, "description", "reference", Status.CURRENT, null);
-        assertFalse("biA shouldn't equal to biB", biA.equals(biB));
-
-        biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-        biB = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT, null);
-        assertEquals("biA should equal to biB", biA, biB);
-
-        biA = new BitImpl(55L, qnameA, schemaPathA, "description", "reference", Status.CURRENT,null);
-
-        // test of getter methods
-        assertEquals("Incorrect value for qname.", qnameA, biA.getQName());
-        assertEquals("Incorrect value for schema path.", schemaPathA, biA.getPath());
-        assertEquals("Incorrect value for description.", "description", biA.getDescription());
-        assertEquals("Incorrect value for reference.", "reference", biA.getReference());
-        assertEquals("Incorrect value for status.", Status.CURRENT, biA.getStatus());
-        assertEquals("Incorrect value for unknown nodes.", Collections.emptyList(), biA.getUnknownSchemaNodes());
-
-        // test of toString method
-        assertEquals("toString method doesn't return correct value", "Bit[name=some name, position=55]", biA.toString());
-
-    }
-}
diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/BitImplTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/BitImplTest.java
new file mode 100644 (file)
index 0000000..5e640ae
--- /dev/null
@@ -0,0 +1,79 @@
+/*
+ * Copyright (c) 2014 Cisco Systems, Inc. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.yangtools.yang.model.util.type;
+
+import static java.util.Collections.emptyList;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import java.net.URI;
+import java.net.URISyntaxException;
+import java.util.Date;
+import org.junit.Test;
+import org.opendaylight.yangtools.yang.common.QName;
+import org.opendaylight.yangtools.yang.model.api.SchemaPath;
+import org.opendaylight.yangtools.yang.model.api.Status;
+
+public class BitImplTest {
+
+    @Test
+    // We're testing equals()
+    @SuppressWarnings({"ObjectEqualsNull", "EqualsBetweenInconvertibleTypes"})
+    public void test() throws URISyntaxException {
+
+        // hashCode method test
+        final URI uriA1 = new URI("some:uriA1");
+        final URI uriA2 = new URI("some:uriA2");
+        final URI uriB1 = new URI("some:uriB1");
+        final URI uriB2 = new URI("some:uriB2");
+
+        QName qnameA1 = QName.create(uriA1, new Date(6000000), "some nameA1");
+        QName qnameA2 = QName.create(uriA2, new Date(7000000), "some nameA2");
+        SchemaPath schemaPathA = SchemaPath.create(true, qnameA1, qnameA2);
+
+        QName qnameB1 = QName.create(uriB1, new Date(6000000), "some nameB1");
+        QName qnameB2 = QName.create(uriB2, new Date(7000000), "some nameB2");
+        SchemaPath schemaPathB = SchemaPath.create(true, qnameB1, qnameB2);
+
+        BitImpl biB;
+        BitImpl biA = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+
+        assertEquals("biA should equals to itsefl", biA, biA);
+        assertFalse("biA shouldn't equal to null", biA.equals(null));
+        assertFalse("biA shouldn't equal to object of other type", biA.equals("str"));
+
+         // // test schemaPath
+        biA = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+        biB = new BitImpl(schemaPathB, 55L, "description", "reference", Status.CURRENT, emptyList());
+        assertFalse("biA shouldn't equal to biB", biA.equals(biB));
+
+        biA = new BitImpl(schemaPathB, 55L, "description", "reference", Status.CURRENT, emptyList());
+        biB = new BitImpl(schemaPathB, 55L, "description", "reference", Status.CURRENT, emptyList());
+        assertEquals("biA should equal to biB", biA, biB);
+
+        biA = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+        biB = new BitImpl(schemaPathB, 55L, "description", "reference", Status.CURRENT, emptyList());
+        assertFalse("biA shouldn't equal to biB", biA.equals(biB));
+
+        biA = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+        biB = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+        assertEquals("biA should equal to biB", biA, biB);
+
+        biA = new BitImpl(schemaPathA, 55L, "description", "reference", Status.CURRENT, emptyList());
+
+        // test of getter methods
+        assertEquals("Incorrect value for qname.", qnameA2, biA.getQName());
+        assertEquals("Incorrect value for schema path.", schemaPathA, biA.getPath());
+        assertEquals("Incorrect value for description.", "description", biA.getDescription());
+        assertEquals("Incorrect value for reference.", "reference", biA.getReference());
+        assertEquals("Incorrect value for status.", Status.CURRENT, biA.getStatus());
+        assertEquals("Incorrect value for unknown nodes.", emptyList(), biA.getUnknownSchemaNodes());
+
+        // test of toString method
+        assertEquals("toString method doesn't return correct value", "Bit[name=some nameA2, position=55]", biA.toString());
+    }
+}
index 8d8ed894476aca4aee1d206877a1645b0eee5d29..fd6985bcaeb52cf4ce8324604014e2f3570da931 100644 (file)
@@ -26,6 +26,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.UnknownSchemaNode;
 import org.opendaylight.yangtools.yang.model.api.type.BinaryTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
+import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
 import org.opendaylight.yangtools.yang.model.api.type.EnumTypeDefinition.EnumPair;
 import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition;
@@ -36,7 +37,6 @@ import org.opendaylight.yangtools.yang.model.api.type.RangeConstraint;
 import org.opendaylight.yangtools.yang.model.api.type.StringTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.UnsignedIntegerTypeDefinition;
 import org.opendaylight.yangtools.yang.model.util.BaseConstraints;
-import org.opendaylight.yangtools.yang.model.util.BitImpl;
 import org.opendaylight.yangtools.yang.model.util.RevisionAwareXPathImpl;
 import org.opendaylight.yangtools.yang.model.util.UnresolvedNumber;
 
@@ -44,8 +44,8 @@ public class TypeTest {
     private static final QName Q_NAME = QName.create("test.namespace", "2016-01-01", "test-name");
     private static final SchemaPath SCHEMA_PATH = SchemaPath.create(true, Q_NAME);
     private static final RevisionAwareXPath REVISION_AWARE_XPATH = new RevisionAwareXPathImpl("/test", true);
-    private static final BitImpl BIT_A = new BitImpl(55L, Q_NAME, SCHEMA_PATH, "description", "reference", Status.CURRENT,
-            null);
+    private static final Bit BIT_A = BitBuilder.create(SCHEMA_PATH, 55L).setDescription("description")
+            .setReference("reference").build();
     private static final Optional<String> ABSENT = Optional.absent();
 
     @Test
@@ -466,9 +466,8 @@ public class TypeTest {
         final BitsTypeBuilder bitsTypeBuilder = BaseTypes.bitsTypeBuilder(SCHEMA_PATH);
         final QName qName = QName.create("test.namespace.1", "2016-01-02", "test-name-1");
         final SchemaPath schemaPath = SchemaPath.create(true, qName);
-        final BitImpl bitB = new BitImpl(55L, qName, schemaPath, null, null, Status.CURRENT, null);
         bitsTypeBuilder.addBit(BIT_A);
-        bitsTypeBuilder.addBit(bitB);
+        bitsTypeBuilder.addBit(BitBuilder.create(schemaPath, 55L).build());
         bitsTypeBuilder.build();
     }
 
index b688f18f46d00c8fb0d6933940b1eba63d358ecf..03d85d0c2886646afdfca348565158428c4dd802 100644 (file)
@@ -12,8 +12,8 @@ import org.opendaylight.yangtools.yang.model.api.stmt.TypeEffectiveStatement;
 import org.opendaylight.yangtools.yang.model.api.stmt.TypeStatement.BitsSpecification;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition;
 import org.opendaylight.yangtools.yang.model.api.type.BitsTypeDefinition.Bit;
-import org.opendaylight.yangtools.yang.model.util.BitImpl;
 import org.opendaylight.yangtools.yang.model.util.type.BaseTypes;
+import org.opendaylight.yangtools.yang.model.util.type.BitBuilder;
 import org.opendaylight.yangtools.yang.model.util.type.BitsTypeBuilder;
 import org.opendaylight.yangtools.yang.parser.spi.meta.StmtContext;
 import org.opendaylight.yangtools.yang.parser.spi.source.SourceException;
@@ -45,8 +45,14 @@ public final class BitsSpecificationEffectiveStatementImpl extends
                             "Bit %s must have a position statement", b);
                     }
 
-                    b = new BitImpl(newPos, b.getQName(), b.getPath(), b.getDescription(), b.getReference(),
-                        b.getStatus(), b.getUnknownSchemaNodes());
+                    final BitBuilder bitBuilder = BitBuilder.create(b.getPath(), newPos)
+                            .setDescription(b.getDescription()).setReference(b.getReference())
+                            .setUnknownSchemaNodes(b.getUnknownSchemaNodes());
+                    if (b.getStatus() != null) {
+                        bitBuilder.setStatus(b.getStatus());
+                    }
+
+                    b = bitBuilder.build();
                 }
 
                 SourceException.throwIf(b.getPosition() < 0L && b.getPosition() > 4294967295L,