2

I am looking for Apache Storm's source code and I encountered something which I never seen: CLOSE_MESSAGE((short)-100) or CLOSE_MESSAGE((short)-100). Why one should subtract someNumber from short type? What does it mean? Here is actual code:

enum ControlMessage {
  CLOSE_MESSAGE((short)-100),
  EOB_MESSAGE((short)-201),
  OK_RESPONSE((short)-200),
  FAILURE_RESPONSE((short)-400),
  SASL_TOKEN_MESSAGE_REQUEST((short)-202),
  SASL_COMPLETE_REQUEST((short)-203);

private short code;

//private constructor
private ControlMessage(short code) {
    this.code = code;
}

/**
 * Return a control message per an encoded status code
 * @param encoded
 * @return
 */
static ControlMessage mkMessage(short encoded) {
    for(ControlMessage cm: ControlMessage.values()) {
      if(encoded == cm.code) return cm;
    }
    return null;
}

int encodeLength() {
    return 2; //short
}

/**
 * encode the current Control Message into a channel buffer
 * @throws Exception
 */
ChannelBuffer buffer() throws IOException {
    ChannelBufferOutputStream bout = new ChannelBufferOutputStream(ChannelBuffers.directBuffer(encodeLength()));      
    write(bout);
    bout.close();
    return bout.buffer();
}

void write(ChannelBufferOutputStream bout) throws IOException {
    bout.writeShort(code);        
} 
}
seenukarthi
  • 8,241
  • 10
  • 47
  • 68
Humoyun Ahmad
  • 2,875
  • 4
  • 28
  • 46

3 Answers3

2

No I don't think it is subtracting the number. It is casting an int to short! The enum has a constructor that accepts a short as a argument, so it just means passing negative integers to the constructor!

Sweeper
  • 213,210
  • 22
  • 193
  • 313
1

From the looks of it, the code (short)-someNumber is simply casting the int to a short. And this has to be done because the Enum constructor accepts short not int in this case.

EDIT: I just realized that the minus can be a bit misleading so, another way to write the same line of code would be like this:

    int someNumber = -100;
    int someNumber1 = -201;
    enum ControlMessage {
       CLOSE_MESSAGE((short)(someNumber)),
       EOB_MESSAGE((short)(someNumber1)),
       (...)
    }

where the someNumber variable is of type int and can be any number, in this case it's -100,-201,-200 etc.

Mechanic
  • 172
  • 1
  • 10
  • after looking through all answers, I think this casting was used to indicate FLAGS like in HTTP header 200 means OK, but I don't understand why they used negative numbers? – Humoyun Ahmad Aug 27 '15 at 07:09
  • To be honest, I am not familiar with Apache, although, it could be a convention for these specific messages, to use specific negative numbers so that when they appear on error logs they can easily be identified sort of like 404 error (not found). Obviously from the enum scope there is no issue with using negative numbers. As for the casting to short, may be just for optimizing memory usage since short requires less bits than int to be represented, thus decreasing the allocated memory. I actually use short instead of int for these cases too. Sometimes byte even. – Mechanic Aug 27 '15 at 19:37
1

As other answers have mentioned, what's occurring in the code is called casting. Here are some resources where you can learn more.

Community
  • 1
  • 1
jaco0646
  • 15,303
  • 7
  • 59
  • 83