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
jaroslav@48
     1
/*
jaroslav@264
     2
 * Quoridor server and related libraries
jaroslav@264
     3
 * Copyright (C) 2009-2010 Jaroslav Tulach <jaroslav.tulach@apidesign.org>
jaroslav@48
     4
 *
jaroslav@264
     5
 * This program is free software: you can redistribute it and/or modify
jaroslav@264
     6
 * it under the terms of the GNU General Public License as published by
jaroslav@264
     7
 * the Free Software Foundation, either version 3 of the License.
jaroslav@48
     8
 *
jaroslav@264
     9
 * This program is distributed in the hope that it will be useful,
jaroslav@264
    10
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
jaroslav@264
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
jaroslav@264
    12
 * GNU General Public License for more details.
jaroslav@48
    13
 *
jaroslav@264
    14
 * You should have received a copy of the GNU General Public License
jaroslav@264
    15
 * along with this program. Look for COPYING file in the top folder.
jaroslav@264
    16
 * If not, see http://www.gnu.org/licenses/.
jaroslav@48
    17
 */
jaroslav@48
    18
jaroslav@48
    19
package cz.xelfi.quoridor.webidor;
jaroslav@48
    20
jaroslav@96
    21
import java.util.Comparator;
jaroslav@48
    22
import java.util.Date;
jaroslav@48
    23
import java.util.UUID;
jaroslav@48
    24
import javax.xml.bind.annotation.XmlAccessType;
jaroslav@48
    25
import javax.xml.bind.annotation.XmlAccessorType;
jaroslav@48
    26
import javax.xml.bind.annotation.XmlAttribute;
jaroslav@48
    27
import javax.xml.bind.annotation.XmlID;
jaroslav@48
    28
import javax.xml.bind.annotation.XmlRootElement;
jaroslav@48
    29
jaroslav@48
    30
/** Basic identification of a game.
jaroslav@48
    31
 *
jaroslav@48
    32
 * @author Jaroslav Tulach <jtulach@netbeans.org>
jaroslav@48
    33
 */
jaroslav@48
    34
@XmlRootElement
jaroslav@48
    35
@XmlAccessorType(XmlAccessType.FIELD)
jaroslav@48
    36
public class GameId {
jaroslav@96
    37
    public static final Comparator<GameId> NEWEST_FIRST = new NewestFirst();
jaroslav@96
    38
jaroslav@48
    39
    @XmlAttribute
jaroslav@48
    40
    private String white;
jaroslav@48
    41
    @XmlAttribute
jaroslav@48
    42
    private String black;
jaroslav@48
    43
    @XmlAttribute
jtulach@78
    44
    private long started;
jtulach@78
    45
    @XmlAttribute
jtulach@78
    46
    private long modified;
jaroslav@48
    47
    @XmlAttribute
jtulach@77
    48
    private GameStatus status;
jtulach@54
    49
    @XmlID @XmlAttribute
jaroslav@48
    50
    private String id;
jaroslav@131
    51
    @XmlAttribute
jaroslav@131
    52
    private int comments;
jtulach@164
    53
    @XmlAttribute
jtulach@164
    54
    private boolean finished;
jaroslav@48
    55
jaroslav@48
    56
    GameId() {
jaroslav@48
    57
    }
jaroslav@48
    58
jaroslav@48
    59
    public GameId(String first, String second) {
jtulach@78
    60
        this(first, second, new Date());
jtulach@78
    61
    }
jtulach@78
    62
    private GameId(String first, String second, Date d) {
jaroslav@48
    63
        this(
jaroslav@48
    64
            UUID.randomUUID().toString(),
jtulach@164
    65
            first, second, d, d, GameStatus.whiteMove, 0, false
jaroslav@48
    66
        );
jaroslav@48
    67
    }
jaroslav@48
    68
jtulach@164
    69
    public GameId(
jtulach@164
    70
        String id, String first, String second,
jtulach@164
    71
        Date started, Date last, GameStatus result,
jtulach@164
    72
        int comments, boolean finished
jtulach@164
    73
    ) {
jaroslav@48
    74
        this.white = first;
jaroslav@48
    75
        this.black = second;
jaroslav@48
    76
        this.id = id;
jtulach@78
    77
        this.started = started.getTime();
jtulach@78
    78
        this.modified = last.getTime();
jtulach@77
    79
        this.status = result;
jaroslav@131
    80
        this.comments = comments;
jtulach@164
    81
        this.finished = finished;
jaroslav@48
    82
    }
jaroslav@48
    83
jaroslav@48
    84
    public String getId() {
jaroslav@48
    85
        return id;
jaroslav@48
    86
    }
jaroslav@48
    87
jaroslav@48
    88
    public String getWhite() {
jaroslav@48
    89
        return white;
jaroslav@48
    90
    }
jaroslav@48
    91
jaroslav@48
    92
    public String getBlack() {
jaroslav@48
    93
        return black;
jaroslav@48
    94
    }
jaroslav@48
    95
jtulach@78
    96
    public long getStarted() {
jaroslav@48
    97
        return started;
jaroslav@48
    98
    }
jaroslav@48
    99
jtulach@78
   100
    public long getModified() {
jtulach@78
   101
        return modified;
jtulach@78
   102
    }
jtulach@78
   103
jtulach@77
   104
    public GameStatus getStatus() {
jtulach@77
   105
        return status;
jaroslav@48
   106
    }
jaroslav@96
   107
jaroslav@131
   108
    public int getComments() {
jaroslav@131
   109
        return comments;
jaroslav@131
   110
    }
jaroslav@131
   111
jtulach@164
   112
    public boolean isFinished() {
jtulach@164
   113
        return finished;
jtulach@164
   114
    }
jtulach@164
   115
jaroslav@96
   116
    private static final class NewestFirst implements Comparator<GameId> {
jaroslav@96
   117
        public int compare(GameId o1, GameId o2) {
jaroslav@96
   118
            if (o1 == o2) {
jaroslav@96
   119
                return 0;
jaroslav@96
   120
            }
jaroslav@96
   121
            long diff = o2.getModified() - o1.getModified();
jaroslav@96
   122
            if (diff != 0) {
jaroslav@96
   123
                return diff < 0 ? -1 : 1;
jaroslav@96
   124
            }
jaroslav@96
   125
            return o1.getId().compareTo(o2.getId());
jaroslav@96
   126
        }
jaroslav@96
   127
    }
jaroslav@48
   128
}