Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file distributed with this work for additional information regarding copyright ownership. The ASF licenses this file to You 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.apache.aries.jmx.codec;
 
 import static org.apache.aries.jmx.util.TypeUtils.fromString;
 import static org.apache.aries.jmx.util.TypeUtils.primitiveTypes;
 import static org.apache.aries.jmx.util.TypeUtils.types;
 import static org.osgi.jmx.JmxConstants.ARRAY_OF;
 import static org.osgi.jmx.JmxConstants.KEY;
 import static org.osgi.jmx.JmxConstants.PROPERTY_TYPE;
 import static org.osgi.jmx.JmxConstants.P_BOOLEAN;
 import static org.osgi.jmx.JmxConstants.P_BYTE;
 import static org.osgi.jmx.JmxConstants.P_CHAR;
 import static org.osgi.jmx.JmxConstants.P_DOUBLE;
 import static org.osgi.jmx.JmxConstants.P_FLOAT;
 import static org.osgi.jmx.JmxConstants.P_INT;
 import static org.osgi.jmx.JmxConstants.P_LONG;
 import static org.osgi.jmx.JmxConstants.TYPE;
 import static org.osgi.jmx.JmxConstants.VALUE;
 import static org.osgi.jmx.JmxConstants.VECTOR_OF;
 
 import java.util.List;
 import java.util.Map;
 
 

PropertyData represents Property Type

