001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * %%
006 * Copyright (C) 2008 - 2022 Allen D. Ball
007 * %%
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *      http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 * ##########################################################################
020 */
021import ball.util.stream.Permutations;
022import java.util.Collection;
023import lombok.NoArgsConstructor;
024import lombok.ToString;
025import org.apache.tools.ant.BuildException;
026
027import static lombok.AccessLevel.PROTECTED;
028
029/**
030 * Abstract {@link.uri http://ant.apache.org/ Ant}
031 * {@link org.apache.tools.ant.Task} to analyze {@link Permutations} of a
032 * {@link Collection}.
033 *
034 * {@ant.task}
035 *
036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
037 */
038@NoArgsConstructor(access = PROTECTED)
039public abstract class PermutationsTask extends InstanceOfTask {
040
041    /**
042     * {@link.uri http://ant.apache.org/ Ant}
043     * {@link org.apache.tools.ant.Task} to count {@link Permutations}.
044     *
045     * {@ant.task}
046     */
047    @AntTask("permutations-count")
048    @NoArgsConstructor @ToString
049    public static class Count extends PermutationsTask {
050        @Override
051        public void execute() throws BuildException {
052            super.execute();
053
054            try {
055                Collection<?> collection = Collection.class.cast(instance);
056
057                log();
058                log(Permutations.of(collection).count() + " permutations");
059            } catch (BuildException exception) {
060                throw exception;
061            } catch (Throwable throwable) {
062                throwable.printStackTrace();
063                throw new BuildException(throwable);
064            }
065        }
066    }
067
068    /**
069     * {@link.uri http://ant.apache.org/ Ant}
070     * {@link org.apache.tools.ant.Task} to show the {@link Permutations}.
071     *
072     * {@ant.task}
073     */
074    @AntTask("permutations-of")
075    @NoArgsConstructor @ToString
076    public static class Of extends PermutationsTask {
077        @Override
078        public void execute() throws BuildException {
079            super.execute();
080
081            try {
082                Collection<?> collection = Collection.class.cast(instance);
083
084                log();
085
086                Permutations.of(collection)
087                    .forEach(t -> log(String.valueOf(t)));
088            } catch (BuildException exception) {
089                throw exception;
090            } catch (Throwable throwable) {
091                throwable.printStackTrace();
092                throw new BuildException(throwable);
093            }
094        }
095    }
096}