Knopflerfish OSGi 5.2.0

org.knopflerfish.util.metatype
Class AD

java.lang.Object
  extended by org.knopflerfish.util.metatype.AD
All Implemented Interfaces:
java.lang.Cloneable, java.lang.Comparable<AD>, AttributeDefinition

public class AD
extends java.lang.Object
implements AttributeDefinition, java.lang.Comparable<AD>, java.lang.Cloneable

Implementation class for AttributeDefinition.

This class contains get and parse methods for operations related to constructing AttributeDefinition.


Field Summary
 
Fields inherited from interface org.osgi.service.metatype.AttributeDefinition
BIGDECIMAL, BIGINTEGER, BOOLEAN, BYTE, CHARACTER, DOUBLE, FLOAT, INTEGER, LONG, PASSWORD, SHORT, STRING
 
Constructor Summary
AD(java.lang.String id, int type, int card, java.lang.String name, java.lang.String[] defValue)
          Create an AttributeDefinition with empty descrition and no option labels or option values.
AD(java.lang.String id, int type, int card, java.lang.String name, java.lang.String desc, java.lang.String[] defValue, java.lang.String[] optLabels, java.lang.String[] optValues)
          Create a new attribute definition.
AD(java.lang.String id, int type, int card, java.lang.String name, java.lang.String desc, java.lang.String[] defValue, java.lang.String min, java.lang.String max, boolean required)
           
 
Method Summary
 java.lang.Object clone()
           
 int compareTo(AD o)
           
 boolean equals(java.lang.Object other)
           
static java.lang.String escape(java.lang.String s)
          Escape a string so that it'll be parsed as one item, even if it contains SEQUENCE_SEP.
static int getArrayType(java.lang.Object val)
          Get type from an array object.
 int getCardinality()
          Return the cardinality of this attribute.
static java.lang.Class<?> getClass(int type)
          Get java class corresponding to AttributeDefinition type.
 java.lang.String[] getDefaultValue()
          Return a default for this attribute.
 java.lang.String getDescription()
          Return a description of this attribute.
 java.lang.String getID()
          Unique identity for this attribute.
 java.lang.String getName()
          Get the name of the attribute.
 java.lang.String[] getOptionLabels()
          Return a list of labels of option values.
 java.lang.String[] getOptionValues()
          Return a list of option values that this attribute can take.
static java.lang.Class<?> getPrimitiveClass(int type)
          Get the primitive java class from a specificed type.
static int getPrimitiveType(java.lang.Object val)
          Get type from primitive object.
 int getRequired()
           
 int getType()
          Return the type for this attribute.
static int getType(java.lang.Object val)
          Get the attribute type given any suported java object.
 int hashCode()
           
 boolean isOptional()
          Return true if this attribute is optional.
static java.lang.Object parse(java.lang.String value, int card, int type)
          Parse a string value to an object given a cardinality and type.
static java.lang.Object parseSingle(java.lang.String value, int type)
          Parse a single value into an object given a type.
 void setDefaultValue(java.lang.String[] value)
          Set the default value.
 void setDescription(java.lang.String s)
           
 void setOptions(java.lang.String[] optValues, java.lang.String[] optLabels)
          Set values returned by getOptionValues and getOptionLabels.
 java.lang.String toString()
          Convert to human-readable string.
static java.lang.String toString(java.lang.Object obj)
          Convert a object to string that can be parsed by parse
static java.lang.String toString(java.lang.Object[] values)
           
static java.lang.String toString(java.util.Vector<?> values)
           
static java.lang.String toStringFromArray(java.lang.Object array)
           
 java.lang.String validate(java.lang.String value)
          Implementation of validation function.
 
Methods inherited from class java.lang.Object
finalize, getClass, notify, notifyAll, wait, wait, wait
 

Constructor Detail

AD

public AD(java.lang.String id,
          int type,
          int card,
          java.lang.String name,
          java.lang.String[] defValue)
Create an AttributeDefinition with empty descrition and no option labels or option values.


AD

public AD(java.lang.String id,
          int type,
          int card,
          java.lang.String name,
          java.lang.String desc,
          java.lang.String[] defValue,
          java.lang.String[] optLabels,
          java.lang.String[] optValues)
Create a new attribute definition.

