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