package starfish.type;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import starfish.helper.Util;

public class TableMetadata implements Serializable {

    private static final long serialVersionUID = 1L;

    public final String tableName, keyColname, valueColname, versionColname, timestampColname;

    public TableMetadata(Map<String, String> names) {
        this.tableName = Util.notNull(names.get("tableName"), "tableName must not be null");
        this.keyColname = Util.notNull(names.get("keyColname"), "keyColname must not be null");
        this.valueColname = Util.notNull(names.get("valueColname"), "valueColname must not be null");
        this.versionColname = Util.notNull(names.get("versionColname"), "versionColname must not be null");
        this.timestampColname = Util.notNull(names.get("timestampColname"), "timestampColname must not be null");
    }

    public static TableMetadata create(String tableName) {
        Map<String, String> names = new HashMap<String, String>();
        names.put("tableName", tableName);
        names.put("keyColname", "key");
        names.put("valueColname", "value");
        names.put("versionColname", "version");
        names.put("timestampColname", "updated");
        return new TableMetadata(names);
    }

    public String groovyReplace(String format) {
        return Util.groovyReplace(format, toMap(), true);
    }

    public String groovyReplaceKeep(String format) {
        return Util.groovyReplace(format, toMap(), false);
    }

    public Map<String, String> toMap() {
        Map<String, String> result = new HashMap<String, String>();
        result.put("tableName", tableName);
        result.put("keyColname", keyColname);
        result.put("valueColname", valueColname);
        result.put("versionColname", versionColname);
        result.put("timestampColname", timestampColname);
        return result;
    }

    @Override
    public String toString() {
        return String.format("tableName=%s, keyColname=%s, valueColname=%s, versionColname=%s, timestampColname=%s",
                tableName, keyColname, valueColname, versionColname, timestampColname);
    }

    @Override
    public int hashCode() {
        return (tableName + '|' + keyColname + '|' + valueColname + '|' + versionColname + '|' + timestampColname)
                .toLowerCase().hashCode();
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null || !(obj instanceof TableMetadata)) {
            return false;
        }
        TableMetadata that = (TableMetadata) obj;
        return tableName.equalsIgnoreCase(that.tableName) &&
                keyColname.equalsIgnoreCase(that.keyColname) &&
                valueColname.equalsIgnoreCase(that.valueColname) &&
                versionColname.equalsIgnoreCase(that.versionColname) &&
                timestampColname.equalsIgnoreCase(that.timestampColname);
    }

}

@@ -1294,32 +1294,553 @@
 (names);%0A    %7D%0A%0A
+    public static TableMetadata create(String tableName, String keyColname, String valueColname, String versionColname,%0A            String timestampColname) %7B%0A        Map%3CString, String%3E names = new HashMap%3CString, String%3E();%0A        names.put(%22tableName%22, tableName);%0A        names.put(%22keyColname%22, keyColname);%0A        names.put(%22valueColname%22, valueColname);%0A        names.put(%22versionColname%22, versionColname);%0A        names.put(%22timestampColname%22, timestampColname);%0A        return new TableMetadata(names);%0A    %7D%0A%0A
     public Strin

add factory method to construct object from bare args