001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions
006: * are met:
007: *
008: * - Redistributions of source code must retain the above copyright
009: * notice, this list of conditions and the following disclaimer.getc
010: *
011: * - Redistribution in binary form must reproduce the above copyright
012: * notice, this list of conditions and the following disclaimer in
013: * the documentation and/or other materials provided with the
014: * distribution.
015: *
016: * Neither the name of Sun Microsystems, Inc. or the names of
017: * contributors may be used to endorse or promote products derived
018: * from this software without specific prior written permission.
019: *
020: * This software is provided "AS IS," without a warranty of any
021: * kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
022: * WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
023: * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
024: * EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES
025: * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR
026: * DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN
027: * OR ITS LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR
028: * FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL OR
029: * PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF
030: * LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE SOFTWARE,
031: * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
032: *
033: * You acknowledge that Software is not designed, licensed or intended
034: * any nuclear facility.
035: */
036: package com.sun.portal.community;
037:
038: import java.security.Principal;
039:
040: import java.util.Map;
041: import java.util.HashMap;
042: import java.util.Iterator;
043: import java.util.HashSet;
044: import java.util.Set;
045:
046: /**
047: * Uniquely identifies a community role.
048: */
049: public class RoleId implements Principal {
050: public static final RoleId OWNER_ROLE = new RoleId("owner");
051: public static final RoleId MEMBER_ROLE = new RoleId("member");
052: public static final RoleId VISITOR_ROLE = new RoleId("visitor");
053: public static final RoleId INVITED_ROLE = new RoleId("invited");
054: public static final RoleId REJECTED_ROLE = new RoleId("rejected");
055: public static final RoleId PENDING_ROLE = new RoleId("pending");
056: public static final RoleId BANNED_ROLE = new RoleId("banned");
057:
058: private static final Map ALL_ROLE_MAP;
059:
060: static {
061: ALL_ROLE_MAP = new HashMap();
062: ALL_ROLE_MAP.put(OWNER_ROLE.toString(), OWNER_ROLE);
063: ALL_ROLE_MAP.put(MEMBER_ROLE.toString(), MEMBER_ROLE);
064: ALL_ROLE_MAP.put(VISITOR_ROLE.toString(), VISITOR_ROLE);
065: ALL_ROLE_MAP.put(INVITED_ROLE.toString(), INVITED_ROLE);
066: ALL_ROLE_MAP.put(REJECTED_ROLE.toString(), REJECTED_ROLE);
067: ALL_ROLE_MAP.put(PENDING_ROLE.toString(), PENDING_ROLE);
068: ALL_ROLE_MAP.put(BANNED_ROLE.toString(), BANNED_ROLE);
069: }
070:
071: public static final Set ALL_ROLES = new HashSet(ALL_ROLE_MAP
072: .values());
073:
074: private String name;
075:
076: private RoleId(String name) {
077: this .name = name;
078: }
079:
080: public boolean equals(Object o) {
081: if (!(o instanceof RoleId)) {
082: return false;
083: }
084:
085: RoleId other = (RoleId) o;
086:
087: if (getName().equals(other.getName())) {
088: return true;
089: }
090:
091: return false;
092: }
093:
094: public String getName() {
095: return name;
096: }
097:
098: public int hashCode() {
099: return toString().hashCode();
100: }
101:
102: /**
103: * Get the string representation of this role ID.
104: */
105: public String toString() {
106: return name;
107: }
108:
109: /**
110: * Get the RoleId
111: * object based on the given string role ID.
112: */
113: public static RoleId valueOf(String r) {
114: return (RoleId) ALL_ROLE_MAP.get(r);
115: }
116:
117: /**
118: * Get the RoleId
119: * objects based on the given role ID strings.
120: */
121: public static Set valueSet(Set roleStrings) {
122: Set roleIds = new HashSet();
123: for (Iterator i = roleStrings.iterator(); i.hasNext();) {
124: String roleString = (String) i.next();
125: RoleId rid = RoleId.valueOf(roleString);
126: roleIds.add(rid);
127: }
128:
129: return roleIds;
130: }
131: }
|