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