001package ball.game.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Game Applications and Utilities
005 * $Id: CardTask.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-game/trunk/src/main/java/ball/game/ant/taskdefs/CardTask.java $
007 * %%
008 * Copyright (C) 2010 - 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.game.card.Card;
024import ball.game.card.poker.Evaluator;
025import ball.util.ant.taskdefs.AnnotatedAntTask;
026import ball.util.ant.taskdefs.AntTask;
027import ball.util.ant.taskdefs.ClasspathDelegateAntTask;
028import ball.util.ant.taskdefs.ConfigurableAntTask;
029import ball.util.ant.taskdefs.NotNull;
030import java.util.Arrays;
031import java.util.Collection;
032import java.util.List;
033import java.util.stream.Collectors;
034import lombok.Getter;
035import lombok.NoArgsConstructor;
036import lombok.Setter;
037import lombok.ToString;
038import lombok.experimental.Accessors;
039import org.apache.tools.ant.BuildException;
040import org.apache.tools.ant.Task;
041import org.apache.tools.ant.util.ClasspathUtils;
042
043import static lombok.AccessLevel.PROTECTED;
044
045/**
046 * Abstract {@link Card} {@link.uri http://ant.apache.org/ Ant}
047 * {@link Task} base class.
048 *
049 * {@ant.task}
050 *
051 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
052 * @version $Revision: 5285 $
053 */
054@NoArgsConstructor(access = PROTECTED)
055public abstract class CardTask extends Task
056                               implements AnnotatedAntTask,
057                                          ClasspathDelegateAntTask,
058                                          ConfigurableAntTask  {
059    @Getter @Setter @Accessors(chain = true, fluent = true)
060    private ClasspathUtils.Delegate delegate = null;
061
062    @Override
063    public void init() throws BuildException {
064        super.init();
065        ClasspathDelegateAntTask.super.init();
066        ConfigurableAntTask.super.init();
067    }
068
069    @Override
070    public void execute() throws BuildException {
071        super.execute();
072        AnnotatedAntTask.super.execute();
073    }
074
075    /**
076     * {@link.uri http://ant.apache.org/ Ant}
077     * {@link org.apache.tools.ant.Task} to evaluate a poker hand.
078     *
079     * {@ant.task}
080     */
081    @AntTask("poker-hand-evaluate")
082    @NoArgsConstructor @ToString
083    public static class PokerHandEvaluate extends CardTask {
084        @NotNull @Getter
085        private List<Card> cards = null;
086
087        public void setCards(String string) {
088            cards =
089                Arrays.stream(string.split("[, ]+"))
090                .map(t -> Card.parse(t))
091                .collect(Collectors.toList());
092        }
093
094        @Override
095        public void execute() throws BuildException {
096            super.execute();
097
098            try {
099                Evaluator evaluator = new EvaluatorImpl(getCards());
100
101                log(String.valueOf(evaluator.getRanking())
102                    + ": " + String.valueOf(evaluator.getHand()));
103            } catch (BuildException exception) {
104                throw exception;
105            } catch (Throwable throwable) {
106                throwable.printStackTrace();
107                throw new BuildException(throwable);
108            }
109        }
110
111        private class EvaluatorImpl extends Evaluator {
112            public EvaluatorImpl(Collection<Card> collection) {
113                super(collection);
114            }
115
116            @Override
117            public void accept(List<Card> list) {
118                super.accept(list);
119
120                log(String.valueOf(list) + " -> " + toString());
121            }
122        }
123    }
124}