wsdor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Sat, 11 Sep 2010 17:04:51 +0200
changeset 256 1758a7727278
parent 164 webidor/src/main/java/cz/xelfi/quoridor/webidor/GameId.java@2949998db4f6
child 264 d60370059c3c
permissions -rw-r--r--
Splitting classes representing data types on the server into own module
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;
jtulach@164
    61
    @XmlAttribute
jtulach@164
    62
    private boolean finished;
jaroslav@48
    63
jaroslav@48
    64
    GameId() {
jaroslav@48
    65
    }
jaroslav@48
    66
jaroslav@48
    67
    public GameId(String first, String second) {
jtulach@78
    68
        this(first, second, new Date());
jtulach@78
    69
    }
jtulach@78
    70
    private GameId(String first, String second, Date d) {
jaroslav@48
    71
        this(
jaroslav@48
    72
            UUID.randomUUID().toString(),
jtulach@164
    73
            first, second, d, d, GameStatus.whiteMove, 0, false
jaroslav@48
    74
        );
jaroslav@48
    75
    }
jaroslav@48
    76
jtulach@164
    77
    public GameId(
jtulach@164
    78
        String id, String first, String second,
jtulach@164
    79
        Date started, Date last, GameStatus result,
jtulach@164
    80
        int comments, boolean finished
jtulach@164
    81
    ) {
jaroslav@48
    82
        this.white = first;
jaroslav@48
    83
        this.black = second;
jaroslav@48
    84
        this.id = id;
jtulach@78
    85
        this.started = started.getTime();
jtulach@78
    86
        this.modified = last.getTime();
jtulach@77
    87
        this.status = result;
jaroslav@131
    88
        this.comments = comments;
jtulach@164
    89
        this.finished = finished;
jaroslav@48
    90
    }
jaroslav@48
    91
jaroslav@48
    92
    public String getId() {
jaroslav@48
    93
        return id;
jaroslav@48
    94
    }
jaroslav@48
    95
jaroslav@48
    96
    public String getWhite() {
jaroslav@48
    97
        return white;
jaroslav@48
    98
    }
jaroslav@48
    99
jaroslav@48
   100
    public String getBlack() {
jaroslav@48
   101
        return black;
jaroslav@48
   102
    }
jaroslav@48
   103
jtulach@78
   104
    public long getStarted() {
jaroslav@48
   105
        return started;
jaroslav@48
   106
    }
jaroslav@48
   107
jtulach@78
   108
    public long getModified() {
jtulach@78
   109
        return modified;
jtulach@78
   110
    }
jtulach@78
   111
jtulach@77
   112
    public GameStatus getStatus() {
jtulach@77
   113
        return status;
jaroslav@48
   114
    }
jaroslav@96
   115
jaroslav@131
   116
    public int getComments() {
jaroslav@131
   117
        return comments;
jaroslav@131
   118
    }
jaroslav@131
   119
jtulach@164
   120
    public boolean isFinished() {
jtulach@164
   121
        return finished;
jtulach@164
   122
    }
jtulach@164
   123
jaroslav@96
   124
    private static final class NewestFirst implements Comparator<GameId> {
jaroslav@96
   125
        public int compare(GameId o1, GameId o2) {
jaroslav@96
   126
            if (o1 == o2) {
jaroslav@96
   127
                return 0;
jaroslav@96
   128
            }
jaroslav@96
   129
            long diff = o2.getModified() - o1.getModified();
jaroslav@96
   130
            if (diff != 0) {
jaroslav@96
   131
                return diff < 0 ? -1 : 1;
jaroslav@96
   132
            }
jaroslav@96
   133
            return o1.getId().compareTo(o2.getId());
jaroslav@96
   134
        }
jaroslav@96
   135
    }
jaroslav@48
   136
}