webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sun, 25 Oct 2009 14:24:58 +0100
changeset 131 19e81456eef2
parent 96 2eeaa41236c3
child 164 2949998db4f6
permissions -rw-r--r--
Displaying the number of left comments for each game
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
     3  *
     4  * The contents of this file are subject to the terms of either the GNU
     5  * General Public License Version 2 only ("GPL") or the Common
     6  * Development and Distribution License("CDDL") (collectively, the
     7  * "License"). You may not use this file except in compliance with the
     8  * License. You can obtain a copy of the License at
     9  * http://www.netbeans.org/cddl-gplv2.html
    10  * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
    11  * specific language governing permissions and limitations under the
    12  * License.  When distributing the software, include this License Header
    13  * Notice in each file and include the License file at
    14  * nbbuild/licenses/CDDL-GPL-2-CP.  Sun designates this
    15  * particular file as subject to the "Classpath" exception as provided
    16  * by Sun in the GPL Version 2 section of the License file that
    17  * accompanied this code. If applicable, add the following below the
    18  * License Header, with the fields enclosed by brackets [] replaced by
    19  * your own identifying information:
    20  * "Portions Copyrighted [year] [name of copyright owner]"
    21  *
    22  * Contributor(s):
    23  *
    24  * Portions Copyrighted 2009 Jaroslav Tulach
    25  */
    26 
    27 package cz.xelfi.quoridor.webidor;
    28 
    29 import java.util.Comparator;
    30 import java.util.Date;
    31 import java.util.UUID;
    32 import javax.xml.bind.annotation.XmlAccessType;
    33 import javax.xml.bind.annotation.XmlAccessorType;
    34 import javax.xml.bind.annotation.XmlAttribute;
    35 import javax.xml.bind.annotation.XmlID;
    36 import javax.xml.bind.annotation.XmlRootElement;
    37 
    38 /** Basic identification of a game.
    39  *
    40  * @author Jaroslav Tulach <jtulach@netbeans.org>
    41  */
    42 @XmlRootElement
    43 @XmlAccessorType(XmlAccessType.FIELD)
    44 public class GameId {
    45     public static final Comparator<GameId> NEWEST_FIRST = new NewestFirst();
    46 
    47     @XmlAttribute
    48     private String white;
    49     @XmlAttribute
    50     private String black;
    51     @XmlAttribute
    52     private long started;
    53     @XmlAttribute
    54     private long modified;
    55     @XmlAttribute
    56     private GameStatus status;
    57     @XmlID @XmlAttribute
    58     private String id;
    59     @XmlAttribute
    60     private int comments;
    61 
    62     GameId() {
    63     }
    64 
    65     public GameId(String first, String second) {
    66         this(first, second, new Date());
    67     }
    68     private GameId(String first, String second, Date d) {
    69         this(
    70             UUID.randomUUID().toString(),
    71             first, second, d, d, GameStatus.whiteMove, 0
    72         );
    73     }
    74 
    75     public GameId(String id, String first, String second, Date started, Date last, GameStatus result, int comments) {
    76         this.white = first;
    77         this.black = second;
    78         this.id = id;
    79         this.started = started.getTime();
    80         this.modified = last.getTime();
    81         this.status = result;
    82         this.comments = comments;
    83     }
    84 
    85     public String getId() {
    86         return id;
    87     }
    88 
    89     public String getWhite() {
    90         return white;
    91     }
    92 
    93     public String getBlack() {
    94         return black;
    95     }
    96 
    97     public long getStarted() {
    98         return started;
    99     }
   100 
   101     public long getModified() {
   102         return modified;
   103     }
   104 
   105     public GameStatus getStatus() {
   106         return status;
   107     }
   108 
   109     public int getComments() {
   110         return comments;
   111     }
   112 
   113     private static final class NewestFirst implements Comparator<GameId> {
   114         public int compare(GameId o1, GameId o2) {
   115             if (o1 == o2) {
   116                 return 0;
   117             }
   118             long diff = o2.getModified() - o1.getModified();
   119             if (diff != 0) {
   120                 return diff < 0 ? -1 : 1;
   121             }
   122             return o1.getId().compareTo(o2.getId());
   123         }
   124     }
   125 }