org.coos.messaging.impl.AttributeConstraint Maven / Gradle / Ivy
/**
* 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;
import org.coos.messaging.Op;
import org.coos.messaging.util.Log;
import org.coos.messaging.util.LogFactory;
public class AttributeConstraint implements AFSerializer, Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
protected static final Log logger = LogFactory.getLog(AttributeConstraint.class.getName());
/** the comparison value */
public AttributeValue value;
/**
* the comparison operator
*
* valid values are defined in {@link Op}
**/
public short op;
public AttributeConstraint() {
super();
}
/** creates a (deep) copy of an attribute constraint */
public AttributeConstraint(AttributeConstraint c) {
value = new AttributeValue(c.value);
op = c.op;
}
/** create an equality constraint with the given value */
public AttributeConstraint(AttributeValue v) {
value = v;
op = Op.EQ;
}
/** create a constraint with the given string value */
public AttributeConstraint(short o, String s) {
value = new AttributeValue(s);
op = o;
}
/** create a constraint with the given byte[] value */
public AttributeConstraint(short o, byte[] s) {
value = new AttributeValue(s);
op = o;
}
/** create a constraint with the given int value */
public AttributeConstraint(short o, int i) {
value = new AttributeValue(i);
op = o;
}
/** create a constraint with the given long value */
public AttributeConstraint(short o, long i) {
value = new AttributeValue(i);
op = o;
}
/** create a constraint with the given long value */
public AttributeConstraint(short o, double d) {
value = new AttributeValue(d);
op = o;
}
/** create a constraint with the given boolean value */
public AttributeConstraint(short o, boolean b) {
value = new AttributeValue(b);
op = o;
}
/** create a constraint with the given value */
public AttributeConstraint(short o, AttributeValue x) {
value = new AttributeValue(x);
op = o;
}
public boolean isEqualTo(AttributeConstraint x) {
//
// this is a conservative implementation.
//
return (op == x.op) && ((op == Op.ANY) || value.isEqualTo(x.value));
}
public String toString() {
/*
* SENPBuffer b = new SENPBuffer(); b.encode(this); return new
* String(b.buf, 0, b.length());
*/
return null;
}
public int hashCode() {
return toString().hashCode();
}
public ByteArrayInputStream deSerialize(byte[] data, AFClassLoader cl) throws IOException {
ByteArrayInputStream bin = new ByteArrayInputStream(data);
DataInputStream din = new DataInputStream(bin);
op = din.readShort();
int len = din.readInt();
byte[] tmp = new byte[len];
din.readFully(tmp);
try {
value = (AttributeValue) AttributeValue.class.newInstance();
value.deSerialize(tmp, cl);
} catch (InstantiationException e) {
logger.error("InstantiationException caught", e);
} catch (IllegalAccessException e) {
logger.error("IllegalAccessException caught", e);
}
return bin;
}
public byte[] serialize() throws IOException {
ByteArrayOutputStream bout = new ByteArrayOutputStream();
DataOutputStream dout = new DataOutputStream(bout);
dout.writeShort(op);
byte[] serializedAttribute = value.serialize();
dout.writeInt(serializedAttribute.length);
if (serializedAttribute.length > 0) {
dout.write(serializedAttribute);
}
dout.flush();
return bout.toByteArray();
}
}