package com.github.rnewson.couchdb.lucene;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.lucene.queryParser.ParseException;
import org.apache.lucene.search.SortField;

/**
 * A TypedField consists of a normal Lucene field name and a type.
 * 
 * @author robertnewson
 * 
 */
public final class TypedField {

    public static enum Type {
        DATE(SortField.LONG), DOUBLE(SortField.DOUBLE), FLOAT(SortField.FLOAT), INT(SortField.INT), LONG(SortField.LONG), STRING(
                SortField.STRING);

        private final int asSortField;

        private Type(final int asSortField) {
            this.asSortField = asSortField;
        }

        public int asSortField() {
            return asSortField;
        }

    }

    private static Pattern PATTERN = Pattern.compile("^(\\w+)(<(\\w+)>)?$");

    private final String name;

    private final Type type;

    public TypedField(final String string) throws ParseException {
        final Matcher matcher = PATTERN.matcher(string);

        if (!matcher.matches()) {
            throw new ParseException("Field '" + string + "' not recognized.");
        }

        this.name = matcher.group(1);
        try {
            this.type = matcher.group(3) == null ? Type.STRING : Type.valueOf(matcher.group(3).toUpperCase());
        } catch (final IllegalArgumentException e) {
            throw new ParseException("Unrecognized type '" + matcher.group(3) + "'");
        }
    }

    public String getName() {
        return name;
    }

    public Type getType() {
        return type;
    }

    @Override
    public String toString() {
        return String.format("%s<%s>", name, type.toString().toLowerCase());
    }

}

@@ -159,49 +159,67 @@
 on;%0A
+%0A
 import 
-org.apache.lucene.search.Sort
+com.github.rnewson.couchdb.lucene.couchdb.
 Field
+Type
 ;%0A%0A/
@@ -361,430 +361,8 @@
  %7B%0A%0A
-    public static enum Type %7B%0A        DATE(SortField.LONG), DOUBLE(SortField.DOUBLE), FLOAT(SortField.FLOAT), INT(SortField.INT), LONG(SortField.LONG), STRING(%0A                SortField.STRING);%0A%0A        private final int asSortField;%0A%0A        private Type(final int asSortField) %7B%0A            this.asSortField = asSortField;%0A        %7D%0A%0A        public int asSortField() %7B%0A            return asSortField;%0A        %7D%0A%0A    %7D%0A%0A
     
@@ -485,16 +485,21 @@
 e final 
+Field
 Type typ
@@ -855,16 +855,21 @@
  null ? 
+Field
 Type.STR
@@ -874,16 +874,21 @@
 TRING : 
+Field
 Type.val
@@ -1131,32 +1131,32 @@
 rn name;%0A    %7D%0A%0A
-
     public Type 
@@ -1146,24 +1146,29 @@
 %0A    public 
+Field
 Type getType

tidy up