Skip to content

Commit 4755803

Browse files
committed
An interface (and an implementing abstract class) that represents the result of the connection
1 parent e9ae1cb commit 4755803

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.connection;
16+
17+
import java.util.Iterator;
18+
19+
public abstract class AbstarctPaginatedData<T> implements PaginatedData<T> {
20+
21+
private boolean hasPreviousPage;
22+
private boolean hasNextPage;
23+
private Iterable<T> data;
24+
25+
public AbstarctPaginatedData(boolean hasPreviousPage, boolean hasNextPage, Iterable<T> data) {
26+
this.hasNextPage = hasNextPage;
27+
this.hasPreviousPage = hasPreviousPage;
28+
this.data = data;
29+
}
30+
31+
@Override
32+
public boolean hasNextPage() {
33+
return hasNextPage;
34+
}
35+
36+
@Override
37+
public boolean hasPreviousPage() {
38+
return hasPreviousPage;
39+
}
40+
41+
@Override
42+
public Iterator<T> iterator() {
43+
return data.iterator();
44+
}
45+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/**
2+
* Copyright 2016 Yurii Rashkovskii
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
*/
15+
package graphql.annotations.connection;
16+
17+
/**
18+
* This class is the result of a connection. Every Graphql connection field must return this interface
19+
* <p>
20+
* NOTE: this interface extends Iterable. The data is retrieved from the "iterator" function.
21+
* Please implement the iterator with data structure that has order
22+
*
23+
* @param <T> the data of which we paginated over
24+
*/
25+
public interface PaginatedData<T> extends Iterable<T> {
26+
27+
/**
28+
* Whether or not this is the last page
29+
*
30+
* @return true if there is a next page, false otherwise
31+
*/
32+
boolean hasNextPage();
33+
34+
/**
35+
* Whether or not this is the first page
36+
*
37+
* @return true if there is a previous page, false otherwise
38+
*/
39+
boolean hasPreviousPage();
40+
41+
/**
42+
* get the encoded cursor of the entity
43+
*
44+
* @param entity the entity
45+
* @return String representation of the cursor of the entity
46+
*/
47+
String getCursor(T entity);
48+
}

0 commit comments

Comments
 (0)