/**
 * Copyright 2013-present febit.org (support@febit.org)
 *
 * 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 org.febit.form;

import java.util.ArrayList;
import org.febit.util.ArraysUtil;

/**
 * @author zqq
 */
public class Order extends ArrayList<Order.Entry> {

    public static class Entry {

        public final String field;
        public final boolean asc;

        public Entry(String field, boolean asc) {
            this.field = field;
            this.asc = asc;
        }
    }

    public void add(String field, boolean asc) {
        add(new Entry(field, asc));
    }

    public Order asc(String... fields) {
        for (String field : fields) {
            add(field, true);
        }
        return this;
    }

    public Order desc(String... fields) {
        for (String field : fields) {
            add(field, false);
        }
        return this;
    }

    public Order asc(String field) {
        add(new Entry(field, true));
        return this;
    }

    public Order desc(String field) {
        add(new Entry(field, false));
        return this;
    }

    public Order keep(String... whiteList) {
        if (whiteList == null || whiteList.length == 0) {
            clear();
            return this;
        }
        for (int i = this.size() - 1; i >= 0; i--) {
            if (!ArraysUtil.contains(whiteList, this.get(i).field)) {
                remove(i);
            }
        }
        return this;
    }

    public static Order create() {
        return new Order();
    }

}

@@ -668,16 +668,80 @@
 ayList;%0A
+import java.util.Objects;%0Aimport java.util.function.BiConsumer;%0A
 import o
@@ -1659,32 +1659,194 @@
 rn this;%0A    %7D%0A%0A
+    public void forEach(BiConsumer%3CString, Boolean%3E action) %7B%0A        Objects.requireNonNull(action);%0A        forEach(e -%3E action.accept(e.field, e.asc));%0A    %7D%0A%0A
     public Order
@@ -1965,43 +1965,29 @@
     
-    return this;%0A    
+%7D else %7B%0A
     
-%7D%0A
         
 for 
@@ -1986,163 +1986,64 @@
     
-for (int i = this.size() - 1; i %3E= 0; i--) %7B%0A            if (!ArraysUtil.contains(whiteList, this.get(i).field)) %7B%0A                remove(i);%0A            %7D
+removeIf(e -%3E !ArraysUtil.contains(whiteList, e.field));
 %0A   

update Order