001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017 018package org.apache.commons.rng.sampling; 019 020import java.util.stream.Stream; 021 022/** 023 * Sampler that generates values of a specified type. 024 * 025 * @param <T> Type of the sample. 026 * @since 1.4 027 */ 028@FunctionalInterface 029public interface ObjectSampler<T> { 030 /** 031 * Create an object sample. 032 * 033 * @return a sample. 034 */ 035 T sample(); 036 037 /** 038 * Returns an effectively unlimited stream of object sample values. 039 * 040 * <p>The default implementation produces a sequential stream that repeatedly 041 * calls {@link #sample sample}(). 042 * 043 * @return a stream of object values. 044 * @since 1.5 045 */ 046 default Stream<T> samples() { 047 return Stream.generate(this::sample).sequential(); 048 } 049 050 /** 051 * Returns a stream producing the given {@code streamSize} number of object 052 * sample values. 053 * 054 * <p>The default implementation produces a sequential stream that repeatedly 055 * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. 056 * 057 * @param streamSize Number of values to generate. 058 * @return a stream of object values. 059 * @since 1.5 060 */ 061 default Stream<T> samples(long streamSize) { 062 return samples().limit(streamSize); 063 } 064}