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