/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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 org.apache.shardingsphere.core.parsing.parser.context.table;

import com.google.common.base.Optional;
import com.google.common.base.Preconditions;
import lombok.ToString;

import java.util.ArrayList;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.TreeSet;

/**
 * Tables collection.
 * 
 * @author zhangliang
 */
@ToString
public final class Tables {
    
    private final List<Table> tables = new ArrayList<>();
    
    /**
     * 添加表解析对象.
     * 
     * @param table 表对象
     */
    public void add(final Table table) {
        tables.add(table);
    }
    
    /**
     * 判断是否为空.
     *
     * @return 是否为空
     */
    public boolean isEmpty() {
        return tables.isEmpty();
    }
    
    /**
     * Judge is same table or not.
     *
     * @return is same table or not
     */
    public boolean isSameTable() {
        Set<String> tableNames = new TreeSet<>(String.CASE_INSENSITIVE_ORDER);
        for (Table each : tables) {
            tableNames.add(each.getName());
        }
        return 1 == tableNames.size();
    }
    
    /**
     * 判断是否为单表.
     * 
     * @return 是否为单表
     */
    public boolean isSingleTable() {
        return 1 == tables.size();
    }
    
    /**
     * 获取表名称.
     *
     * @return 表名称
     */
    public String getSingleTableName() {
        Preconditions.checkArgument(!isEmpty());
        return tables.get(0).getName();
    }
    
    /**
     * 获取表名称集合.
     * 
     * @return 表名称集合
     */
    public Collection<String> getTableNames() {
        Collection<String> result = new LinkedHashSet<>(tables.size(), 1);
        for (Table each : tables) {
            result.add(each.getName());
        }
        return result;
    }
    
    /**
     * 根据表名称或别名查找表解析对象.
     * 
     * @param tableNameOrAlias 表名称或别名
     * @return 表解析对象
     */
    public Optional<Table> find(final String tableNameOrAlias) {
        Optional<Table> tableFromName = findTableFromName(tableNameOrAlias);
        return tableFromName.isPresent() ? tableFromName : findTableFromAlias(tableNameOrAlias);
    }
    
    private Optional<Table> findTableFromName(final String name) {
        for (Table each : tables) {
            if (each.getName().equals(name)) {
                return Optional.of(each);
            }
        }
        return Optional.absent();
    }
    
    private Optional<Table> findTableFromAlias(final String alias) {
        for (Table each : tables) {
            if (each.getAlias().isPresent() && each.getAlias().get().equalsIgnoreCase(alias)) {
                return Optional.of(each);
            }
        }
        return Optional.absent();
    }
}

@@ -1154,19 +1154,8 @@
 bles
- collection
 .%0A *
@@ -1307,15 +1307,17 @@
   * 
-%E6%B7%BB%E5%8A%A0%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%AF%B9%E8%B1%A1
+Add table
 .%0A  
@@ -1346,11 +1346,13 @@
 ble 
-%E8%A1%A8%E5%AF%B9%E8%B1%A1
+table
 %0A   
@@ -1454,14 +1454,35 @@
   * 
-%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA
+Judge table is empty or not
 .%0A  
@@ -1505,12 +1505,29 @@
 urn 
-%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA
+table is empty or not
 %0A   
@@ -1973,15 +1973,36 @@
   * 
-%E5%88%A4%E6%96%AD%E6%98%AF%E5%90%A6%E4%B8%BA%E5%8D%95%E8%A1%A8
+Judge is single table or not
 .%0A  
@@ -2026,13 +2026,30 @@
 urn 
-%E6%98%AF%E5%90%A6%E4%B8%BA%E5%8D%95%E8%A1%A8
+is single table or not
 %0A   
@@ -2151,21 +2151,37 @@
 %0A     * 
-%E8%8E%B7%E5%8F%96%E8%A1%A8%E5%90%8D%E7%A7%B0
+Get single table name
 .%0A     *
@@ -2196,19 +2196,33 @@
 @return 
-%E8%A1%A8%E5%90%8D%E7%A7%B0
+single table name
 %0A     */
@@ -2382,15 +2382,23 @@
   * 
-%E8%8E%B7%E5%8F%96%E8%A1%A8%E5%90%8D%E7%A7%B0%E9%9B%86%E5%90%88
+Get table names
 .%0A  
@@ -2422,13 +2422,19 @@
 urn 
-%E8%A1%A8%E5%90%8D%E7%A7%B0%E9%9B%86%E5%90%88
+table names
 %0A   
@@ -2700,23 +2700,42 @@
   * 
-%E6%A0%B9%E6%8D%AE%E8%A1%A8%E5%90%8D%E7%A7%B0%E6%88%96%E5%88%AB%E5%90%8D%E6%9F%A5%E6%89%BE%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%AF%B9%E8%B1%A1
+Find table via table name or alias
 .%0A  
@@ -2775,14 +2775,27 @@
 ias 
-%E8%A1%A8%E5%90%8D%E7%A7%B0%E6%88%96%E5%88%AB%E5%90%8D
+table name or alias
 %0A   
@@ -2810,13 +2810,13 @@
 urn 
-%E8%A1%A8%E8%A7%A3%E6%9E%90%E5%AF%B9%E8%B1%A1
+table
 %0A   

change Tables's javadoc to english