wsdor/src/main/java/cz/xelfi/quoridor/webidor/User.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 package cz.xelfi.quoridor.webidor;
    19 
    20 import java.util.ArrayList;
    21 import java.util.List;
    22 import java.util.Set;
    23 import java.util.TreeSet;
    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.XmlElement;
    28 import javax.xml.bind.annotation.XmlRootElement;
    29 import javax.xml.bind.annotation.XmlValue;
    30 
    31 /**
    32  *
    33  * @author Jaroslav Tulach <jtulach@netbeans.org>
    34  */
    35 @XmlRootElement
    36 @XmlAccessorType(XmlAccessType.NONE)
    37 public final class User extends Object {
    38     @XmlAttribute
    39     private String id;
    40     @XmlElement(name="property")
    41     private List<Property> properties;
    42     private transient Set<String> permissions;
    43 
    44     User() {
    45     }
    46 
    47     public User(String id) {
    48         this.id = id;
    49     }
    50 
    51     public void addProperty(String name, String value) {
    52         if (properties == null) {
    53             properties = new ArrayList<Property>();
    54         }
    55         properties.add(new Property(name, value));
    56     }
    57 
    58     public void addPermission(String permission) {
    59         if (permissions == null) {
    60             permissions = new TreeSet<String>();
    61         }
    62         permissions.add(permission);
    63     }
    64 
    65     public String getProperty(String name) {
    66         if (properties == null) {
    67             return null;
    68         }
    69         for (Property p : properties) {
    70             if (p.name.equals(name)) {
    71                 return p.value;
    72             }
    73         }
    74         return null;
    75     }
    76 
    77     public boolean hasPermission(String permission) {
    78         if (permissions == null) {
    79             return false;
    80         }
    81         return permissions.contains(permission);
    82     }
    83 
    84     public static boolean canSee(GameId gId, String userId) {
    85         if (!gId.isFinished()) {
    86             return true;
    87         }
    88         if (userId.equals(gId.getWhite())) {
    89             return true;
    90         }
    91         if (userId.equals(gId.getBlack())) {
    92             return true;
    93         }
    94         return false;
    95     }
    96 
    97     public boolean canSee(GameId gId){
    98         return canSee(gId, id);
    99     }
   100 
   101     public String getId() {
   102         return id;
   103     }
   104 
   105     public static final class Property {
   106         @XmlAttribute
   107         private String name;
   108         @XmlValue
   109         private String value;
   110 
   111         private Property() {
   112         }
   113 
   114         Property(String name, String value) {
   115             this.name = name;
   116             this.value = value;
   117         }
   118     }
   119 }