Java Code Examples for java.util.stream.Stream#skip()

The following examples show how to use java.util.stream.Stream#skip() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QueryExecutorImpl.java    From grakn with GNU Affero General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked") // All attribute values are comparable value types
private Stream<ConceptMap> filter(Filterable query, Stream<ConceptMap> answers) {
    if (query.sort().isPresent()) {
        Variable var = query.sort().get().var();
        Comparator<ConceptMap> comparator = (map1, map2) -> {
            Object val1 = map1.get(var).asAttribute().value();
            Object val2 = map2.get(var).asAttribute().value();

            if (val1 instanceof String) {
                return ((String) val1).compareToIgnoreCase((String) val2);
            } else {
                return ((Comparable<? super Comparable>) val1).compareTo((Comparable<? super Comparable>) val2);
            }
        };
        comparator = (query.sort().get().order() == Graql.Token.Order.DESC) ? comparator.reversed() : comparator;
        answers = answers.sorted(comparator);
    }
    if (query.offset().isPresent()) {
        answers = answers.skip(query.offset().get());
    }
    if (query.limit().isPresent()) {
        answers = answers.limit(query.limit().get());
    }
    return answers;
}
 
Example 2
Source File: BaseInMemoryRepository.java    From business with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public Stream<A> get(Specification<A> specification, Option... options) {
    Stream<A> stream = bucket.values()
            .stream()
            .filter(specification.asPredicate());
    for (Option option : options) {
        if (option instanceof OffsetOption) {
            stream = stream.skip(((OffsetOption) option).getOffset());
        } else if (option instanceof LimitOption) {
            stream = stream.limit(((LimitOption) option).getLimit());
        } else if (option instanceof SortOption) {
            stream = stream.sorted(((SortOption) option).buildComparator());
        }
    }
    return stream;
}
 
Example 3
Source File: SpelQueryEngine.java    From spring-data-keyvalue with Apache License 2.0 6 votes vote down vote up
private static <S> List<S> filterMatchingRange(List<S> source, @Nullable SpelCriteria criteria, long offset,
		int rows) {

	Stream<S> stream = source.stream();

	if (criteria != null) {
		stream = stream.filter(it -> evaluateExpression(criteria, it));
	}
	if (offset > 0) {
		stream = stream.skip(offset);
	}
	if (rows > 0) {
		stream = stream.limit(rows);
	}

	return stream.collect(Collectors.toList());
}
 
Example 4
Source File: FeatureSubset.java    From sis with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a stream of all features contained in this dataset.
 */
@Override
public Stream<AbstractFeature> features(final boolean parallel) throws DataStoreException {
    Stream<AbstractFeature> stream = source.features(parallel);
    /*
     * Apply offset.
     */
    final long offset = query.getOffset();
    if (offset > 0) {
        stream = stream.skip(offset);
    }
    /*
     * Apply limit.
     */
    final long limit = query.getLimit();
    if (limit >= 0) {
        stream = stream.limit(limit);
    }
    return stream;
}
 
Example 5
Source File: AttributeRepositorySecurityDecorator.java    From molgenis with GNU Lesser General Public License v3.0 6 votes vote down vote up
@Override
public Stream<Attribute> findAll(Query<Attribute> q) {
  if (currentUserIsSuOrSystem()) {
    return delegate().findAll(q);
  } else {
    Query<Attribute> qWithoutLimitOffset = new QueryImpl<>(q);
    qWithoutLimitOffset.offset(0).pageSize(Integer.MAX_VALUE);
    Stream<Attribute> attrs = delegate().findAll(qWithoutLimitOffset);
    Stream<Attribute> filteredAttrs = filterReadMetadataPermission(attrs);
    if (q.getOffset() > 0) {
      filteredAttrs = filteredAttrs.skip(q.getOffset());
    }
    if (q.getPageSize() > 0) {
      filteredAttrs = filteredAttrs.limit(q.getPageSize());
    }
    return filteredAttrs;
  }
}
 
Example 6
Source File: VaultQueryEngine.java    From spring-vault with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("unchecked")
public <T> Collection<T> execute(@Nullable VaultQuery vaultQuery, @Nullable Comparator<?> comparator, long offset,
		int rows, String keyspace, Class<T> type) {

	Stream<String> stream = getRequiredAdapter().doList(keyspace).stream();

	if (vaultQuery != null) {
		stream = stream.filter(vaultQuery::test);
	}

	if (comparator == null) {

		if (offset > 0) {
			stream = stream.skip(offset);
		}

		if (rows > 0) {
			stream = stream.limit(rows);
		}
	}

	Stream<T> typed = stream.map(it -> getRequiredAdapter().get(it, keyspace, type));

	if (comparator != null) {

		typed = typed.sorted((Comparator) comparator);

		if (offset > 0) {
			typed = typed.skip(offset);
		}

		if (rows > 0) {
			typed = typed.limit(rows);
		}
	}

	return typed.collect(Collectors.toCollection(ArrayList::new));
}
 
Example 7
Source File: ExplorableDatasetRest.java    From mobi with GNU Affero General Public License v3.0 5 votes vote down vote up
/**
 * Creates a Response which contains the proper paged details.
 *
 * @param uriInfo    The URI information of the request.
 * @param items      The total list of items that need to be sorted, limited, and offset.
 * @param comparator The Comparator which will be used to sort the items.
 * @param asc        Whether the sorting should be ascending or descending.
 * @param limit      The size of the page of items to return.
 * @param offset     The number of items to skip.
 * @param <T>        A class that extends Object.
 * @return A Response with a page of items that has been filtered, sorted, and limited and headers for the total
 *         size and links to the next and previous pages if present.
 */
