net.percederberg.mibble

## Class MibType

- java.lang.Object
- - net.percederberg.mibble.MibType

- Direct Known Subclasses:
    BitSetType, BooleanType, ChoiceType, ElementType, IntegerType, NullType, ObjectIdentifierType, RealType, SequenceOfType, SequenceType, SnmpType, StringType, TypeReference

      public abstract class MibType
      extends java.lang.Object

  The base MIB type class. There are two categories of MIB types extending this class, primitive ASN.1 type and SNMP macro types. The primitive types are used in SNMP for transferring data on the wire. The SNMP macro types are used in the MIB files for adding additional information to the primitive types or values, such as descriptions and similar. Most of the SNMP macro types only support object identifier values, and can only be used at the top level. The primitive types support whatever values are appropriate for the specific type, and are normally used inside the SNMP macro types in a MIB file.

  The best way to extract the specific type information from a MIB type is to check the type instance and then cast the MibType object to the corresponding subtype. Each subtype have very different properties, which is why the API in this class is rather limited. The example below shows some skeleton code for extracting type information.

          if (type instanceof SnmpObjectType) {
              objectType = (SnmpObjectType) type;
              ...
          }

  Another way to check which type is at hand, is to query the type tags with the hasTag() method. In this way it is possible to distinguish between types using the same or a similar primitive ASN.1 type representation (such as DisplayString and IpAddress). This should normally be done in order to create a correct BER- or DER-encoding of the type. The example below illustrates how this could be done.

          tag = type.getTag();
          if (tag.getCategory() == MibTypeTag.UNIVERSAL) {
              // Set BER and DER identifier bits 8 & 7 to 00
          } else if (tag.getCategory() == MibTypeTag.APPLICATION) {
              // Set BER and DER identifier bits 8 & 7 to 01
          }
          ...
          if (!type.isPrimitive()) {
              // Set BER and DER constructed bit
          }

  Since:
    2.0

