RoleId.java :  » Portal » Open-Portal » com » sun » portal » community » Java Open Source

Java Open Source » Portal » Open Portal 
Open Portal » com » sun » portal » community » RoleId.java
/*
 * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 *   notice, this list of conditions and the following disclaimer.getc
 *
 * - Redistribution in binary form must reproduce the above copyright
 *   notice, this list of conditions and the following disclaimer in
 *   the documentation and/or other materials provided with the
 *   distribution.
 *
 * Neither the name of Sun Microsystems, Inc. or the names of
 * contributors may be used to endorse or promote products derived
 * from this software without specific prior written permission.
 *
 * This software is provided "AS IS," without a warranty of any
 * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
 * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
 * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
 * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
 * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
 * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
 * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
 * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
 * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
 * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
 *
 * You acknowledge that Software is not designed, licensed or intended
 * any nuclear facility.
 */
package com.sun.portal.community;


import java.security.Principal;

import java.util.Map;
import java.util.HashMap;
import java.util.Iterator;
import java.util.HashSet;
import java.util.Set;

/**
 * Uniquely identifies a community role.
 */
public class RoleId implements Principal {
    public static final RoleId OWNER_ROLE = new RoleId("owner");
    public static final RoleId MEMBER_ROLE = new RoleId("member");
    public static final RoleId VISITOR_ROLE = new RoleId("visitor");
    public static final RoleId INVITED_ROLE = new RoleId("invited");
    public static final RoleId REJECTED_ROLE = new RoleId("rejected");
    public static final RoleId PENDING_ROLE = new RoleId("pending");
    public static final RoleId BANNED_ROLE = new RoleId("banned");
        
    private static final Map ALL_ROLE_MAP;

    static {
        ALL_ROLE_MAP = new HashMap();
        ALL_ROLE_MAP.put(OWNER_ROLE.toString(), OWNER_ROLE);
        ALL_ROLE_MAP.put(MEMBER_ROLE.toString(), MEMBER_ROLE);
        ALL_ROLE_MAP.put(VISITOR_ROLE.toString(), VISITOR_ROLE);
        ALL_ROLE_MAP.put(INVITED_ROLE.toString(), INVITED_ROLE);
        ALL_ROLE_MAP.put(REJECTED_ROLE.toString(), REJECTED_ROLE);
        ALL_ROLE_MAP.put(PENDING_ROLE.toString(), PENDING_ROLE);
        ALL_ROLE_MAP.put(BANNED_ROLE.toString(), BANNED_ROLE);
    }
    
    public static final Set ALL_ROLES = new HashSet(ALL_ROLE_MAP.values());
    
    private String name;
        
    private RoleId(String name) {
        this.name = name;
    }
    
    public boolean equals(Object o) {
        if (!(o instanceof RoleId)) {
            return false;
        }
        
        RoleId other = (RoleId)o;
        
        if (getName().equals(other.getName())) {
            return true;
        }
        
        return false;
    }
    
    public String getName() {
        return name;
    }
    
    public int hashCode() {
        return toString().hashCode();
    }
    
    /**
     * Get the string representation of this role ID.
     */
    public String toString() {
        return name;
    }
    
    /**
     * Get the RoleId 
     * object based on the given string role ID.
     */
    public static RoleId valueOf(String r) {
        return (RoleId)ALL_ROLE_MAP.get(r);
    }
    
    /**
     * Get the RoleId 
     * objects based on the given role ID strings.
     */
    public static Set valueSet(Set roleStrings) {
        Set roleIds = new HashSet();
        for (Iterator i = roleStrings.iterator(); i.hasNext(); ) {
            String roleString = (String)i.next();
            RoleId rid = RoleId.valueOf(roleString);
            roleIds.add(rid);
        }
        
        return roleIds;
    }
}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.