All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.coos.messaging.impl.AttributeValue Maven / Gradle / Ivy

There is a newer version: 1.3.1
Show newest version
/**
 * COOS - Connected Objects Operating System (www.connectedobjects.org).
 *
 * Copyright (C) 2009 Telenor ASA and Tellu AS. All rights reserved.
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This library is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see .
 *
 * You may also contact one of the following for additional information:
 * Telenor ASA, Snaroyveien 30, N-1331 Fornebu, Norway (www.telenor.no)
 * Tellu AS, Hagalokkveien 13, N-1383 Asker, Norway (www.tellu.no)
 */
package org.coos.messaging.impl;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.Serializable;

import org.coos.util.serialize.AFClassLoader;
import org.coos.util.serialize.AFSerializer;


public class AttributeValue implements AFSerializer, Serializable {


    /** null type, the default type of an attribute */
    public static final int NULL = 0;

    /** string of bytes */
    public static final int BYTEARRAY = 1;

    /**
     * string of bytes
     *
     **/
    public static final int STRING = 1;

    /**
     * integer type.
     *
     * corresponds to the Java long type.
     **/
    public static final int LONG = 2;

    /**
     * integer type.
     *
     * corresponds to the Java int type.
     **/
    public static final int INT = 2;

    /**
     * double type.
     *
     * corresponds to the Java double type.
     **/
    public static final int DOUBLE = 3;

    /**
     * boolean type.
     *
     * corresponds to the Java boolean type.
     **/
    public static final int BOOL = 4;

    private int type;

    private byte[] sval;
    private long ival;
    private double dval;
    private boolean bval;

    public AttributeValue() {
        type = NULL;
    }

    public AttributeValue(AttributeValue x) {

        if (x == null) {
            type = NULL;

            return;
        }

        type = x.type;

        switch (type) {

        case INT:
            ival = x.ival;

            break;

        case BOOL:
            bval = x.bval;

            break;

        case DOUBLE:
            dval = x.dval;

            break;

        case BYTEARRAY:
            sval = cloneByteArray((byte[]) x.sval);

            break;
        }
    }

    public AttributeValue(String s) {

        if (s == null) {
            type = NULL;

            return;
        }

        type = BYTEARRAY;
        sval = s.getBytes();
    }

    public AttributeValue(byte[] s) {
        type = BYTEARRAY;
        sval = cloneByteArray((byte[]) s);
    }

    public AttributeValue(long i) {
        type = LONG;
        ival = i;
        sval = null;
    }

    public AttributeValue(boolean b) {
        type = BOOL;
        bval = b;
        sval = null;
    }

    public AttributeValue(double d) {
        type = DOUBLE;
        dval = d;
        sval = null;
    }

    private byte[] cloneByteArray(byte[] target) {
        byte[] retb = new byte[target.length];

        for (int i = 0; i < target.length; i++) {
            retb[i] = target[i];
        }

        return retb;
    }

    //
    // other types here ...work in progress...
    //

    public int getType() {
        return type;
    }

    public int intValue() {

        switch (type) {

        case LONG:
            return (int) ival;

        case BOOL:
            return bval ? 1 : 0;

        case DOUBLE:
            return (int) dval;

        case BYTEARRAY:
            return Integer.parseInt(new String(sval));

        default:
            return 0;
        }
    }

    public long longValue() {

        switch (type) {

        case LONG:
            return ival;

        case BOOL:
            return bval ? 1 : 0;

        case DOUBLE:
            return (int) dval;

        case BYTEARRAY:
            return Long.parseLong(new String(sval));

        default:
            return 0;
        }
    }

    public double doubleValue() {

        switch (type) {

        case LONG:
            return ival;

        case BOOL:
            return bval ? 1 : 0;

        case DOUBLE:
            return dval;

        case BYTEARRAY:
            return Double.valueOf(new String(sval)).doubleValue();

        default:
            return 0;
        }
    }

    public boolean booleanValue() {

        switch (type) {

        case LONG:
            return ival != 0;

        case BOOL:
            return bval;

        case DOUBLE:
            return dval != 0;

        case BYTEARRAY:
            return new Boolean(new String(sval).equalsIgnoreCase("true")).booleanValue();

        default:
            return false;
        }
    }

    public String stringValue() {

        switch (type) {

        case LONG:
            return String.valueOf(ival);

        case BOOL:
            return String.valueOf(bval);

        case DOUBLE:
            return String.valueOf(dval);

        case BYTEARRAY:
            return new String(sval);

        default:
            return "";
        }
    }

    public byte[] byteArrayValue() {

        switch (type) {

        case LONG:
            return String.valueOf(ival).getBytes();

        case BOOL:
            return String.valueOf(bval).getBytes();

        case DOUBLE:
            return String.valueOf(dval).getBytes();

        case BYTEARRAY:
            return sval;

        default:
            return null;
        }
    }

    public boolean isEqualTo(AttributeValue x) {

        switch (type) {

        case BYTEARRAY:

            if (sval.length != x.sval.length) {
                return false;
            }

            for (int i = 0; i < sval.length; i++) {

                if (sval[i] != x.sval[i]) {
                    return false;
                }
            }

            return true;

        case LONG:
            return ival == x.longValue();

        case DOUBLE:
            return dval == x.doubleValue();

        case BOOL:
            return bval == x.booleanValue();

        default:
            return false;
        }
    }

    public String toString() {
        String s = "Attribute: Type: ";

        switch (type) {

        case LONG:
            return s + "long, Value: " + stringValue();

        case BOOL:
            return s + "boolean, Value: " + stringValue();

        case DOUBLE:
            return s + "double, Value: " + stringValue();

        case BYTEARRAY:
            return s + "String, Value: " + stringValue();

        default:
            return null;
        }

    }

    public int hashCode() {
        return this.toString().hashCode();
    }

    public ByteArrayInputStream deSerialize(byte[] data, AFClassLoader cl) throws IOException {
        ByteArrayInputStream bin = new ByteArrayInputStream(data);
        DataInputStream din = new DataInputStream(bin);
        type = din.readInt();

        if (type != 0) {

            switch (type) {

            case LONG:
                ival = din.readLong();

                break;

            case BOOL:
                bval = din.readBoolean();

                break;

            case DOUBLE:
                dval = din.readDouble();

                break;

            case BYTEARRAY:

                int l = din.readInt();
                sval = new byte[l];
                din.read(sval);

                break;

            default:

            }
        }

        return null;
    }

    public byte[] serialize() throws IOException {
        ByteArrayOutputStream bout = new ByteArrayOutputStream();
        DataOutputStream dout = new DataOutputStream(bout);
        dout.writeInt(type);

        if (type != 0) {

            switch (type) {

            case LONG:
                dout.writeLong(ival);

                break;

            case BOOL:
                dout.writeBoolean(bval);

                break;

            case DOUBLE:
                dout.writeDouble(dval);

                break;

            case BYTEARRAY:
                dout.writeInt(sval.length);
                dout.write(sval);

                break;

            default:

            }
        }

        dout.flush();

        return bout.toByteArray();
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy