From e08b7ba20e6c29f50ec33f342d4568abde79339c Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Fri, 29 Jul 2016 14:30:40 +0200 Subject: [PATCH] BUG-865: remove BitImpl Introduce a BitBuilder and remove public BitImpl class. Change-Id: Icf6297192cba21773a89b87bae817ebb0eebad9e Signed-off-by: Robert Varga --- .../yang/model/util/type/BitBuilder.java | 75 ++++++++++++ .../yang/model/util/{ => type}/BitImpl.java | 36 +++--- .../yang/model/util/BitImplTest.java | 114 ------------------ .../yang/model/util/type/BitImplTest.java | 79 ++++++++++++ .../yang/model/util/type/TypeTest.java | 9 +- ...tsSpecificationEffectiveStatementImpl.java | 12 +- 6 files changed, 181 insertions(+), 144 deletions(-) create mode 100644 yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitBuilder.java rename yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/{ => type}/BitImpl.java (71%) delete mode 100644 yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java create mode 100644 yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/BitImplTest.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 index 0000000000..7e709966f8 --- /dev/null +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitBuilder.java @@ -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, Mutable { + private final SchemaPath schemaPath; + private final Long position; + + private List 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 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); + } +} diff --git a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BitImpl.java b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitImpl.java 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 1b03a65734..f7edfe22cf 100644 --- a/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/BitImpl.java +++ b/yang/yang-model-util/src/main/java/org/opendaylight/yangtools/yang/model/util/type/BitImpl.java @@ -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 unknownNodes; private final SchemaPath schemaPath; private final String description; private final String reference; private final Status status; - private final List 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 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 index 274bc0d197..0000000000 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/BitImplTest.java +++ /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 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 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 index 0000000000..5e640ae744 --- /dev/null +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/BitImplTest.java @@ -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()); + } +} diff --git a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java index 8d8ed89447..fd6985bcae 100644 --- a/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java +++ b/yang/yang-model-util/src/test/java/org/opendaylight/yangtools/yang/model/util/type/TypeTest.java @@ -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 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(); } diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java index b688f18f46..03d85d0c28 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/stmt/rfc6020/effective/type/BitsSpecificationEffectiveStatementImpl.java @@ -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, -- 2.36.6