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 */ 017package org.apache.commons.rng.sampling.distribution; 018 019import java.util.stream.IntStream; 020 021/** 022 * Sampler that generates values of type {@code int}. 023 * 024 * @since 1.0 025 */ 026@FunctionalInterface 027public interface DiscreteSampler { 028 /** 029 * Creates an {@code int} sample. 030 * 031 * @return a sample. 032 */ 033 int sample(); 034 035 /** 036 * Returns an effectively unlimited stream of {@code int} sample values. 037 * 038 * <p>The default implementation produces a sequential stream that repeatedly 039 * calls {@link #sample sample}(). 040 * 041 * @return a stream of {@code int} values. 042 * @since 1.5 043 */ 044 default IntStream samples() { 045 return IntStream.generate(this::sample).sequential(); 046 } 047 048 /** 049 * Returns a stream producing the given {@code streamSize} number of {@code int} 050 * sample values. 051 * 052 * <p>The default implementation produces a sequential stream that repeatedly 053 * calls {@link #sample sample}(); the stream is limited to the given {@code streamSize}. 054 * 055 * @param streamSize Number of values to generate. 056 * @return a stream of {@code int} values. 057 * @since 1.5 058 */ 059 default IntStream samples(long streamSize) { 060 return samples().limit(streamSize); 061 } 062}