001package ball.util.stream;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: Permutations.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/stream/Permutations.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import java.util.Collection;
024import java.util.List;
025import java.util.function.Predicate;
026import java.util.stream.Stream;
027
028/**
029 * {@link Stream} implementaion that provides all permutations of a
030 * {@link List}.
031 *
032 * @param       <T>             The {@link List} element type.
033 *
034 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
035 * @version $Revision: 5285 $
036 */
037public interface Permutations<T> extends Combinations<T> {
038
039    /**
040     * Method to get the {@link Stream} of permutations.
041     *
042     * @param   collection      The {@link Collection} of elements to
043     *                          permute.
044     * @param   <T>             The {@link Collection} element type.
045     *
046     * @return  The {@link Stream} of permutations.
047     */
048    public static <T> Stream<List<T>> of(Collection<T> collection) {
049        return of(null, collection);
050    }
051
052    /**
053     * Method to get the {@link Stream} of permutations.
054     *
055     * @param   predicate       The optional {@link Predicate} (may be
056     *                          {@code null}) specifying prerequisite
057     *                          requirement(s) for the combinations.  Any
058     *                          path that does not match will be pruned.
059     * @param   collection      The {@link Collection} of elements to
060     *                          permute.
061     * @param   <T>             The {@link Collection} element type.
062     *
063     * @return  The {@link Stream} of permutations.
064     */
065    public static <T> Stream<List<T>> of(Predicate<List<T>> predicate,
066                                         Collection<T> collection) {
067        int size = collection.size();
068
069        return Combinations.of(size, size, predicate, collection);
070    }
071}