001/*
002 * Stallion Core: A Modern Web Framework
003 *
004 * Copyright (C) 2015 - 2016 Stallion Software LLC.
005 *
006 * This program is free software: you can redistribute it and/or modify it under the terms of the
007 * GNU General Public License as published by the Free Software Foundation, either version 2 of
008 * the License, or (at your option) any later version. This program is distributed in the hope that
009 * it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
010 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
011 * License for more details. You should have received a copy of the GNU General Public License
012 * along with this program.  If not, see <http://www.gnu.org/licenses/gpl-2.0.html>.
013 *
014 *
015 *
016 */
017
018package io.stallion.dataAccess.filtering;
019
020import java.util.*;
021
022
023public class FilterGroup<Y> {
024    private List<Y> items = new ArrayList<>();
025    private String key = "";
026    private int count = 0;
027    private Set<List<String>> firstOfs = new HashSet<>();
028    private Set<List<String>> lastOfs = new HashSet<>();
029
030    public FilterGroup(String key) {
031        this.key = key;
032    }
033
034
035    public boolean lastOf(String...fieldNames) {
036        if (getLastOfs().contains(Arrays.asList(fieldNames))) {
037            return true;
038        }
039        return false;
040    }
041
042    public boolean firstOf(String...fieldNames) {
043        if (getFirstOfs().contains(Arrays.asList(fieldNames))) {
044            return true;
045        } else {
046            return false;
047        }
048    }
049
050    /**
051     * The first matching item for this group.
052     *
053     * @return
054     */
055    public Y getFirst() {
056        if (items.size() == 0) {
057            return null;
058        } else {
059            return getItems().get(0);
060        }
061    }
062
063    /**
064     * All matching items for this group
065     * @return
066     */
067    public List<Y> getItems() {
068        return items;
069    }
070
071    public void setItems(List<Y> items) {
072        this.items = items;
073    }
074
075    public String getKey() {
076        return key;
077    }
078
079    public void setKey(String key) {
080        this.key = key;
081    }
082
083    /**
084     * The count of matching items.
085     *
086     * @return
087     */
088    public int getCount() {
089        return count;
090    }
091
092    void setCount(int count) {
093        this.count = count;
094    }
095
096    int incrCount() {
097        count++;
098        return count;
099    }
100
101    public Set<List<String>> getFirstOfs() {
102        return firstOfs;
103    }
104
105    public void setFirstOfs(Set<List<String>> firstOfs) {
106        this.firstOfs = firstOfs;
107    }
108
109    public Set<List<String>> getLastOfs() {
110        return lastOfs;
111    }
112
113    public void setLastOfs(Set<List<String>> lastOfs) {
114        this.lastOfs = lastOfs;
115    }
116}