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 */
028public interface ObjectSampler<T> {
029    /**
030     * Create an object sample.
031     *
032     * @return a sample.
033     */
034    T sample();
035
036    /**
037     * Returns an effectively unlimited stream of object sample values.
038     *
039     * <p>The default implementation produces a sequential stream that repeatedly
040     * calls {@link #sample sample}().
041     *
042     * @return a stream of object values.
043     * @since 1.5
044     */
045    default Stream<T> samples() {
046        return Stream.generate(this::sample).sequential();
047    }
048
049    /**
050     * Returns a stream producing the given {@code streamSize} number of object
051     * sample values.
052     *
053     * <p>The default implementation produces a sequential stream that repeatedly
054     * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}.
055     *
056     * @param streamSize Number of values to generate.
057     * @return a stream of object values.
058     * @since 1.5
059     */
060    default Stream<T> samples(long streamSize) {
061        return samples().limit(streamSize);
062    }
063}