statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTreeNode.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 191 c8a0edd2a392
child 266 15fcdfc4cd4a
permissions -rw-r--r--
Changing headers to GPLv3
     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 package cz.xelfi.quoridor.statistics;
    19 
    20 import cz.xelfi.quoridor.webidor.Game;
    21 import cz.xelfi.quoridor.webidor.GameId;
    22 import cz.xelfi.quoridor.webidor.GameStatus;
    23 import cz.xelfi.quoridor.webidor.User;
    24 import cz.xelfi.quoridor.Move;
    25 import java.util.HashMap;
    26 import java.util.Map;
    27 import java.util.List;
    28 import java.util.ArrayList;
    29 import javax.xml.bind.annotation.XmlAttribute;
    30 
    31 /**
    32  *
    33  * @author Martin Rexa
    34  */
    35 public class OpeningTreeNode {
    36     @XmlAttribute
    37     String hashCode;
    38     List<GameId> gameIds;
    39     List<GameId> finishedGames;
    40     Map<Move, OpeningTreeNode> children;
    41 
    42     public OpeningTreeNode(){
    43     }
    44 
    45     public OpeningTreeNode(String hashCode){
    46         this.hashCode = hashCode;
    47         gameIds = new ArrayList<GameId>();
    48         finishedGames = new ArrayList<GameId>();
    49         children = new HashMap<Move,OpeningTreeNode>();
    50     }
    51 
    52     public OpeningTreeNode addGame(Game game){
    53         gameIds.add(game.getId());
    54         return this;
    55     }
    56 
    57     public OpeningTreeNode addFinishedGame(Game game){
    58         finishedGames.add(game.getId());
    59         return this;
    60     }
    61 
    62     public OpeningTreeNode addGameId(GameId gameId){
    63         gameIds.add(gameId);
    64         return this;
    65     }
    66 
    67     public OpeningTreeNode addChild(Move move, OpeningTreeNode child){
    68         children.put(move, child);
    69         return this;
    70     }
    71 
    72     public OpeningNodeView getView(User user){
    73         return getView(user.getId());
    74     }
    75 
    76     public OpeningNodeView getView(String userId){
    77         OpeningNodeView view = new OpeningNodeView(hashCode);
    78         for(Map.Entry<Move, OpeningTreeNode> e: children.entrySet()){
    79             for(GameId gId: e.getValue().gameIds){
    80                 if(User.canSee(gId, userId)){
    81                     view.processGame(e.getKey(), e.getValue().hashCode, gId);
    82                 }
    83             }
    84         }
    85         for(GameId gId: finishedGames){
    86             if(User.canSee(gId, userId)){
    87                 view.addFinishedGame(gId);
    88             }
    89         }
    90         return view;
    91     }
    92 
    93     public List<GameId> filterGames(String userId, GameStatus status){
    94         List<GameId> result = new ArrayList<GameId>();
    95         for(GameId gId: gameIds){
    96             if(User.canSee(gId, userId))
    97                 if(gId.getStatus().equals(status))
    98                     result.add(gId);
    99         }
   100         return result;
   101     }
   102 
   103     public List<GameId> getGameIds(){
   104         return gameIds;
   105     }
   106 
   107 //    @XmlAttribute
   108 //    @XmlElement
   109     public String getHashCode(){
   110         return this.hashCode;
   111     }
   112 
   113 
   114     @Override
   115     public boolean equals(Object obj) {
   116         if (obj == null) {
   117             return false;
   118         }
   119         if (getClass() != obj.getClass()) {
   120             return false;
   121         }
   122         OpeningTreeNode other = (OpeningTreeNode)obj;
   123         return hashCode.equals(other.hashCode);
   124     }
   125 
   126     @Override
   127     public int hashCode() {
   128         return hashCode.hashCode();
   129     }
   130 
   131 }