001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: PermutationsTask.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/ant/taskdefs/PermutationsTask.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 ball.util.stream.Permutations;
024import java.util.Collection;
025import lombok.NoArgsConstructor;
026import lombok.ToString;
027import org.apache.tools.ant.BuildException;
028
029import static lombok.AccessLevel.PROTECTED;
030
031/**
032 * Abstract {@link.uri http://ant.apache.org/ Ant}
033 * {@link org.apache.tools.ant.Task} to analyze {@link Permutations} of a
034 * {@link Collection}.
035 *
036 * {@ant.task}
037 *
038 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
039 * @version $Revision: 5285 $
040 */
041@NoArgsConstructor(access = PROTECTED)
042public abstract class PermutationsTask extends InstanceOfTask {
043
044    /**
045     * {@link.uri http://ant.apache.org/ Ant}
046     * {@link org.apache.tools.ant.Task} to count {@link Permutations}.
047     *
048     * {@ant.task}
049     */
050    @AntTask("permutations-count")
051    @NoArgsConstructor @ToString
052    public static class Count extends PermutationsTask {
053        @Override
054        public void execute() throws BuildException {
055            super.execute();
056
057            try {
058                Collection<?> collection = Collection.class.cast(instance);
059
060                log();
061                log(Permutations.of(collection).count() + " permutations");
062            } catch (BuildException exception) {
063                throw exception;
064            } catch (Throwable throwable) {
065                throwable.printStackTrace();
066                throw new BuildException(throwable);
067            }
068        }
069    }
070
071    /**
072     * {@link.uri http://ant.apache.org/ Ant}
073     * {@link org.apache.tools.ant.Task} to show the {@link Permutations}.
074     *
075     * {@ant.task}
076     */
077    @AntTask("permutations-of")
078    @NoArgsConstructor @ToString
079    public static class Of extends PermutationsTask {
080        @Override
081        public void execute() throws BuildException {
082            super.execute();
083
084            try {
085                Collection<?> collection = Collection.class.cast(instance);
086
087                log();
088
089                Permutations.of(collection)
090                    .forEach(t -> log(String.valueOf(t)));
091            } catch (BuildException exception) {
092                throw exception;
093            } catch (Throwable throwable) {
094                throwable.printStackTrace();
095                throw new BuildException(throwable);
096            }
097        }
098    }
099}