import org.checkerframework.checker.regex.qual.Regex;
import org.opendaylight.yangtools.concepts.Identifier;
import org.opendaylight.yangtools.concepts.WritableObject;
+import org.opendaylight.yangtools.yang.common.Uint32;
/**
* An OID, or ObjectIdentifier, as defined by ITU and ISO/IEC.
private static final Pattern CHECK_OID_PATTERN = Pattern.compile(CHECK_OID_REGEX);
private static final Pattern SPLIT_PATTERN = Pattern.compile(".", Pattern.LITERAL);
- private final int[] components;
+ private final int[] subIdentifiers;
- private ObjectIdentifier(final int[] components) {
- this.components = components;
+ private ObjectIdentifier(final int[] subIdentifiers) {
+ this.subIdentifiers = subIdentifiers;
}
/**
- * Create an {@link ObjectIdentifier} from its integer components.
+ * Create an {@link ObjectIdentifier} from its integer components. Each sub-identifier is interpreted as an unsigned
+ * integer.
*
- * @param components OID items
+ * @param subIdentifiers OID sub-identifiers
* @return An ObjectIdentifier.
*/
- public static ObjectIdentifier forComponents(final int... components) {
- return new ObjectIdentifier(components.clone());
+ public static ObjectIdentifier of(final int... subIdentifiers) {
+ return new ObjectIdentifier(subIdentifiers.clone());
}
/**
return new ObjectIdentifier(parseObjectId(str));
}
- public int[] getComponents() {
+ public int[] getSubIdentifiers() {
// Always make a defensive copy
- return components.clone();
+ return subIdentifiers.clone();
}
- public IntStream streamComponents() {
- return Arrays.stream(components);
+ public IntStream streamSubIdentifiers() {
+ return Arrays.stream(subIdentifiers);
}
/**
* @throws IOException If an I/O error is reported
*/
public static ObjectIdentifier readFrom(final DataInput in) throws IOException {
- final int count = in.readInt();
- checkArgument(count >= 0, "Illegal item count");
+ final int count = in.readUnsignedByte();
+ checkArgument(count >= 0 && count <= 128, "Illegal item count %s", count);
final int[] oid = new int[count];
for (int index = 0; index < count; ++index) {
@Override
public void writeTo(final DataOutput out) throws IOException {
- out.writeInt(components.length);
- for (int i : components) {
+ out.writeByte(subIdentifiers.length);
+ for (int i : subIdentifiers) {
out.writeInt(i);
}
}
@Override
public int hashCode() {
- return Arrays.hashCode(components);
+ return Arrays.hashCode(subIdentifiers);
}
@Override
public boolean equals(final Object obj) {
return this == obj || obj instanceof ObjectIdentifier
- && Arrays.equals(components, ((ObjectIdentifier) obj).components);
+ && Arrays.equals(subIdentifiers, ((ObjectIdentifier) obj).subIdentifiers);
}
@Override
public String toString() {
StringBuilder stringBuilder = new StringBuilder();
- stringBuilder.append(components[0]);
- for (int index = 1; index < components.length; index++) {
- stringBuilder.append('.').append(components[index]);
+ stringBuilder.append(subIdentifiers[0]);
+ for (int index = 1; index < subIdentifiers.length; index++) {
+ stringBuilder.append('.').append(subIdentifiers[index]);
}
return stringBuilder.toString();
}
checkArgument(CHECK_OID_PATTERN.matcher(objectId).matches(), "Wrong format for OID: '%s'", objectId);
final String[] splitOid = SPLIT_PATTERN.split(objectId);
+ checkArgument(splitOid.length <= 128, "Object Identifier can have at most 128 sub-identifiers");
+
final int[] oid = new int[splitOid.length];
for (int index = 0; index < splitOid.length; index ++) {
- oid[index] = Integer.parseInt(splitOid[index]);
+ oid[index] = Uint32.valueOf(splitOid[index]).intValue();
}
return oid;
}