aboutsummaryrefslogtreecommitdiffstats
path: root/server/src/main/java/com/vaadin/server/data/Query.java
blob: 6a18bbc398452cc2e72b435ad33b6a1685ee585d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright 2000-2016 Vaadin Ltd.
 *
 * 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 com.vaadin.server.data;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
 * Query object used to request data from a backend. Contains index limits,
 * sorting and filtering information.
 *
 * @since
 */
public class Query implements Serializable {

    private final int offset;
    private final int limit;
    private final List<SortOrder<String>> sortOrders;
    private final Set<Object> filters;

    /**
     * Constructs a Query for all rows from 0 to {@link Integer#MAX_VALUE}
     * without sorting and filtering.
     */
    public Query() {
        offset = 0;
        limit = Integer.MAX_VALUE;
        sortOrders = Collections.emptyList();
        filters = Collections.emptySet();
    }

    /**
     * Constructs a Query for all rows from 0 to {@link Integer#MAX_VALUE} with
     * filtering.
     *
     * @param filters
     *            set of back end filters
     */
    public Query(Set<Object> filters) {
        offset = 0;
        limit = Integer.MAX_VALUE;
        sortOrders = Collections.emptyList();
        this.filters = filters;
    }

    /**
     * Constructs a new Query object with given offset, limit, sorting and
     * filtering.
     *
     * @param offset
     *            first index to fetch
     * @param limit
     *            fetched item count
     * @param sortOrders
     *            sorting order for fetching
     * @param filters
     *            filtering for fetching
     */
    public Query(int offset, int limit, List<SortOrder<String>> sortOrders,
            Set<Object> filters) {
        this.offset = offset;
        this.limit = limit;
        this.sortOrders = sortOrders;
        this.filters = filters;
    }

    /**
     * Gets the first index of items to fetch.
     *
     * @return offset for data request
     */
    public int getOffset() {
        return offset;
    }

    /**
     * Gets the limit of items to fetch.
     * <p>
     * <strong>Note: </strong>It is possible that
     * {@code offset + limit > item count}
     *
     * @return number of items to fetch
     */
    public int getLimit() {
        return limit;
    }

    /**
     * Gets the sorting for items to fetch.
     *
     * @return list of sort orders
     */
    public List<SortOrder<String>> getSortOrders() {
        return sortOrders;
    }

    /**
     * Gets the filters for items to fetch.
     *
     * @return set of filters
     */
    public Set<Object> getFilters() {
        return filters;
    }
}