Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright (c) OSGi Alliance (2004, 2011). All Rights Reserved.
   * 
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
   *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 package org.osgi.service.monitor;
 
 import java.util.Date;

A StatusVariable object represents the value of a status variable taken with a certain collection method at a certain point of time. The type of the StatusVariable can be int, float, boolean or String.

A StatusVariable is identified by an ID string that is unique within the scope of a Monitorable. The ID must be a non- null, non-empty string that conforms to the "symbolic-name" definition in the OSGi core specification. This means that only the characters [-_.a-zA-Z0-9] may be used. The length of the ID must not exceed 32 bytes when UTF-8 encoded.

Version:
$Id: 637f5f827f34da9c9743385701f517dc2f5e763d $
 
 public final class StatusVariable {
     //----- Public constants -----//
     
Constant for identifying int data type.
 
     public static final int    TYPE_INTEGER   = 0;

    
Constant for identifying float data type.
 
     public static final int    TYPE_FLOAT = 1;

    
Constant for identifying String data type.
 
     public static final int    TYPE_STRING = 2;

    
Constant for identifying boolean data type.
 
    public static final int    TYPE_BOOLEAN = 3;

    
Constant for identifying 'Cumulative Counter' data collection method.
 
     public static final int    CM_CC        = 0;

    
Constant for identifying 'Discrete Event Registration' data collection method.
 
     public static final int    CM_DER       = 1;

    
Constant for identifying 'Gauge' data collection method.
 
     public static final int    CM_GAUGE     = 2;

    
Constant for identifying 'Status Inspection' data collection method.
 
     public static final int    CM_SI        = 3;
 
     //----- Package private constants -----//
 
     static final String SYMBOLIC_NAME_CHARACTERS =
         "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ" + "0123456789" +
         "-_.";   // a subset of the characters allowed in DMT URIs 
 
     static final int MAX_ID_LENGTH = 32;
     
     //----- Private fields -----//
     private String  id;
     private Date    timeStamp;
     private int     cm;
     private int     type;
 
     private int     intData;
     private float   floatData;
     private String  stringData;
     private boolean booleanData;
 
 
    //----- Constructors -----//
    
Constructor for a StatusVariable of int type.

Parameters:
id the identifier of the StatusVariable
cm the collection method, one of the CM_ constants
data the int value of the StatusVariable
Throws:
java.lang.IllegalArgumentException if the given id is not a valid StatusVariable name, or if cm is not one of the collection method constants
java.lang.NullPointerException if the id parameter is null
    public StatusVariable(String idint cmint data) {
        setCommon(idcm);
         = ;
         = data;
    }

    
Constructor for a StatusVariable of float type.

Parameters:
id the identifier of the StatusVariable
cm the collection method, one of the CM_ constants
data the float value of the StatusVariable
Throws:
java.lang.IllegalArgumentException if the given id is not a valid StatusVariable name, or if cm is not one of the collection method constants
java.lang.NullPointerException if the id parameter is null
    public StatusVariable(String idint cmfloat data) {
        setCommon(idcm);
         = ;
         = data;
    }

    
Constructor for a StatusVariable of boolean type.

Parameters:
id the identifier of the StatusVariable
cm the collection method, one of the CM_ constants
data the boolean value of the StatusVariable
Throws:
java.lang.IllegalArgumentException if the given id is not a valid StatusVariable name, or if cm is not one of the collection method constants
java.lang.NullPointerException if the id parameter is null
    public StatusVariable(String idint cmboolean data) {
        setCommon(idcm);
         = ;
         = data;
    }

    
Constructor for a StatusVariable of String type.

Parameters:
id the identifier of the StatusVariable
cm the collection method, one of the CM_ constants
data the String value of the StatusVariable, can be null
Throws:
java.lang.IllegalArgumentException if the given id is not a valid StatusVariable name, or if cm is not one of the collection method constants
java.lang.NullPointerException if the id parameter is null
    public StatusVariable(String idint cmString data) {
        setCommon(idcm);
         = ;
         = data;
    }
    
    // ----- Public methods -----//
    
Returns the ID of this StatusVariable. The ID is unique within the scope of a Monitorable.

Returns:
the ID of this StatusVariable
    public String getID() {
        return ;
    }

    
Returns information on the data type of this StatusVariable.

Returns:
one of the TYPE_ constants indicating the type of this StatusVariable
    public int getType() {
        return ;
    }

Returns the timestamp associated with the StatusVariable. The timestamp is stored when the StatusVariable instance is created, generally during the Monitorable.getStatusVariable(java.lang.String) method call.

Returns:
the time when the StatusVariable value was queried, cannot be null
    public Date getTimeStamp() {
        return ;
    }

    
Returns the StatusVariable value if its type is String.

Returns:
the StatusVariable value as a String
Throws:
java.lang.IllegalStateException if the type of the StatusVariable is not String
    public String getString() throws IllegalStateException {
        if ( != )
            throw new IllegalStateException(
                    "This StatusVariable does not contain a String value.");
        return ;
    }

    
Returns the StatusVariable value if its type is int.

Returns:
the StatusVariable value as an int
Throws:
java.lang.IllegalStateException if the type of this StatusVariable is not int
    public int getInteger() throws IllegalStateException {
        if ( != )
            throw new IllegalStateException(
                    "This StatusVariable does not contain an integer value.");
        return ;
    }

    
Returns the StatusVariable value if its type is float.

Returns:
the StatusVariable value as a float
Throws:
java.lang.IllegalStateException if the type of this StatusVariable is not float
    public float getFloat() throws IllegalStateException {
        if ( != )
            throw new IllegalStateException(
                    "This StatusVariable does not contain a float value.");
        return ;
    }

    
Returns the StatusVariable value if its type is boolean.

Returns:
the StatusVariable value as a boolean
Throws:
java.lang.IllegalStateException if the type of this StatusVariable is not boolean
    public boolean getBoolean() throws IllegalStateException {
        if ( != )
            throw new IllegalStateException(
                    "This StatusVariable does not contain a boolean value.");
        return ;
    }
    
    
Returns the collection method of this StatusVariable. See section 3.3 b) in [ETSI TS 132 403]

