/*
 * Copyright 2017 Philip Fritzsche <p-f@users.noreply.github.com>.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package one.p_f.testing.msagimport.data;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

/**
 *
 * @author p-f
 */
public class TableSchema {

    public static final class Builder {

        private String schemaName;
        private ObjectType type;
        private final List<FieldType> fieldTypes;
        private final List<String> fieldNames;

        public Builder() {
            schemaName = null;
            fieldTypes = new ArrayList<>();
            fieldNames = new ArrayList<>();
        }

        public Builder setObjectType(ObjectType type) {
            this.type = type;
            return this;
        }

        public Builder setSchemaName(String name) {
            schemaName = name;
            return this;
        }

        public Builder addField(FieldType type, String name) {
            fieldTypes.add(type);
            fieldNames.add(name);
            return this;
        }

        public TableSchema build() {
            if (schemaName == null) {
                throw new IllegalStateException("No schema name set.");
            }
            TableSchema schema = new TableSchema();
            schema.schemaName = schemaName;
            schema.type = type;
            schema.fieldTypes = fieldTypes
                    .toArray(new FieldType[fieldTypes.size()]);
            schema.fieldNames = fieldNames
                    .toArray(new String[fieldNames.size()]);
            return schema;
        }
    }

    public static enum ObjectType {
        NODE,
        EDGE
    }

    public static enum FieldType {
        ID,
        ATTRIBUTE,
        KEY,
        IGNORE
    }

    public static char SCOPE_SEPARATOR = ':';

    private TableSchema() {
    }

    private String schemaName;

    private ObjectType type;

    private FieldType[] fieldTypes;

    private String[] fieldNames;

    /**
     * @return the schemaName
     */
    public String getSchemaName() {
        return schemaName;
    }

    /**
     * @return the type
     */
    public ObjectType getType() {
        return type;
    }

    /**
     * @return the fieldTypes
     */
    public List<FieldType> getFieldTypes() {
        return Collections.unmodifiableList(Arrays.asList(fieldTypes));
    }

    /**
     * @return the fieldNames
     */
    public List<String> getFieldNames() {
        return Collections.unmodifiableList(Arrays.asList(fieldNames));
    }

    @Override
    public String toString() {
        return type.toString() + ' ' + schemaName + ':'
                + IntStream.range(0, fieldNames.length)
                        .mapToObj(i -> fieldTypes[i].toString() + ' '
                        + fieldNames[i])
                        .collect(Collectors.joining(", "));
    }
}

@@ -700,33 +700,8 @@
 st;%0A
-import java.util.Arrays;%0A
 impo
@@ -1972,62 +1972,37 @@
 s = 
-fieldTypes%0A                    .toArray(new FieldType%5B
+Collections.unmodifiableList(
 fiel
@@ -2007,24 +2007,16 @@
 eldTypes
-.size()%5D
 );%0A     
@@ -2046,59 +2046,37 @@
 s = 
-fieldNames%0A                    .toArray(new String%5B
+Collections.unmodifiableList(
 fiel
@@ -2085,16 +2085,8 @@
 ames
-.size()%5D
 );%0A 
@@ -2455,16 +2455,21 @@
 private 
+List%3C
 FieldTyp
@@ -2469,18 +2469,17 @@
 ieldType
-%5B%5D
+%3E
  fieldTy
@@ -2500,16 +2500,20 @@
 ate 
+List%3C
 String
-%5B%5D
+%3E
  fie
@@ -2850,51 +2850,8 @@
 urn 
-Collections.unmodifiableList(Arrays.asList(
 fiel
@@ -2856,18 +2856,16 @@
 eldTypes
-))
 ;%0A    %7D%0A
@@ -2972,51 +2972,8 @@
 urn 
-Collections.unmodifiableList(Arrays.asList(
 fiel
@@ -2978,18 +2978,16 @@
 eldNames
-))
 ;%0A    %7D%0A
@@ -3140,14 +3140,14 @@
 mes.
-length
+size()
 )%0A  
@@ -3193,19 +3193,23 @@
 eldTypes
-%5Bi%5D
+.get(i)
 .toStrin
@@ -3258,11 +3258,15 @@
 ames
-%5Bi%5D
+.get(i)
 )%0A  

Store data as List instead of array in TableSchema