001/* 002 * (C) Copyright 2019 Nuxeo (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bdelbosc 018 */ 019package org.nuxeo.ecm.core.scroll; 020 021import java.util.Collections; 022import java.util.List; 023import java.util.Map; 024import java.util.NoSuchElementException; 025 026import org.nuxeo.ecm.core.api.scroll.Scroll; 027import org.nuxeo.ecm.core.api.scroll.ScrollRequest; 028 029import com.google.common.collect.Lists; 030 031/** 032 * Returns a static result list of identifiers. 033 * 034 * @since 11.1 035 */ 036public class StaticScroll implements Scroll { 037 038 protected StaticScrollRequest request; 039 040 protected List<List<String>> partitions; 041 042 protected int currentPosition; 043 044 protected int nextPosition; 045 046 @Override 047 public void init(ScrollRequest request, Map<String, String> options) { 048 if (!(request instanceof StaticScrollRequest)) { 049 throw new IllegalArgumentException("Requires a StaticScrollRequest"); 050 } 051 this.request = (StaticScrollRequest) request; 052 partitions = Lists.partition(this.request.getIdentifiers(), request.getSize()); 053 currentPosition = 0; 054 } 055 056 @Override 057 public boolean hasNext() { 058 return currentPosition < partitions.size(); 059 } 060 061 @Override 062 public List<String> next() { 063 if (currentPosition >= partitions.size()) { 064 throw new NoSuchElementException(); 065 } 066 return partitions.get(currentPosition++); 067 } 068 069 @Override 070 public void close() { 071 partitions = null; 072 } 073 074 @Override 075 public String toString() { 076 return "StaticScroll request: " + request + " currentPos: " + currentPosition + " nextPos: " + nextPosition 077 + " ids: " + partitions; 078 } 079}