Returns:
one of the CM_ constants
    public int getCollectionMethod() {
        return ;
    }

    
Compares the specified object with this StatusVariable. Two StatusVariable objects are considered equal if their full path, collection method and type are identical, and the data (selected by their type) is equal.

Parameters:
obj the object to compare with this StatusVariable
Returns:
true if the argument represents the same StatusVariable as this object
    public boolean equals(Object obj) {
        if (!(obj instanceof StatusVariable))
            return false;
        
        StatusVariable other = (StatusVariableobj;
        
        if (!equals(other.id) ||  != other.cm ||  != other.type)
            return false;
        
        switch () {
        case return  == other.intData;
        case :   return  == other.floatData;
        case :  return equals(other.stringData);
        case return  == other.booleanData;
        }
        
        return false// never reached
    }

    
Returns the hash code value for this StatusVariable. The hash code is calculated based on the full path, collection method and value of the StatusVariable.

Returns:
the hash code of this object
    public int hashCode() {
        int hash = hashCode() ^ ;
        switch () {
        case return hash ^ ;
        case :   return hash ^ hashCode(new Float());
        case return hash ^ hashCode(new Boolean());
        case :  return hash ^ hashCode();
        }
        
        return 0; // never reached
    }
    //  String representation: StatusVariable(path, cm, time, type, value)
    
Returns a String representation of this StatusVariable. The returned String contains the full path, collection method, timestamp, type and value parameters of the StatusVariable in the following format:
StatusVariable(<path>, <cm>, <timestamp>, <type>, <value>)
The collection method identifiers used in the string representation are "CC", "DER", "GAUGE" and "SI" (without the quotes). The format of the timestamp is defined by the Date.toString method, while the type is identified by one of the strings "INTEGER", "FLOAT", "STRING" and "BOOLEAN". The final field contains the string representation of the value of the status variable.

Returns:
the String representation of this StatusVariable
    public String toString() {
        String cmName = null;
        switch () {
        case :    cmName = "CC";    break;
        case :   cmName = "DER";   break;
        case cmName = "GAUGE"break;
        case :    cmName = "SI";    break;
        }
        
        String beg = "StatusVariable(" +  + ", " + cmName + ", "
                +  + ", ";
        
        switch () {
        case return beg + "INTEGER, " +  + ")";
        case :   return beg + "FLOAT, " +  + ")";
        case :  return beg + "STRING, " +  + ")";
        case return beg + "BOOLEAN, " +  + ")";
        }
        
        return null// never reached
    }
    //----- Private methods -----//
    
    private void setCommon(String idint cm)
            throws IllegalArgumentExceptionNullPointerException {
        checkId(id"StatusVariable ID");
        
        if (cm !=  && cm !=  && cm !=  && cm != )
            throw new IllegalArgumentException(
                    "Unknown data collection method constant '" + cm + "'.");
        
        this. = id;
        this. = cm;
         = new Date();
    }
    
    private boolean equals(Object o1Object o2) {
        return o1 == null ? o2 == null : o1.equals(o2);
    }
    private int hashCode(Object o) {
        return o == null ? 0 : o.hashCode();
    }
    private static void checkId(String idString idName)
            throws IllegalArgumentExceptionNullPointerException {
        if (id == null)
            throw new NullPointerException(idName + " is null.");
        if(id.length() == 0)
            throw new IllegalArgumentException(idName + " is empty.");
        
        byte[] nameBytes;
        try {
            nameBytes = id.getBytes("UTF-8");
        } catch (UnsupportedEncodingException e) {
            // never happens, "UTF-8" must always be supported
            throw new IllegalStateException(e.getMessage());
        }
        if(nameBytes.length > )
            throw new IllegalArgumentException(idName + " is too long " + 
                    "(over " +  + " bytes in UTF-8 encoding).");
        if(id.equals(".") || id.equals(".."))
            throw new IllegalArgumentException(idName + " is invalid.");
        
        if(!containsValidChars(id))
            throw new IllegalArgumentException(idName + 
                    " contains invalid characters.");
    }
    
    private static boolean containsValidChars(String name) {
        char[] chars = name.toCharArray();
        for(int i = 0; i < chars.lengthi++)
            if(.indexOf(chars[i]) == -1)
                return false;
        
        return true;        
    }
New to GrepCode? Check out our FAQ X