Car Development

October 31, 2008

The Java problem expanded

Filed under: Java, Programing — Tags: , — balp @ 12:42 pm

I try to expand a little on the java problem, Java is in some ways great. But when needing to match a binary format it have some weaknesses. Our problem is that we have a binary protocol, bases on bytes, Bytes that Java also don’t like, for this we need to write serialization and deserialize in java. There is already code in C++, but we like a pure java solution. In sort from java, over a bus, to c++ and back again. The bus might be slow and small, think CAN. So every bite counts.

I like the solution on this to only mention the numbers one time all other time is the code a symbolical name should be used, the first part is easy in java as ell. The solution is C++ is easy with the down side that it’s not totally type safe.

enum type {
  vaule1 = 1,
  vaule2 = 2
};

int main(int argc, char **argv)
{
  type var = vaule1;
  int i = var;
  i++;
  var = (type)i;
  return 0;
}

The C++ solution is easy and efficient as much in C++. It’s relative type safe, getting to an interger is easy the enums only work for integers how ever and the conversion out is implicit. I can with out problem with the typecast make an illiegal enum. One could notice i have to use the c style cast to get this trick working.

When trying to do this in Java i get into trouble, my first example is.

public enum EnumValues {
	Value1(1),
	Value2(2);
	private int i;

	EnumValues(int i) {
		this.i = i;
	}
	public int intValue() {
		return i;
	}
}

This work for converting the value for the net from a type with written names, e.g os.writeByte(EnumValues.Value1.byteValue(). The other direction from the read byte to the enum is harder. In the receiving code what I like to do is.

	switch(i) {
	case EnumTesting.EnumValues.Value1: // Java ERROR
		// one code
		break;
	default:
		// not supported packet
	}

This how ever fail on that the case values can’t be converted to integers, to make this work we need a constant byte vaule out of the enum, the only possibility i have found is a “public static final byte VALUE1 = 1;”

public enum EnumValues {
        Value1(EnumValues.VALUE1),
        Value2(EnumValues.VALUE2);
        final private byte i;
        private static final byte VALUE1 = 1;
        private static final byte VALUE2 = 2;

        EnumValues(byte i) {
                this.i = i;
        }
        final public byte intValue() {
                return i;
        }
        public static EnumValues fromInt(int v) {
                switch(v) {
                case EnumValues.VALUE1:
                        return Value1;
                case EnumValues.VALUE2:
                        return Value2;
                default:
                        return null;
                }
        }
}

This leaves the switch to.

                switch(EnumValues.fromInt(i)) {
		case Value1:
			// one code
			break;
		default:
			// not supported packet
		}

The question is all this better that the enum? Can this be changes by using a factory method? There is no possibility to make the constructor public. The EnumValues class can easy be refractored down to using the vales only once, with the loss of some speed. As i see this solutions is clearer to read especially with large number of enum values, but looses in speed.

public enum EnumValues {
	Value1(1),
	Value2(2);
	final private byte i;

	EnumValues(int i) {
		this.i = (byte)i;
	}
	final public byte intValue() {
		return i;
	}
	public static EnumValues fromInt(int v) {
		for(EnumValues t : EnumValues.values())
			if(v == t.intValue()) {
				return t;
			}
		return null;
	}
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: