package com.openxc.messages;

import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import android.os.Parcel;

import com.google.common.base.Objects;
import com.google.gson.annotations.SerializedName;

public class NamedVehicleMessage extends KeyedMessage {
    public static final String NAME_KEY = "name";

    @SerializedName(NAME_KEY)
    private String mName;

    public static final String[] sRequiredFieldsValues = new String[] {
            NAME_KEY };
    public static final Set<String> sRequiredFields = new HashSet<String>(
            Arrays.asList(sRequiredFieldsValues));

    public NamedVehicleMessage(String name) {
        mName = name;
    }

    public NamedVehicleMessage(Long timestamp, String name) {
        super(timestamp);
        mName = name;
    }

    public String getName() {
        return mName;
    }

    @Override
    public boolean equals(Object obj) {
        if(!super.equals(obj) || getClass() != obj.getClass()) {
            return false;
        }

        final NamedVehicleMessage other = (NamedVehicleMessage) obj;
        return mName.equals(other.mName);
    }

    @Override
    public MessageKey getKey() {
        HashMap<String, Object> key = new HashMap<>();
        key.put(NAME_KEY, getName());
        return new MessageKey(key);
    }

    public static boolean containsRequiredFields(Set<String> fields) {
        return fields.containsAll(sRequiredFields);
    }

    @Override
    public String toString() {
        return Objects.toStringHelper(this)
            .add("timestamp", getTimestamp())
            .add("name", getName())
            .add("extras", getExtras())
            .toString();
    }

    @Override
    public void writeToParcel(Parcel out, int flags) {
        super.writeToParcel(out, flags);
        out.writeString(getName());
    }

    @Override
    protected void readFromParcel(Parcel in) {
        super.readFromParcel(in);
        mName = in.readString();
    }

    protected NamedVehicleMessage(Parcel in)
            throws UnrecognizedMessageTypeException {
        readFromParcel(in);
    }

    protected NamedVehicleMessage() { }
}

@@ -871,32 +871,308 @@
 n mName;%0A    %7D%0A%0A
+    @Override%0A    public int compareTo(VehicleMessage other) %7B%0A        NamedVehicleMessage otherMessage = (NamedVehicleMessage) other;%0A        int nameComp = getName().compareTo(otherMessage.getName());%0A        return nameComp == 0 ? super.compareTo(other) : nameComp;%0A    %7D%0A%0A
     @Override%0A  

Add comparator for Named messages, but sort isn't working in dashboard.