Example usage for java.util.concurrent ConcurrentSkipListMap tailMap

List of usage examples for java.util.concurrent ConcurrentSkipListMap tailMap

Introduction

In this page you can find the example usage for java.util.concurrent ConcurrentSkipListMap tailMap.

Prototype

public ConcurrentNavigableMap<K, V> tailMap(K fromKey, boolean inclusive) 

Source Link

Usage

From source file:com.google.cloud.dns.testing.LocalDnsHelper.java

/**
 * Lists zones. Next page token is the last listed zone name and is returned only of there is more
 * to list and if the user does not exclude nextPageToken from field options.
 *//*from   w  w  w . ja  v a  2 s .c o  m*/
@VisibleForTesting
Response listZones(String projectId, String query) {
    Map<String, Object> options = OptionParsers.parseListZonesOptions(query);
    Response response = checkListOptions(options);
    if (response != null) {
        return response;
    }
    ConcurrentSkipListMap<String, ZoneContainer> containers = findProject(projectId).zones();
    String[] fields = (String[]) options.get("fields");
    String dnsName = (String) options.get("dnsName");
    String pageToken = (String) options.get("pageToken");
    Integer maxResults = options.get("maxResults") == null ? null
            : Integer.valueOf((String) options.get("maxResults"));
    boolean sizeReached = false;
    boolean hasMorePages = false;
    LinkedList<String> serializedZones = new LinkedList<>();
    String lastZoneName = null;
    ConcurrentNavigableMap<String, ZoneContainer> fragment = pageToken != null
            ? containers.tailMap(pageToken, false)
            : containers;
    for (ZoneContainer zoneContainer : fragment.values()) {
        ManagedZone zone = zoneContainer.zone();
        if (dnsName == null || zone.getDnsName().equals(dnsName)) {
            if (sizeReached) {
                // we do not add this, just note that there would be more and there should be a token
                hasMorePages = true;
                break;
            } else {
                try {
                    lastZoneName = zone.getName();
                    serializedZones.addLast(jsonFactory.toString(OptionParsers.extractFields(zone, fields)));
                } catch (IOException e) {
                    return Error.INTERNAL_ERROR.response(String.format(
                            "Error when serializing managed zone %s in project %s", lastZoneName, projectId));
                }
            }
        }
        sizeReached = maxResults != null && maxResults.equals(serializedZones.size());
    }
    boolean includePageToken = hasMorePages
            && (fields == null || Arrays.asList(fields).contains("nextPageToken"));
    return toListResponse(serializedZones, "managedZones", lastZoneName, includePageToken);
}