
/*
 * Copyright (C) 2014 Archie L. Cobbs. All rights reserved.
 *
 * $Id$
 */

package org.jsimpledb.core;

import org.jsimpledb.parse.ParseContext;
import org.jsimpledb.util.ByteReader;
import org.jsimpledb.util.ByteWriter;
import org.jsimpledb.util.UnsignedIntEncoder;

/**
 * Raw {@link Enum} type. Does not support null values, but generates them when a value cannot be decoded.
 */
class EnumValueType extends FieldType<EnumValue> {

    private final StringType stringType = new StringType();

    EnumValueType() {
        super(EnumValue.class);
    }

// FieldType

    @Override
    public EnumValue read(ByteReader reader) {
        final int ordinal = UnsignedIntEncoder.read(reader);
        final String name = this.stringType.read(reader);
        return new EnumValue(ordinal, name);
    }

    @Override
    public void write(ByteWriter writer, EnumValue value) {
        UnsignedIntEncoder.write(writer, value.getOrdinal());
        this.stringType.write(writer, value.getName());
    }

    @Override
    public byte[] getDefaultValue() {
        throw new UnsupportedOperationException();          // null is the default value
    }

    @Override
    public void skip(ByteReader reader) {
        UnsignedIntEncoder.skip(reader);
        this.stringType.skip(reader);
    }

    @Override
    public EnumValue fromParseableString(ParseContext ctx) {
        ctx.expect('[');
        final String name = ctx.matchPrefix("[^]#]+").group();
        ctx.expect('#');
        final int ordinal = Integer.parseInt(ctx.matchPrefix("[0-9]+").group());
        ctx.expect(']');
        return new EnumValue(ordinal, name);
    }

    @Override
    public String toParseableString(EnumValue value) {
        return "[" + value.getName() + "#" + value.getOrdinal() + "]";
    }

    @Override
    public int compare(EnumValue value1, EnumValue value2) {
        int diff = Integer.compare(value1.getOrdinal(), value2.getOrdinal());
        if (diff != 0)
            return diff;
        diff = value1.getName().compareTo(value2.getName());
        if (diff != 0)
            return diff;
        return 0;
    }

    @Override
    public boolean hasPrefix0xff() {
        return false;
    }
}


@@ -1369,33 +1369,8 @@
 ) %7B%0A
-        ctx.expect('%5B');%0A
     
@@ -1414,14 +1414,64 @@
 ix(%22
-%5B%5E%5D#%5D+
+%5C%5Cp%7BjavaJavaIdentifierStart%7D%5C%5Cp%7BjavaJavaIdentifierPart%7D*
 %22).g
@@ -1535,83 +1535,49 @@
 l = 
-Integer.parseInt(ctx.matchPrefix(%22%5B0-9%5D+%22).group());%0A        ctx.expect('%5D'
+FieldType.INTEGER.fromParseableString(ctx
 );%0A 
@@ -1714,14 +1714,8 @@
 turn
- %22%5B%22 +
  val
@@ -1757,14 +1757,8 @@
 al()
- + %22%5D%22
 ;%0A  

Remove square brackets from string encoding of EnumValue.