Question

The abstract class MidiMessage has 3 subclasses: MetaMessage, ShortMessage, SysexMessage. I'm listening to a MidiDevice, which sends either shortmessages or sysexmessages. Problem is that the hashcode of a MidiMessage is never the same. Therefore the only difference between MidiMessages is the data byte-array.

My current solution is to store the bytes in a string and then check if the hashmap contains the string:

public class Test{
    private String inputKey="";
    private byte[] data;
    private int isShortMessage;

    //...

    private void setInputKey(MidiMessage msg){
        inputKey="";
        data=msg.getMessage();
        if(msg instanceof ShortMessage){
            inputKey+= ((ShortMessage) msg).getChannel();   
            //last byte contains the value. Value isn't needed. 
            isShortMessage=1;
        }
        else isShortMessage=0;
        for(int i=0; i<msg.getLength()-isShortMessage;i++){
            inputKey+= data[i]; 
        }
   }
} 

But I don't like it because of the overhead of the casting (could get rid of that, if I always cut off the last byte; this should work almost always...) and the string conversion. So, is there a better way to check if the hashmap or a collection contains a midimessage ?

Was it helpful?

Solution

I will use:

Arrays.hashCode(byte[] a)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top