1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.commons.rng.sampling; 19 20 import java.util.stream.Stream; 21 22 /** 23 * Sampler that generates values of a specified type. 24 * 25 * @param <T> Type of the sample. 26 * @since 1.4 27 */ 28 public interface ObjectSampler<T> { 29 /** 30 * Create an object sample. 31 * 32 * @return a sample. 33 */ 34 T sample(); 35 36 /** 37 * Returns an effectively unlimited stream of object sample values. 38 * 39 * <p>The default implementation produces a sequential stream that repeatedly 40 * calls {@link #sample sample}(). 41 * 42 * @return a stream of object values. 43 * @since 1.5 44 */ 45 default Stream<T> samples() { 46 return Stream.generate(this::sample).sequential(); 47 } 48 49 /** 50 * Returns a stream producing the given {@code streamSize} number of object 51 * sample values. 52 * 53 * <p>The default implementation produces a sequential stream that repeatedly 54 * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. 55 * 56 * @param streamSize Number of values to generate. 57 * @return a stream of object values. 58 * @since 1.5 59 */ 60 default Stream<T> samples(long streamSize) { 61 return samples().limit(streamSize); 62 } 63 }