- - ### Constructor Summary

    Constructors
    Modifier | Constructor and Description
    --- | ---
    `protected` | `MibType(java.lang.String name, boolean primitive)` Creates a new MIB type instance.

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `MibType` | `createReference()` Creates a type reference to this type.
    `MibType` | `createReference(java.util.ArrayList<?> values)` Creates a constrained type reference to this type.
    `MibType` | `createReference(Constraint constraint)` Creates a constrained type reference to this type.
    `java.lang.String` | `getComment()` Returns the type comment.
    `java.lang.String` | `getName()` Returns the type name.
    `MibTypeSymbol` | `getReferenceSymbol()` Returns the type reference symbol.
    `MibTypeTag` | `getTag()` Returns the type tag.
    `boolean` | `hasReferenceTo(java.lang.String name)` Checks if this type referenced the specified type symbol.
    `boolean` | `hasReferenceTo(java.lang.String module, java.lang.String name)` Checks if this type referenced the specified type symbol.
    `boolean` | `hasTag(int category, int value)` Checks if this type has a specific type tag.
    `boolean` | `hasTag(MibTypeTag tag)` Checks if this type has a specific type tag.
    `abstract MibType` | `initialize(MibSymbol symbol, MibLoaderLog log)` Initializes the MIB type.
    `abstract boolean` | `isCompatible(MibValue value)` Checks if the specified value is compatible with this type.
    `boolean` | `isPrimitive()` Checks if this type represents a primitive type.
    `void` | `setReferenceSymbol(MibTypeSymbol symbol)` Sets the type reference symbol.
    `void` | `setTag(boolean implicit, MibTypeTag tag)` Sets the type tag.
    `java.lang.String` | `toString()` Returns a string representation of this object.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait`

- - ### Constructor Detail

    - #### MibType

          protected MibType(java.lang.String name,
                 boolean primitive)

      Creates a new MIB type instance.
      Parameters:
        `name` - the type name
        `primitive` - the primitive type flag

  - ### Method Detail

    - #### initialize

          public abstract MibType initialize(MibSymbol symbol,
                           MibLoaderLog log)
                                      throws MibException

      Initializes the MIB type. This will remove all levels of indirection present, such as references to types or values. No information is lost by this operation. This method may modify this object as a side-effect, and will return the basic type.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Parameters:
        `symbol` - the MIB symbol containing this type
        `log` - the MIB loader log
      Returns:
        the basic MIB type
      Throws:
        `MibException` - if an error was encountered during the initialization
      Since:
        2.2

    - #### createReference

          public MibType createReference()
                                  throws java.lang.UnsupportedOperationException

      Creates a type reference to this type. The type reference is normally an identical type, but with the primitive flag set to false. Only certain types support being referenced, and the default implementation of this method throws an exception.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Returns:
        the MIB type reference
      Throws:
        `java.lang.UnsupportedOperationException` - if a type reference couldn't be created
      Since:
        2.2

    - #### createReference

          public MibType createReference(Constraint constraint)
                                  throws java.lang.UnsupportedOperationException

      Creates a constrained type reference to this type. The type reference is normally an identical type, but with the primitive flag set to false. Only certain types support being referenced, and the default implementation of this method throws an exception.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Parameters:
        `constraint` - the type constraint
      Returns:
        the MIB type reference
      Throws:
        `java.lang.UnsupportedOperationException` - if a type reference couldn't be created with constraints
      Since:
        2.2

    - #### createReference

          public MibType createReference(java.util.ArrayList<?> values)
                                  throws java.lang.UnsupportedOperationException

      Creates a constrained type reference to this type. The type reference is normally an identical type, but with the primitive flag set to false. Only certain types support being referenced, and the default implementation of this method throws an exception.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Parameters:
        `values` - the type value symbols
      Returns:
        the MIB type reference
      Throws:
        `java.lang.UnsupportedOperationException` - if a type reference couldn't be created with value constraints
      Since:
        2.2

    - #### isCompatible

          public abstract boolean isCompatible(MibValue value)

      Checks if the specified value is compatible with this type. A value is compatible if it has a type that matches this one and a value that satisfies all constraints.
      Parameters:
        `value` - the value to check
      Returns:
        true if the value is compatible, or false otherwise

    - #### isPrimitive

          public boolean isPrimitive()

      Checks if this type represents a primitive type. The primitive types are the basic building blocks of the ASN.1 type system. By defining new types (that may be identical to a primitive type), the new type looses it's primitive status.
      Returns:
        true if this type represents a primitive type, or false otherwise
      Since:
        2.2

    - #### hasTag

          public boolean hasTag(MibTypeTag tag)

      Checks if this type has a specific type tag. This method will check the whole type tag chain.
      Parameters:
        `tag` - the type tag to search for
      Returns:
        true if the specified type tag was present, or false otherwise
      Since:
        2.2
      See Also:
        `hasTag(int, int)`

    - #### hasTag

          public boolean hasTag(int category,
                       int value)

      Checks if this type has a specific type tag. This method will check the whole type tag chain.
      Parameters:
        `category` - the tag category to search for
        `value` - the tag value to search for
      Returns:
        true if the specified type tag was present, or false otherwise
      Since:
        2.2
      See Also:
        `hasTag(MibTypeTag)`

    - #### hasReferenceTo

          public boolean hasReferenceTo(java.lang.String name)

      Checks if this type referenced the specified type symbol. This method should be avoided if possible, as it is much better to rely on type tags to distinguish between two types with the same base type (such as DisplayString and IpAddress).
      Parameters:
        `name` - the type symbol name
      Returns:
        true if this type was a reference to the symbol, or false otherwise
      Since:
        2.2
      See Also:
        `hasTag(int, int)`, `hasTag(MibTypeTag)`, `getReferenceSymbol()`, `SnmpTextualConvention.findReference(MibType)`

    - #### hasReferenceTo

          public boolean hasReferenceTo(java.lang.String module,
                               java.lang.String name)

      Checks if this type referenced the specified type symbol. This method should be avoided if possible, as it is much better to rely on type tags to distinguish between two types with the same base type (such as DisplayString and IpAddress).
      Parameters:
        `module` - the type symbol module (MIB) name
        `name` - the type symbol name
      Returns:
        true if this type was a reference to the symbol, or false otherwise
      Since:
        2.2
      See Also:
        `hasTag(int, int)`, `hasTag(MibTypeTag)`, `getReferenceSymbol()`, `SnmpTextualConvention.findReference(MibType)`

    - #### getName

          public java.lang.String getName()

      Returns the type name.
      Returns:
        the type name, or an empty string if not applicable
      Since:
        2.2

    - #### getTag

          public MibTypeTag getTag()

      Returns the type tag. The type tags consist of a category and value number, and are used to identify a specific type uniquely (such as IpAddress and similar). Most (if not all) SNMP types have unique tags that are normally inherited when the type is referenced. Type tags may also be chained together, in which case this method returns the first tag in the chain.
      Returns:
        the type tag, or null if no type tag has been set
      Since:
        2.2

    - #### setTag

          public void setTag(boolean implicit,
                    MibTypeTag tag)

      Sets the type tag. The old type tag is kept to some extent, depending on if the implicit flag is set to true or false. For implicit inheritance, the first tag in the old tag chain is replaced with the new tag. For explicit inheritance, the new tag is added first in the tag chain without removing any old tag.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Parameters:
        `implicit` - the implicit inheritance flag
        `tag` - the new type tag
      Since:
        2.2

    - #### getReferenceSymbol

          public MibTypeSymbol getReferenceSymbol()

      Returns the type reference symbol. A type reference is created whenever a type is defined in a type assignment, and later referenced by name from some other symbol. The complete chain of type references is available by calling getReference() recursively on the type of the returned type symbol.

      In general, this method should be avoided as it is much better to rely on type tags to distinguish between two types with the same base type (such as DisplayString and IpAddress).

      Returns:
        the type reference symbol, or null if this type never referenced another type
      Since:
        2.2
      See Also:
        `getTag()`, `SnmpTextualConvention.findReference(MibType)`

    - #### setReferenceSymbol

          public void setReferenceSymbol(MibTypeSymbol symbol)

      Sets the type reference symbol. The type reference is set whenever a type is defined in a type assignment, and later referenced by name from some other symbol.

      NOTE: This is an internal method that should only be called by the MIB loader.

      Parameters:
        `symbol` - the referenced type symbol
      Since:
        2.2

    - #### getComment

          public java.lang.String getComment()

      Returns the type comment.
      Returns:
        the type comment, or null if no comment was set
      Since:
        2.9

    - #### toString

          public java.lang.String toString()

      Returns a string representation of this object.
      Overrides:
        `toString` in class `java.lang.Object`
      Returns:
        a string representation of this object