statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTreeNode.java
branchstatistics-and-elo
changeset 178 4b78d4f028b3
child 191 c8a0edd2a392
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/statistics/src/main/java/cz/xelfi/quoridor/statistics/OpeningTreeNode.java	Thu Jan 07 22:34:17 2010 +0100
     1.3 @@ -0,0 +1,138 @@
     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.Game;
    1.33 +import cz.xelfi.quoridor.webidor.GameId;
    1.34 +import cz.xelfi.quoridor.webidor.GameStatus;
    1.35 +import cz.xelfi.quoridor.webidor.User;
    1.36 +import cz.xelfi.quoridor.Move;
    1.37 +import java.util.HashMap;
    1.38 +import java.util.Map;
    1.39 +import java.util.List;
    1.40 +import java.util.ArrayList;
    1.41 +import javax.xml.bind.annotation.XmlAttribute;
    1.42 +
    1.43 +/**
    1.44 + *
    1.45 + * @author Martin Rexa
    1.46 + */
    1.47 +public class OpeningTreeNode {
    1.48 +    @XmlAttribute
    1.49 +    String hashCode;
    1.50 +    List<GameId> gameIds;
    1.51 +    List<GameId> finishedGames;
    1.52 +    Map<Move, OpeningTreeNode> children;
    1.53 +
    1.54 +    public OpeningTreeNode(){
    1.55 +    }
    1.56 +
    1.57 +    public OpeningTreeNode(String hashCode){
    1.58 +        this.hashCode = hashCode;
    1.59 +        gameIds = new ArrayList<GameId>();
    1.60 +        finishedGames = new ArrayList<GameId>();
    1.61 +        children = new HashMap<Move,OpeningTreeNode>();
    1.62 +    }
    1.63 +
    1.64 +    public OpeningTreeNode addGame(Game game){
    1.65 +        gameIds.add(game.getId());
    1.66 +        return this;
    1.67 +    }
    1.68 +
    1.69 +    public OpeningTreeNode addFinishedGame(Game game){
    1.70 +        finishedGames.add(game.getId());
    1.71 +        return this;
    1.72 +    }
    1.73 +
    1.74 +    public OpeningTreeNode addGameId(GameId gameId){
    1.75 +        gameIds.add(gameId);
    1.76 +        return this;
    1.77 +    }
    1.78 +
    1.79 +    public OpeningTreeNode addChild(Move move, OpeningTreeNode child){
    1.80 +        children.put(move, child);
    1.81 +        return this;
    1.82 +    }
    1.83 +
    1.84 +    public OpeningNodeView getView(User user){
    1.85 +        return getView(user.getId());
    1.86 +    }
    1.87 +
    1.88 +    public OpeningNodeView getView(String userId){
    1.89 +        OpeningNodeView view = new OpeningNodeView(hashCode);
    1.90 +        for(Map.Entry<Move, OpeningTreeNode> e: children.entrySet()){
    1.91 +            for(GameId gId: e.getValue().gameIds){
    1.92 +                if(User.canSee(gId, userId)){
    1.93 +                    view.processGame(e.getKey(), e.getValue().hashCode, gId);
    1.94 +                }
    1.95 +            }
    1.96 +        }
    1.97 +        for(GameId gId: finishedGames){
    1.98 +            view.addFinishedGame(gId);
    1.99 +        }
   1.100 +        return view;
   1.101 +    }
   1.102 +
   1.103 +    public List<GameId> filterGames(String userId, GameStatus status){
   1.104 +        List<GameId> result = new ArrayList<GameId>();
   1.105 +        for(GameId gId: gameIds){
   1.106 +            if(User.canSee(gId, userId))
   1.107 +                if(gId.getStatus().equals(status))
   1.108 +                    result.add(gId);
   1.109 +        }
   1.110 +        return result;
   1.111 +    }
   1.112 +
   1.113 +    public List<GameId> getGameIds(){
   1.114 +        return gameIds;
   1.115 +    }
   1.116 +
   1.117 +//    @XmlAttribute
   1.118 +//    @XmlElement
   1.119 +    public String getHashCode(){
   1.120 +        return this.hashCode;
   1.121 +    }
   1.122 +
   1.123 +
   1.124 +    @Override
   1.125 +    public boolean equals(Object obj) {
   1.126 +        if (obj == null) {
   1.127 +            return false;
   1.128 +        }
   1.129 +        if (getClass() != obj.getClass()) {
   1.130 +            return false;
   1.131 +        }
   1.132 +        OpeningTreeNode other = (OpeningTreeNode)obj;
   1.133 +        return hashCode.equals(other.hashCode);
   1.134 +    }
   1.135 +
   1.136 +    @Override
   1.137 +    public int hashCode() {
   1.138 +        return hashCode.hashCode();
   1.139 +    }
   1.140 +
   1.141 +}