Version:
$Rev: 1243383 $ $Date: 2012-02-12 19:54:17 -0500 (Sun, 12 Feb 2012) $
See also:
org.osgi.jmx.JmxConstants.PROPERTY_TYPE. It is a codec for the CompositeData representing a Property with an associated Type and Value.
 
 public class PropertyData<T> {

    
 
     private String key;

    
 
     private T value;

    
 
     private String encodedValue;

    
 
     private String encodedType;
 
     private PropertyData() {
         super();
     }
 
 
     @SuppressWarnings("unchecked")
     private PropertyData(String key, T valueString preservedBaseTypethrows IllegalArgumentException {
         if (key == null) {
             throw new IllegalArgumentException("Argument key cannot be null");
         }
         if (value == null) {
             throw new IllegalArgumentException("Argument value cannot be null");
         }
         this. = key;
         this. = value;
         Class<T> type = (Class<T>) value.getClass();
         if (type.isArray()) {
             this. =  + type.getComponentType().getSimpleName();
             StringBuilder builder = new StringBuilder();
             int length = Array.getLength(value);
             boolean useDelimiter = false;
            for (int i = 0; i < lengthi++) {
                if (useDelimiter) {
                    builder.append(",");
                } else {
                    useDelimiter = true;
                }
                builder.append(Array.get(valuei));
            }
            this. = builder.toString();
        } else if (type.equals(Vector.class)) {
            Vector<?> vector = (Vector<?>) value;
            Class<? extends ObjectcomponentType = Object.class;
            if (vector.size() > 0) {
                componentType = vector.firstElement().getClass();
            }
            this. =  + componentType.getSimpleName();
            StringBuilder builder = new StringBuilder();
            Vector<?> valueVector = (Vector<?>) value;
            boolean useDelimiter = false;
            for (Object val : valueVector) {
                if (useDelimiter) {
                    builder.append(",");
                } else {
                    useDelimiter = true;
                }
                builder.append(val);
            }
            this. = builder.toString();
        } else if (List.class.isAssignableFrom(type)) {
            // Lists are encoded as Arrays...
            List<?> list = (List<?>) value;
            Class<?> componentType = Object.class;
            if (list.size() > 0)
                componentType = list.get(0).getClass();
            this. =  + componentType.getSimpleName();
            StringBuilder builder = new StringBuilder();
            boolean useDelimiter = false;
            for (Object o : list) {
                if (useDelimiter) {
                    builder.append(",");
                } else {
                    useDelimiter = true;
                }
                builder.append(o);
            }
            this. = builder.toString();
        } else {
            this. = (preservedBaseType == null) ? type.getSimpleName() : preservedBaseType;
            this. = value.toString();
        }
    }

    
Static factory method for PropertyData instance parameterized by value's type

Parameters:
<T>
key
value an instance of org.osgi.jmx.JmxConstants.SCALAR
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static <T> PropertyData<T> newInstance(String key, T valuethrows IllegalArgumentException {
        return new PropertyData<T>(keyvaluenull);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive int type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<IntegernewInstance(String keyint valuethrows IllegalArgumentException {
        return new PropertyData<Integer>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive long type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<LongnewInstance(String keylong valuethrows IllegalArgumentException {
        return new PropertyData<Long>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive float type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<FloatnewInstance(String keyfloat valuethrows IllegalArgumentException {
        return new PropertyData<Float>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive double type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<DoublenewInstance(String keydouble valuethrows IllegalArgumentException {
        return new PropertyData<Double>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive byte type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<BytenewInstance(String keybyte valuethrows IllegalArgumentException {
        return new PropertyData<Byte>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive char type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<CharacternewInstance(String keychar valuethrows IllegalArgumentException {
        return new PropertyData<Character>(keyvalue);
    }

    
Static factory method for PropertyData instance which preserves encoded type information for primitive boolean type

Parameters:
key
value
Returns:
Throws:
java.lang.IllegalArgumentException if key or value are null or value's type cannot be encoded
    public static PropertyData<BooleannewInstance(String keyboolean valuethrows IllegalArgumentException {
        return new PropertyData<Boolean>(keyvalue);
    }


    
Returns CompositeData representing a Property typed by org.osgi.jmx.JmxConstants.PROPERTY_TYPE.

Returns:
    public CompositeData toCompositeData() {
        CompositeData result = null;
        Map<StringObjectitems = new HashMap<StringObject>();
        items.put(this.);
        items.put(this.);
        items.put(this.);
        try {
            result = new CompositeDataSupport(items);
        } catch (OpenDataException e) {
            throw new IllegalStateException("Failed to create CompositeData for Property [" + this. + ":" + this. + "]"e);
        }
        return result;
    }

    
Constructs a PropertyData object from the given CompositeData

Parameters:
compositeData
Returns:
Throws:
IlleglArgumentException if compositeData is null or not of type org.osgi.jmx.JmxConstants.PROPERTY_TYPE
    @SuppressWarnings("unchecked")
    public static <T> PropertyData<T> from(CompositeData compositeDatathrows IllegalArgumentException {
        if ( compositeData == null ) {
            throw new IllegalArgumentException("Argument compositeData cannot be null");
        }
        if (!compositeData.getCompositeType().equals()) {
            throw new IllegalArgumentException("Invalid CompositeType [" + compositeData.getCompositeType() + "]");
        }
        PropertyData propertyData = new PropertyData();
        propertyData.key = (StringcompositeData.get();
        propertyData.encodedType = (StringcompositeData.get();
        propertyData.encodedValue = (StringcompositeData.get();
        if (propertyData.encodedType == null || propertyData.encodedType.length() < 1) {
            throw new IllegalArgumentException ("Cannot determine type from compositeData : " + compositeData);
        }
        StringTokenizer values = new StringTokenizer(propertyData.encodedValue",");
        int valuesLength = values.countTokens();
        if (propertyData.encodedType.startsWith()) {
            String[] arrayTypeParts = propertyData.encodedType.split("\\s");
            if (arrayTypeParts.length < 3) {
                throw new IllegalArgumentException("Cannot parse Array type from type item : " + propertyData.encodedType);
            }
            String arrayTypeName = arrayTypeParts[2].trim();
            if (!.containsKey(arrayTypeName)) {
                throw new IllegalArgumentException ("Cannot determine type from value : " + arrayTypeName);
            }
            Class<? extends ObjectarrayType = .get(arrayTypeName);
            propertyData.value = Array.newInstance(arrayTypevaluesLength);
            int index = 0;
            while (values.hasMoreTokens()) {
                Array.set(propertyData.valueindex++, fromString(arrayTypevalues.nextToken()));
            }
        } else if (propertyData.encodedType.startsWith()) {
            String[] vectorTypeParts = propertyData.encodedType.split("\\s");
            if (vectorTypeParts.length < 3) {
                throw new IllegalArgumentException("Cannot parse Array type from type item : " + propertyData.encodedType);
            }
            String vectorTypeName = vectorTypeParts[2].trim();
            if (!.containsKey(vectorTypeName)) {
                throw new IllegalArgumentException ("Cannot determine type from value : " + vectorTypeName);
            }
            Class<? extends ObjectvectorType = .get(vectorTypeName);
            Vector vector = new Vector();
            while (values.hasMoreTokens()) {
                vector.add(fromString(vectorTypevalues.nextToken()));
            }
            propertyData.value = vector;
        } else {
            if (!.containsKey(propertyData.encodedType)) {
                throw new IllegalArgumentException ("Cannot determine type from value : " + propertyData.encodedType);
            }
            Class<? extends ObjectvalueType = .get(propertyData.encodedType);
            propertyData.value = fromString(valueTypepropertyData.encodedValue);
        }
        return propertyData;
    }
    public String getKey() {
        return ;
    }
    public T getValue() {
        return ;
    }
    public String getEncodedType() {
        return ;
    }
    public String getEncodedValue() {
        return ;
    }
    public boolean isEncodingPrimitive() {
        return .containsKey();
    }
New to GrepCode? Check out our FAQ X