Introduce a BitBuilder and remove public BitImpl class.
Change-Id: Icf6297192cba21773a89b87bae817ebb0eebad9e
Signed-off-by: Robert Varga <rovarga@cisco.com>
--- /dev/null
+/*
+ * 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);
+ }
+}
/*
- * 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;
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
@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();
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 + "]";
}
}
+++ /dev/null
-/*
- * 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());
-
- }
-}
--- /dev/null
+/*
+ * 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());
+ }
+}
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;
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;
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
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();
}
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;
"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,