Line data Source code
1 : // Copyright 2008 Google Inc. 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 : // See the License for the specific language governing permissions and 13 : // limitations under the License. 14 : 15 : package com.google.gerrit.index.query; 16 : 17 : import static java.util.Objects.requireNonNull; 18 : 19 : import com.google.common.collect.ImmutableList; 20 : import java.util.Iterator; 21 : import java.util.List; 22 : 23 : /** 24 : * Result set for queries that run synchronously or for cases where the result is already known and 25 : * we just need to pipe it back through our interfaces. 26 : * 27 : * <p>If your implementation benefits from asynchronous execution (i.e. dispatching a query and 28 : * awaiting results only when {@link ResultSet#toList()} is called, consider using {@link 29 : * LazyResultSet}. 30 : */ 31 : public class ListResultSet<T> implements ResultSet<T> { 32 : private ImmutableList<T> results; 33 : 34 151 : public ListResultSet(List<T> r) { 35 151 : results = ImmutableList.copyOf(requireNonNull(r, "results can't be null")); 36 151 : } 37 : 38 : @Override 39 : public Iterator<T> iterator() { 40 120 : return toList().iterator(); 41 : } 42 : 43 : @Override 44 : public ImmutableList<T> toList() { 45 151 : if (results == null) { 46 0 : throw new IllegalStateException("Results already obtained"); 47 : } 48 151 : ImmutableList<T> r = results; 49 151 : results = null; 50 151 : return r; 51 : } 52 : 53 : @Override 54 : public void close() { 55 0 : results = null; 56 0 : } 57 : 58 : @Override 59 : public Object searchAfter() { 60 0 : return null; 61 : } 62 : }