private <T> Response createPagedResponse(UriInfo uriInfo, List<T> items, Comparator<T> comparator, boolean asc,
                                         int limit, int offset) {
    validatePaginationParams(limit, offset, items.size());
    Stream<T> stream = items.stream();
    if (!asc) {
        stream = stream.sorted(comparator.reversed());
    } else {
        stream = stream.sorted(comparator);
    }
    if (offset > 0) {
        stream = stream.skip(offset);
    }
    if (limit > 0) {
        stream = stream.limit(limit);
    }
    List<T> pagedItems = stream.collect(Collectors.toList());
    Response.ResponseBuilder builder = Response.ok(pagedItems).header("X-Total-Count", items.size());
    Links links = LinksUtils.buildLinks(uriInfo, pagedItems.size(), items.size(), limit, offset);
    if (links.getNext() != null) {
        builder = builder.link(links.getBase() + links.getNext(), "next");
    }
    if (links.getPrev() != null) {
        builder = builder.link(links.getBase() + links.getPrev(), "prev");
    }
    return builder.build();
}
 
Example 8
Source File: RestliUtils.java    From brooklin with BSD 2-Clause "Simplified" License 5 votes vote down vote up
/**
 * Applies a Paging Context to a Stream.
 * @param stream stream of elements to be paginated
 * @param pagingContext settings for pagination
 * @param <T> type of elements in the Stream
 */
public static <T> Stream<T> withPaging(Stream<T> stream, final PagingContext pagingContext) {
  if (pagingContext.hasStart()) {
    stream = stream.skip(pagingContext.getStart());
  }

  if (pagingContext.hasCount()) {
    stream = stream.limit(pagingContext.getCount());
  }

  return stream;
}
 
Example 9
Source File: StreamArtifactLocator.java    From packagedrone with Eclipse Public License 1.0 5 votes vote down vote up
private static <T extends ArtifactInformation> Stream<T> applyOptions ( Stream<T> stream, final SearchOptions options )
{
    if ( options.getSkip () > 0 )
    {
        stream = stream.skip ( options.getSkip () );
    }
    if ( options.getLimit () > 0 )
    {
        stream = stream.limit ( options.getLimit () );
    }
    return stream;
}
 
Example 10
Source File: AbstractStreamCollectionReader.java    From baleen with Apache License 2.0 5 votes vote down vote up
@Override
protected final Iterator<T> initializeIterator(UimaContext context) throws BaleenException {

  Stream<T> stream = initializeStream(context);

  if (skipDocuments > 0) {
    stream = stream.skip(skipDocuments);
  }

  if (maxDocuments > 0) {
    stream = stream.limit(maxDocuments);
  }

  return stream.iterator();
}
 
Example 11
Source File: SampleUserDirectoryProvider.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public List<UserEdit> searchExternalUsers(String criteria, int first, int last, UserFactory factory) {
	Stream<Info> stream = m_info.values().stream().filter(i -> i.contains(criteria));
	if (first != -1) {
		stream = stream.skip(first);
	}
	if (last != -1) {
		stream = stream.limit(last-first+1);
	}
	return stream.map(i -> {
		UserEdit edit = factory.newUser(i.id);
		return getUser(edit)?edit:null;
	}).filter(Objects::nonNull).collect(Collectors.toList());
}
 
Example 12
Source File: SampleUserDirectoryProvider.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public List<UserEdit> searchExternalUsers(String criteria, int first, int last, UserFactory factory) {
	Stream<Info> stream = m_info.values().stream().filter(i -> i.contains(criteria));
	if (first != -1) {
		stream = stream.skip(first);
	}
	if (last != -1) {
		stream = stream.limit(last-first+1);
	}
	return stream.map(i -> {
		UserEdit edit = factory.newUser(i.id);
		return getUser(edit)?edit:null;
	}).filter(Objects::nonNull).collect(Collectors.toList());
}
 
Example 13
Source File: PermissionCheckingDecorator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Stream<E> skipAndLimitStream(Stream<E> entityStream, Query<E> q) {
  if (q.getOffset() > 0) {
    entityStream = entityStream.skip(q.getOffset());
  }
  if (q.getPageSize() > 0) {
    entityStream = entityStream.limit(q.getPageSize());
  }
  return entityStream;
}
 
Example 14
Source File: AbstractRowLevelSecurityRepositoryDecorator.java    From molgenis with GNU Lesser General Public License v3.0 5 votes vote down vote up
private Stream<E> findAllPermitted(Query<E> query, Action action) {
  Query<E> qWithoutLimitOffset = new QueryImpl<>(query);
  qWithoutLimitOffset.offset(0).pageSize(Integer.MAX_VALUE);
  Stream<E> permittedEntityStream =
      delegate().findAll(qWithoutLimitOffset).filter(entity -> isActionPermitted(entity, action));
  if (query.getOffset() > 0) {
    permittedEntityStream = permittedEntityStream.skip(query.getOffset());
  }
  if (query.getPageSize() > 0) {
    permittedEntityStream = permittedEntityStream.limit(query.getPageSize());
  }
  return permittedEntityStream;
}