Parameters:
id - Unique id of the definition
card - cardinality of the definition
type - One of the type constants STRING...BOOLEAN
Throws:
java.lang.IllegalArgumentException - if type is not supported.
java.lang.IllegalArgumentException - if id is null or empty
java.lang.IllegalArgumentException - if desc is null

AD

public AD(java.lang.String id,
          int type,
          int card,
          java.lang.String name,
          java.lang.String desc,
          java.lang.String[] defValue,
          java.lang.String min,
          java.lang.String max,
          boolean required)
Method Detail

clone

public java.lang.Object clone()
Overrides:
clone in class java.lang.Object

getCardinality

public int getCardinality()
Description copied from interface: AttributeDefinition
Return the cardinality of this attribute. The OSGi environment handles multi valued attributes in arrays ([]) or in Vector objects. The return value is defined as follows:
 
    x = Integer.MIN_VALUE    no limit, but use Vector
    x < 0                    -x = max occurrences, store in Vector
    x > 0                     x = max occurrences, store in array []
    x = Integer.MAX_VALUE    no limit, but use array []
    x = 0                     1 occurrence required
 
 

Specified by:
getCardinality in interface AttributeDefinition
Returns:
The cardinality of this attribute.

getDefaultValue

public java.lang.String[] getDefaultValue()
Description copied from interface: AttributeDefinition
Return a default for this attribute. The object must be of the appropriate type as defined by the cardinality and getType(). The return type is a list of String objects that can be converted to the appropriate type. The cardinality of the return array must follow the absolute cardinality of this type. E.g. if the cardinality = 0, the array must contain 1 element. If the cardinality is 1, it must contain 0 or 1 elements. If it is -5, it must contain from 0 to max 5 elements. Note that the special case of a 0 cardinality, meaning a single value, does not allow arrays or vectors of 0 elements.

Specified by:
getDefaultValue in interface AttributeDefinition
Returns:
Return a default value or null if no default exists.

setDescription

public void setDescription(java.lang.String s)

setDefaultValue

public void setDefaultValue(java.lang.String[] value)
Set the default value.

Throws:
java.lang.IllegalArgumentException - if any of the values cannot be validated.

getDescription

public java.lang.String getDescription()
Description copied from interface: AttributeDefinition
Return a description of this attribute. The description may be localized and must describe the semantics of this type and any constraints.

Specified by:
getDescription in interface AttributeDefinition
Returns:
The localized description of the definition.

getID

public java.lang.String getID()
Description copied from interface: AttributeDefinition
Unique identity for this attribute. Attributes share a global namespace in the registry. E.g. an attribute cn or commonName must always be a String and the semantics are always a name of some object. They share this aspect with LDAP/X.500 attributes. In these standards the OSI Object Identifier (OID) is used to uniquely identify an attribute. If such an OID exists, (which can be requested at several standard organisations and many companies already have a node in the tree) it can be returned here. Otherwise, a unique id should be returned which can be a Java class name (reverse domain name) or generated with a GUID algorithm. Note that all LDAP defined attributes already have an OID. It is strongly advised to define the attributes from existing LDAP schemes which will give the OID. Many such schemes exist ranging from postal addresses to DHCP parameters.

Specified by:
getID in interface AttributeDefinition
Returns:
The id or oid

getName

public java.lang.String getName()
Description copied from interface: AttributeDefinition
Get the name of the attribute. This name may be localized.

Specified by:
getName in interface AttributeDefinition
Returns:
The localized name of the definition.

setOptions

public void setOptions(java.lang.String[] optValues,
                       java.lang.String[] optLabels)
Set values returned by getOptionValues and getOptionLabels.

Parameters:
optValues - Values to be return by getOptionValues. Can be null.
optLabels - Values to be return by getOptionLabels. Can be null iff optValues is null.
Throws:
java.lang.IllegalArgumentException - if optValues and optLabels are not the same length.

getOptionLabels

public java.lang.String[] getOptionLabels()
Description copied from interface: AttributeDefinition
Return a list of labels of option values.

The purpose of this method is to allow menus with localized labels. It is associated with getOptionValues. The labels returned here are ordered in the same way as the values in that method.

If the function returns null, there are no option labels available.

