statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningNodeView.java
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 264 d60370059c3c
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningNodeView.java	Thu Jan 07 22:34:17 2010 +0100
     1.3 @@ -0,0 +1,129 @@
     1.4 +/*
     1.5 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     1.6 + *
     1.7 + * The contents of this file are subject to the terms of either the GNU
     1.8 + * General Public License Version 2 only ("GPL") or the Common
     1.9 + * Development and Distribution License("CDDL") (collectively, the
    1.10 + * "License"). You may not use this file except in compliance with the
    1.11 + * License. You can obtain a copy of the License at
    1.12 + * http://www.netbeans.org/cddl-gplv2.html
    1.13 + * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    1.14 + * specific language governing permissions and limitations under the
    1.15 + * License.  When distributing the software, include this License Header
    1.16 + * Notice in each file and include the License file at
    1.17 + * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    1.18 + * particular file as subject to the "Classpath" exception as provided
    1.19 + * by Sun in the GPL Version 2 section of the License file that
    1.20 + * accompanied this code. If applicable, add the following below the
    1.21 + * License Header, with the fields enclosed by brackets [] replaced by
    1.22 + * your own identifying information:
    1.23 + * "Portions Copyrighted [year] [name of copyright owner]"
    1.24 + *
    1.25 + * Contributor(s):
    1.26 + *
    1.27 + * Portions Copyrighted 2010 Martin Rexa
    1.28 + */
    1.29 +
    1.30 +package cz.xelfi.quoridor.statistics;
    1.31 +
    1.32 +import cz.xelfi.quoridor.webidor.GameId;
    1.33 +import cz.xelfi.quoridor.webidor.GameStatus;
    1.34 +import cz.xelfi.quoridor.Move;
    1.35 +import java.util.Set;
    1.36 +import java.util.HashSet;
    1.37 +import java.util.Map;
    1.38 +import java.util.HashMap;
    1.39 +import java.util.List;
    1.40 +import java.util.ArrayList;
    1.41 +import javax.xml.bind.annotation.XmlElement;
    1.42 +import javax.xml.bind.annotation.XmlElementWrapper;
    1.43 +import javax.xml.bind.annotation.XmlRootElement;
    1.44 +//import javax.xml.bind.annotation.XmlAccessType;
    1.45 +//import javax.xml.bind.annotation.XmlAccessorType;
    1.46 +import javax.xml.bind.annotation.XmlAttribute;
    1.47 +
    1.48 +/**
    1.49 + *
    1.50 + * @author Martin Rexa
    1.51 + */
    1.52 +
    1.53 +@XmlRootElement
    1.54 +//@XmlAccessorType(XmlAccessType.FIELD)
    1.55 +public class OpeningNodeView {
    1.56 +    @XmlAttribute
    1.57 +    String code;
    1.58 +    @XmlElementWrapper(name="whiteGames")
    1.59 +    @XmlElement(name="gameId")
    1.60 +    List<GameId> whiteGames;
    1.61 +    @XmlElementWrapper(name="blackGames")
    1.62 +    @XmlElement(name="gameId")
    1.63 +    List<GameId> blackGames;
    1.64 +    Map<Move, OpeningNodeViewEntry> entries;
    1.65 +    boolean empty;
    1.66 +
    1.67 +    public OpeningNodeView(){
    1.68 +    }
    1.69 +
    1.70 +    public OpeningNodeView(String code){
    1.71 +        this.code = code;
    1.72 +        entries = new HashMap<Move, OpeningNodeViewEntry>();
    1.73 +        whiteGames = new ArrayList<GameId>();
    1.74 +        blackGames = new ArrayList<GameId>();
    1.75 +        empty = true;
    1.76 +    }
    1.77 +
    1.78 +    @XmlElement
    1.79 +    public String getNodeCode(){
    1.80 +        return code;
    1.81 +    }
    1.82 +
    1.83 +    public void processGame(Move m, String code, GameId gId){
    1.84 +        OpeningNodeViewEntry e = entries.get(m);
    1.85 +        if(e == null){
    1.86 +            e = new OpeningNodeViewEntry(m,code);
    1.87 +            entries.put(m, e);
    1.88 +        }
    1.89 +        e.processGame(gId);
    1.90 +        empty = false;
    1.91 +    }
    1.92 +
    1.93 +    @XmlElementWrapper(name="children")
    1.94 +    @XmlElement(name="item")
    1.95 +    public Set<OpeningNodeViewEntry> getChildren(){
    1.96 +        Set<OpeningNodeViewEntry> result = new HashSet<OpeningNodeViewEntry>();
    1.97 +        for(Map.Entry<Move, OpeningNodeViewEntry> e: entries.entrySet()){
    1.98 +            result.add(e.getValue());
    1.99 +        }
   1.100 +        return result;
   1.101 +    }
   1.102 +
   1.103 +
   1.104 +    public void addFinishedGame(GameId gId){
   1.105 +        if(gId.getStatus().equals(GameStatus.whiteWon))
   1.106 +            whiteGames.add(gId);
   1.107 +        if(gId.getStatus().equals(GameStatus.blackWon))
   1.108 +            blackGames.add(gId);
   1.109 +        empty = false;
   1.110 +    }
   1.111 +
   1.112 +    @XmlAttribute
   1.113 +    public int getWhiteCount(){
   1.114 +        return whiteGames.size();
   1.115 +    }
   1.116 +
   1.117 +    @XmlAttribute
   1.118 +    public int getBlackCount(){
   1.119 +        return blackGames.size();
   1.120 +    }
   1.121 +
   1.122 +
   1.123 +    @XmlAttribute
   1.124 +    public boolean getChildrenNotEmpty(){
   1.125 +        return !entries.isEmpty();
   1.126 +    }
   1.127 +
   1.128 +    public boolean isEmpty(){
   1.129 +        return this.empty;
   1.130 +    }
   1.131 +
   1.132 +}