org.graylog2.rest.models.PaginatedResponse Maven / Gradle / Ivy
/**
* This file is part of Graylog.
*
* Graylog is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Graylog is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Graylog. If not, see .
*/
package org.graylog2.rest.models;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonValue;
import com.google.common.collect.ImmutableMap;
import org.graylog2.database.PaginatedList;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Map;
@JsonAutoDetect
public class PaginatedResponse {
private final String listKey;
private final PaginatedList paginatedList;
private final String query;
private final Map context;
private PaginatedResponse(String listKey, PaginatedList paginatedList, @Nullable String query, @Nullable Map context) {
this.listKey = listKey;
this.paginatedList = paginatedList;
this.query = query;
this.context = context;
}
@JsonValue
public Map jsonValue() {
final ImmutableMap.Builder builder = ImmutableMap.builder()
.putAll(paginatedList.pagination().asMap())
.put(listKey, new ArrayList<>(paginatedList));
if (query != null) {
builder.put("query", query);
}
if (paginatedList.grandTotal().isPresent()) {
builder.put("grand_total", paginatedList.grandTotal().get());
}
if (context != null && !context.isEmpty()) {
builder.put("context", context);
}
return builder.build();
}
public static PaginatedResponse create(String listKey, PaginatedList paginatedList) {
return new PaginatedResponse<>(listKey, paginatedList, null, null);
}
public static PaginatedResponse create(String listKey, PaginatedList paginatedList, Map context) {
return new PaginatedResponse<>(listKey, paginatedList, null, context);
}
public static PaginatedResponse create(String listKey, PaginatedList paginatedList, String query) {
return new PaginatedResponse<>(listKey, paginatedList, query, null);
}
public static PaginatedResponse create(String listKey, PaginatedList paginatedList, String query, Map context) {
return new PaginatedResponse<>(listKey, paginatedList, query, context);
}
}