1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java Sat Sep 11 17:04:51 2010 +0200
1.3 @@ -0,0 +1,136 @@
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 2009 Jaroslav Tulach
1.28 + */
1.29 +
1.30 +package cz.xelfi.quoridor.webidor;
1.31 +
1.32 +import java.util.Comparator;
1.33 +import java.util.Date;
1.34 +import java.util.UUID;
1.35 +import javax.xml.bind.annotation.XmlAccessType;
1.36 +import javax.xml.bind.annotation.XmlAccessorType;
1.37 +import javax.xml.bind.annotation.XmlAttribute;
1.38 +import javax.xml.bind.annotation.XmlID;
1.39 +import javax.xml.bind.annotation.XmlRootElement;
1.40 +
1.41 +/** Basic identification of a game.
1.42 + *
1.43 + * @author Jaroslav Tulach <jtulach@netbeans.org>
1.44 + */
1.45 +@XmlRootElement
1.46 +@XmlAccessorType(XmlAccessType.FIELD)
1.47 +public class GameId {
1.48 + public static final Comparator<GameId> NEWEST_FIRST = new NewestFirst();
1.49 +
1.50 + @XmlAttribute
1.51 + private String white;
1.52 + @XmlAttribute
1.53 + private String black;
1.54 + @XmlAttribute
1.55 + private long started;
1.56 + @XmlAttribute
1.57 + private long modified;
1.58 + @XmlAttribute
1.59 + private GameStatus status;
1.60 + @XmlID @XmlAttribute
1.61 + private String id;
1.62 + @XmlAttribute
1.63 + private int comments;
1.64 + @XmlAttribute
1.65 + private boolean finished;
1.66 +
1.67 + GameId() {
1.68 + }
1.69 +
1.70 + public GameId(String first, String second) {
1.71 + this(first, second, new Date());
1.72 + }
1.73 + private GameId(String first, String second, Date d) {
1.74 + this(
1.75 + UUID.randomUUID().toString(),
1.76 + first, second, d, d, GameStatus.whiteMove, 0, false
1.77 + );
1.78 + }
1.79 +
1.80 + public GameId(
1.81 + String id, String first, String second,
1.82 + Date started, Date last, GameStatus result,
1.83 + int comments, boolean finished
1.84 + ) {
1.85 + this.white = first;
1.86 + this.black = second;
1.87 + this.id = id;
1.88 + this.started = started.getTime();
1.89 + this.modified = last.getTime();
1.90 + this.status = result;
1.91 + this.comments = comments;
1.92 + this.finished = finished;
1.93 + }
1.94 +
1.95 + public String getId() {
1.96 + return id;
1.97 + }
1.98 +
1.99 + public String getWhite() {
1.100 + return white;
1.101 + }
1.102 +
1.103 + public String getBlack() {
1.104 + return black;
1.105 + }
1.106 +
1.107 + public long getStarted() {
1.108 + return started;
1.109 + }
1.110 +
1.111 + public long getModified() {
1.112 + return modified;
1.113 + }
1.114 +
1.115 + public GameStatus getStatus() {
1.116 + return status;
1.117 + }
1.118 +
1.119 + public int getComments() {
1.120 + return comments;
1.121 + }
1.122 +
1.123 + public boolean isFinished() {
1.124 + return finished;
1.125 + }
1.126 +
1.127 + private static final class NewestFirst implements Comparator<GameId> {
1.128 + public int compare(GameId o1, GameId o2) {
1.129 + if (o1 == o2) {
1.130 + return 0;
1.131 + }
1.132 + long diff = o2.getModified() - o1.getModified();
1.133 + if (diff != 0) {
1.134 + return diff < 0 ? -1 : 1;
1.135 + }
1.136 + return o1.getId().compareTo(o2.getId());
1.137 + }
1.138 + }
1.139 +}