statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningNodeView.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 09:46:43 +0200
changeset 266 15fcdfc4cd4a
parent 264 d60370059c3c
permissions -rw-r--r--
Using maven-license-plugin and updating all missing headers
     1 /**
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Martin Rexa
     4  *
     5  * This program is free software: you can redistribute it and/or modify
     6  * it under the terms of the GNU General Public License as published by
     7  * the Free Software Foundation, either version 3 of the License.
     8  *
     9  * This program is distributed in the hope that it will be useful,
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    12  * GNU General Public License for more details.
    13  *
    14  * You should have received a copy of the GNU General Public License
    15  * along with this program. Look for COPYING file in the top folder.
    16  * If not, see http://www.gnu.org/licenses/.
    17  */
    18 
    19 package cz.xelfi.quoridor.statistics;
    20 
    21 import cz.xelfi.quoridor.webidor.GameId;
    22 import cz.xelfi.quoridor.webidor.GameStatus;
    23 import cz.xelfi.quoridor.Move;
    24 import java.util.Set;
    25 import java.util.HashSet;
    26 import java.util.Map;
    27 import java.util.HashMap;
    28 import java.util.List;
    29 import java.util.ArrayList;
    30 import javax.xml.bind.annotation.XmlElement;
    31 import javax.xml.bind.annotation.XmlElementWrapper;
    32 import javax.xml.bind.annotation.XmlRootElement;
    33 //import javax.xml.bind.annotation.XmlAccessType;
    34 //import javax.xml.bind.annotation.XmlAccessorType;
    35 import javax.xml.bind.annotation.XmlAttribute;
    36 
    37 /**
    38  *
    39  * @author Martin Rexa
    40  */
    41 
    42 @XmlRootElement
    43 //@XmlAccessorType(XmlAccessType.FIELD)
    44 public class OpeningNodeView {
    45     @XmlAttribute
    46     String code;
    47     @XmlElementWrapper(name="whiteGames")
    48     @XmlElement(name="gameId")
    49     List<GameId> whiteGames;
    50     @XmlElementWrapper(name="blackGames")
    51     @XmlElement(name="gameId")
    52     List<GameId> blackGames;
    53     Map<Move, OpeningNodeViewEntry> entries;
    54     boolean empty;
    55 
    56     public OpeningNodeView(){
    57     }
    58 
    59     public OpeningNodeView(String code){
    60         this.code = code;
    61         entries = new HashMap<Move, OpeningNodeViewEntry>();
    62         whiteGames = new ArrayList<GameId>();
    63         blackGames = new ArrayList<GameId>();
    64         empty = true;
    65     }
    66 
    67     @XmlElement
    68     public String getNodeCode(){
    69         return code;
    70     }
    71 
    72     public void processGame(Move m, String code, GameId gId){
    73         OpeningNodeViewEntry e = entries.get(m);
    74         if(e == null){
    75             e = new OpeningNodeViewEntry(m,code);
    76             entries.put(m, e);
    77         }
    78         e.processGame(gId);
    79         empty = false;
    80     }
    81 
    82     @XmlElementWrapper(name="children")
    83     @XmlElement(name="item")
    84     public Set<OpeningNodeViewEntry> getChildren(){
    85         Set<OpeningNodeViewEntry> result = new HashSet<OpeningNodeViewEntry>();
    86         for(Map.Entry<Move, OpeningNodeViewEntry> e: entries.entrySet()){
    87             result.add(e.getValue());
    88         }
    89         return result;
    90     }
    91 
    92 
    93     public void addFinishedGame(GameId gId){
    94         if(gId.getStatus().equals(GameStatus.whiteWon))
    95             whiteGames.add(gId);
    96         if(gId.getStatus().equals(GameStatus.blackWon))
    97             blackGames.add(gId);
    98         empty = false;
    99     }
   100 
   101     @XmlAttribute
   102     public int getWhiteCount(){
   103         return whiteGames.size();
   104     }
   105 
   106     @XmlAttribute
   107     public int getBlackCount(){
   108         return blackGames.size();
   109     }
   110 
   111 
   112     @XmlAttribute
   113     public boolean getChildrenNotEmpty(){
   114         return !entries.isEmpty();
   115     }
   116 
   117     public boolean isEmpty(){
   118         return this.empty;
   119     }
   120 
   121 }