OpenConcerto

Dépôt officiel du code source de l'ERP OpenConcerto
sonarqube

svn://code.openconcerto.org/openconcerto

Rev

Blame | Last modification | View Log | RSS feed

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 * 
 * Copyright 2011-2019 OpenConcerto, by ILM Informatique. All rights reserved.
 * 
 * The contents of this file are subject to the terms of the GNU General Public License Version 3
 * only ("GPL"). You may not use this file except in compliance with the License. You can obtain a
 * copy of the License at http://www.gnu.org/licenses/gpl-3.0.html See the License for the specific
 * language governing permissions and limitations under the License.
 * 
 * When distributing the software, include this License Header Notice in each file.
 */
 
 package org.openconcerto.sql.view.list.action;

import org.openconcerto.sql.element.SQLElement;
import org.openconcerto.sql.model.SQLRowAccessor;
import org.openconcerto.sql.model.SQLRowValues;
import org.openconcerto.sql.model.SQLTable;
import org.openconcerto.utils.CollectionUtils;
import org.openconcerto.utils.cc.IPredicate;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class ListEvent {

    static private final IPredicate<ListEvent> emptyTotalRowCountPredicate = createTotalRowCountPredicate(0, 0);

    static public final IPredicate<ListEvent> getEmptyListPredicate() {
        return emptyTotalRowCountPredicate;
    }

    static public final IPredicate<ListEvent> createTotalRowCountPredicate(final int min, final int max) {
        return new IPredicate<ListEvent>() {
            @Override
            public boolean evaluateChecked(ListEvent e) {
                return e.getTotalRowCount() >= min && e.getTotalRowCount() <= max;
            }
        };
    }

    static private final IPredicate<ListEvent> singleSelectionPredicate = createSelectionCountPredicate(1, 1);
    static private final IPredicate<ListEvent> nonEmptySelectionPredicate = createNonEmptySelectionPredicate(Integer.MAX_VALUE);

    static public final IPredicate<ListEvent> getSingleSelectionPredicate() {
        return singleSelectionPredicate;
    }

    static public final IPredicate<ListEvent> getNonEmptySelectionPredicate() {
        return nonEmptySelectionPredicate;
    }

    static public final IPredicate<ListEvent> createNonEmptySelectionPredicate(final int max) {
        return createSelectionCountPredicate(1, max);
    }

    static public final IPredicate<ListEvent> createSelectionCountPredicate(final int min, final int max) {
        return new IPredicate<ListEvent>() {
            @Override
            public boolean evaluateChecked(ListEvent e) {
                // this is the fastest since it involves no object creation
                final List<?> selectedIDs = e.getSelectedRowAccessors();
                return selectedIDs.size() >= min && selectedIDs.size() <= max;
            }
        };
    }

    static public final ListEvent createFromRowValues(final Object source, final SQLElement elem, final int totalRowCount, final List<SQLRowValues> selection) {
        return new ListEvent(source, elem, totalRowCount, selection, selection);
    }

    static public final ListEvent createFromRowAccessors(final Object source, final SQLElement elem, final int totalRowCount, final List<? extends SQLRowAccessor> selection) {
        return new ListEvent(source, elem, totalRowCount, null, selection);
    }

    private final Object source;
    private final SQLElement elem;
    private final int totalRowCount;
    private final List<SQLRowValues> selection;
    private final List<? extends SQLRowAccessor> selectionAccessor;

    protected ListEvent(final Object source, final SQLElement elem, final int totalRowCount, final List<SQLRowValues> selection, final List<? extends SQLRowAccessor> selectionAccessor) {
        super();
        this.source = source;
        this.elem = elem;
        this.totalRowCount = totalRowCount;
        assert selection == null || selection == selectionAccessor : "Wasting memory";
        this.selection = selection;
        this.selectionAccessor = Objects.requireNonNull(selectionAccessor);
    }

    public final Object getSource() {
        return this.source;
    }

    public final SQLRowAccessor getSelectedRow() {
        return CollectionUtils.getFirst(this.getSelectedRowAccessors());
    }

    public final List<SQLRowValues> getSelectedRows() {
        if (this.selection == null)
            throw new IllegalStateException("SQLRowValues unavailable, use getSelectedRowAccessors()");
        return this.selection;
    }

    public final List<? extends SQLRowAccessor> getSelectedRowAccessors() {
        return this.selectionAccessor;
    }

    public final List<Number> getSelectedIDs() {
        return SQLRowAccessor.getIDs(this.getSelectedRowAccessors(), new ArrayList<>());
    }

    public final int getTotalRowCount() {
        return this.totalRowCount;
    }

    public final SQLTable getTable() {
        return this.getElement().getTable();
    }

    public final SQLElement getElement() {
        return this.elem;
    }
}