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