/*
 * 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.parse.old.parser.context.condition;

import com.google.common.base.Predicate;
import com.google.common.collect.Collections2;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * Conditions collection.
 *
 * @author zhangliang
 * @author maxiaoguang
 */
@Getter
@ToString
@RequiredArgsConstructor
public final class Conditions {
    
    private List<AndCondition> orCondition = new ArrayList<>();
    
    public Conditions(final Condition condition) {
        add(condition);
    }
    
    /**
     * Add condition.
     *
     * @param condition condition
     */
    public void add(final Condition condition) {
        // TODO self-join has problem, table name maybe use alias
        if (orCondition.isEmpty()) {
            orCondition.add(new AndCondition());
        }
        orCondition.iterator().next().getConditions().add(condition);
    }
    
    /**
     * Optimize or condition.
     *
     */
    public void optimize() {
        List<AndCondition> nullConditions = null;
        for (AndCondition each : orCondition) {
            if (each.getConditions().iterator().next() instanceof NullCondition) {
                nullConditions = getNullConditions();
                break;
            }
        }
        if (null != nullConditions) {
            orCondition = nullConditions;
        }
    }
    
    private List<AndCondition> getNullConditions() {
        List<AndCondition> result = new LinkedList<>();
        AndCondition andCondition = new AndCondition();
        andCondition.getConditions().add(new NullCondition());
        result.add(andCondition);
        return result;
    }
    
    /**
     * Find conditions by column.
     *
     * @param column column
     * @return conditions
     */
    public List<Condition> findConditions(final Column column) {
        List<Condition> result = new LinkedList<>();
        for (AndCondition each : orCondition) {
            result.addAll(Collections2.filter(each.getConditions(), new Predicate<Condition>() {
                
                @Override
                public boolean apply(final Condition input) {
                    return input.getColumn().equals(column);
                }
            }));
        }
        return result;
    }
}

@@ -1323,32 +1323,33 @@
 ion%3E orCondition
+s
  = new ArrayList
@@ -1662,16 +1662,17 @@
 ondition
+s
 .isEmpty
@@ -1700,16 +1700,17 @@
 ondition
+s
 .add(new
@@ -1756,16 +1756,17 @@
 ondition
+s
 .iterato
@@ -1987,32 +1987,33 @@
 ch : orCondition
+s
 ) %7B%0A            
@@ -2241,24 +2241,25 @@
  orCondition
+s
  = nullCondi
@@ -2847,32 +2847,33 @@
 ch : orCondition
+s
 ) %7B%0A            

refactor Conditions.orCondition => Conditions.orConditions.