This list must be in the same sequence as the getOptionValues() method. I.e. for each index i in getOptionLabels, i in getOptionValues() should be the associated value.

For example, if an attribute can have the value male, female, unknown, this list can return (for dutch) new String[] { "Man", "Vrouw", "Onbekend" }.

Specified by:
getOptionLabels in interface AttributeDefinition
Returns:
A list values

getOptionValues

public java.lang.String[] getOptionValues()
Description copied from interface: AttributeDefinition
Return a list of option values that this attribute can take.

If the function returns null, there are no option values available.

Each value must be acceptable to validate() (return "") and must be a String object that can be converted to the data type defined by getType() for this attribute.

This list must be in the same sequence as getOptionLabels(). I.e. for each index i in getOptionValues, i in getOptionLabels() should be the label.

For example, if an attribute can have the value male, female, unknown, this list can return new String[] { "male", "female", "unknown" }.

Specified by:
getOptionValues in interface AttributeDefinition
Returns:
A list values

getType

public int getType()
Description copied from interface: AttributeDefinition
Return the type for this attribute.

Defined in the following constants which map to the appropriate Java type. STRING,LONG,INTEGER, CHAR, BYTE,DOUBLE,FLOAT, BOOLEAN.

Specified by:
getType in interface AttributeDefinition
Returns:
The type for this attribute.

isOptional

public boolean isOptional()
Return true if this attribute is optional.


getRequired

public int getRequired()

getType

public static int getType(java.lang.Object val)
Get the attribute type given any suported java object.

Parameters:
val - Any java object, including arrays of primitive types. If val is a Vector, it must contain at least one element.
Returns:
STRING...BOOLEAN
Throws:
java.lang.IllegalArgumentException - if type cannot be derived.

getArrayType

public static int getArrayType(java.lang.Object val)
Get type from an array object.

Parameters:
val - an array object
Returns:
STRING...BOOLEAN
Throws:
java.lang.IllegalArgumentException - if type cannot be derived.

getPrimitiveType

public static int getPrimitiveType(java.lang.Object val)
Get type from primitive object.

Parameters:
val - an object of one of the boxed primitive java object classes.
Returns:
STRING...BOOLEAN
Throws:
java.lang.IllegalArgumentException - if type cannot be derived.

validate

public java.lang.String validate(java.lang.String value)
Implementation of validation function.

Validation of primitive types is performed by trying to create an' object from the corresponding String constructor.

Validation of arrays and vectors is performed by splitting the input string into comma-separated words.

Specified by:
validate in interface AttributeDefinition
Parameters:
value - The value before turning it into the basic data type. If the cardinality indicates a multi-valued attribute then the given string must be escaped.
Returns:
null, "", or another string

parse

public static java.lang.Object parse(java.lang.String value,
                                     int card,
                                     int type)
Parse a string value to an object given a cardinality and type.


parseSingle

public static java.lang.Object parseSingle(java.lang.String value,
                                           int type)
Parse a single value into an object given a type.


getClass

public static java.lang.Class<?> getClass(int type)
Get java class corresponding to AttributeDefinition type.

Throws:
java.lang.IllegalArgumentException - if type is not supporte.d

getPrimitiveClass

public static java.lang.Class<?> getPrimitiveClass(int type)
Get the primitive java class from a specificed type.

Throws:
java.lang.IllegalArgumentException - if type is not supported.

toString

public java.lang.String toString()
Convert to human-readable string.

Overrides:
toString in class java.lang.Object

toString

public static java.lang.String toString(java.lang.Object obj)
Convert a object to string that can be parsed by parse


escape

public static java.lang.String escape(java.lang.String s)
Escape a string so that it'll be parsed as one item, even if it contains SEQUENCE_SEP.


toStringFromArray

public static java.lang.String toStringFromArray(java.lang.Object array)

toString

public static java.lang.String toString(java.lang.Object[] values)

toString

public static java.lang.String toString(java.util.Vector<?> values)

compareTo

public int compareTo(AD o)
Specified by:
compareTo in interface java.lang.Comparable<AD>

hashCode

public int hashCode()
Overrides:
hashCode in class java.lang.Object

equals

public boolean equals(java.lang.Object other)
Overrides:
equals in class java.lang.Object

Knopflerfish OSGi 5.2.0