wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Tue, 14 Sep 2010 08:56:13 +0200
changeset 264 d60370059c3c
parent 256 1758a7727278
permissions -rw-r--r--
Changing headers to GPLv3
     1 /*
     2  * Quoridor server and related libraries
     3  * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
     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.webidor;
    20 
    21 import java.util.Comparator;
    22 import java.util.Date;
    23 import java.util.UUID;
    24 import javax.xml.bind.annotation.XmlAccessType;
    25 import javax.xml.bind.annotation.XmlAccessorType;
    26 import javax.xml.bind.annotation.XmlAttribute;
    27 import javax.xml.bind.annotation.XmlID;
    28 import javax.xml.bind.annotation.XmlRootElement;
    29 
    30 /** Basic identification of a game.
    31  *
    32  * @author Jaroslav Tulach <jtulach@netbeans.org>
    33  */
    34 @XmlRootElement
    35 @XmlAccessorType(XmlAccessType.FIELD)
    36 public class GameId {
    37     public static final Comparator<GameId> NEWEST_FIRST = new NewestFirst();
    38 
    39     @XmlAttribute
    40     private String white;
    41     @XmlAttribute
    42     private String black;
    43     @XmlAttribute
    44     private long started;
    45     @XmlAttribute
    46     private long modified;
    47     @XmlAttribute
    48     private GameStatus status;
    49     @XmlID @XmlAttribute
    50     private String id;
    51     @XmlAttribute
    52     private int comments;
    53     @XmlAttribute
    54     private boolean finished;
    55 
    56     GameId() {
    57     }
    58 
    59     public GameId(String first, String second) {
    60         this(first, second, new Date());
    61     }
    62     private GameId(String first, String second, Date d) {
    63         this(
    64             UUID.randomUUID().toString(),
    65             first, second, d, d, GameStatus.whiteMove, 0, false
    66         );
    67     }
    68 
    69     public GameId(
    70         String id, String first, String second,
    71         Date started, Date last, GameStatus result,
    72         int comments, boolean finished
    73     ) {
    74         this.white = first;
    75         this.black = second;
    76         this.id = id;
    77         this.started = started.getTime();
    78         this.modified = last.getTime();
    79         this.status = result;
    80         this.comments = comments;
    81         this.finished = finished;
    82     }
    83 
    84     public String getId() {
    85         return id;
    86     }
    87 
    88     public String getWhite() {
    89         return white;
    90     }
    91 
    92     public String getBlack() {
    93         return black;
    94     }
    95 
    96     public long getStarted() {
    97         return started;
    98     }
    99 
   100     public long getModified() {
   101         return modified;
   102     }
   103 
   104     public GameStatus getStatus() {
   105         return status;
   106     }
   107 
   108     public int getComments() {
   109         return comments;
   110     }
   111 
   112     public boolean isFinished() {
   113         return finished;
   114     }
   115 
   116     private static final class NewestFirst implements Comparator<GameId> {
   117         public int compare(GameId o1, GameId o2) {
   118             if (o1 == o2) {
   119                 return 0;
   120             }
   121             long diff = o2.getModified() - o1.getModified();
   122             if (diff != 0) {
   123                 return diff < 0 ? -1 : 1;
   124             }
   125             return o1.getId().compareTo(o2.getId());
   126         }
   127     }
   128 }