Hi all,

Was using the spare time of my flight back to Europe to continue hacking on the 
code-gen enum extension.

So now if you define something like this in your mspec:
[enum uint 4 'ApplicationTags'
    ['0x0' NULL]
    ['0x1' BOOLEAN]
    ['0x2' UNSIGNED_INTEGER]
    ['0x3' SIGNED_INTEGER]
    ['0x4' REAL]
    ['0x5' DOUBLE]
    ['0x6' OCTET_STRING]
    ['0x7' CHARACTER_STRING]
    ['0x8' BIT_STRING]
    ['0x9' ENUMERATED]
    ['0xA' DATE]
    ['0xB' TIME]
    ['0xC' BACNET_OBJECT_IDENTIFIER]
]


It generates enums like this:


package org.apache.plc4x.java.bacnetip.readwrite;

import org.apache.plc4x.java.utils.Message;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.Map;

public enum ApplicationTags {

    NULL((byte) 0x0),
    BOOLEAN((byte) 0x1),
    UNSIGNED_INTEGER((byte) 0x2),
    SIGNED_INTEGER((byte) 0x3),
    REAL((byte) 0x4),
    DOUBLE((byte) 0x5),
    OCTET_STRING((byte) 0x6),
    CHARACTER_STRING((byte) 0x7),
    BIT_STRING((byte) 0x8),
    ENUMERATED((byte) 0x9),
    DATE((byte) 0xA),
    TIME((byte) 0xB),
    BACNET_OBJECT_IDENTIFIER((byte) 0xC);

    private static final Logger logger = 
LoggerFactory.getLogger(ApplicationTags.class);

    private static final Map<Byte, ApplicationTags> map;
    static {
        map = new HashMap<>();
        for (ApplicationTags value : ApplicationTags.values()) {
            map.put((byte) value.getValue(), value);
        }
    }

    private byte value;

    ApplicationTags(byte value) {
        this.value = value;
    }

    public byte getValue() {
        return value;
    }

    public static ApplicationTags valueOf(byte value) {
        if (!map.containsKey(value)) {
            logger.error("No ApplicationTags for value {}", value);
        }
        return map.get(value);
    }

}

However I guess I’ll refactor the template to not use normal Enums in Java as 
these can’t be extended and a lot of protocols define some constants in the 
spec and allow the implementation to define its own ones. This wouldn’t be 
possible with Java enums.

Still need to test and fine-tune the feature of being able to pass additional 
named constants along. In general both mspec as well as the code-gen should 
support this already, just haven’t tested it.

So I guess I’ll be able to continue implementing BACnet tomorrow.

Chris



Reply via email to