001: /*
002: * Copyright 2005 Sun Microsystems, Inc. All
003: * rights reserved. Use of this product is subject
004: * to license terms. Federal Acquisitions:
005: * Commercial Software -- Government Users
006: * Subject to Standard License Terms and
007: * Conditions.
008: *
009: * Sun, Sun Microsystems, the Sun logo, and Sun ONE
010: * are trademarks or registered trademarks of Sun Microsystems,
011: * Inc. in the United States and other countries.
012: */
013: package com.sun.portal.community.mc;
014:
015: import java.security.Principal;
016:
017: import java.util.Map;
018: import java.util.HashMap;
019: import java.util.Iterator;
020: import java.util.HashSet;
021: import java.util.Set;
022:
023: /**
024: * Uniquely identifies a community role.
025: * <br><br>
026: * There are three possible roles: owner, member, and visitor.
027: * These are defined by static members of this class. Community
028: * roles may not be defined by clients. This class does not have
029: * a public constructor to allow clients to create instances.
030: */
031: public class CMCRolePrincipal implements Principal, Comparable {
032: private String name;
033:
034: public static final CMCRolePrincipal OWNER_ROLE = new CMCRolePrincipal(
035: "owner");
036: public static final CMCRolePrincipal MEMBER_ROLE = new CMCRolePrincipal(
037: "member");
038: public static final CMCRolePrincipal VISITOR_ROLE = new CMCRolePrincipal(
039: "visitor");
040: public static final CMCRolePrincipal INVITED_ROLE = new CMCRolePrincipal(
041: "invited");
042: public static final CMCRolePrincipal REJECTED_ROLE = new CMCRolePrincipal(
043: "rejected");
044: public static final CMCRolePrincipal PENDING_ROLE = new CMCRolePrincipal(
045: "pending");
046: public static final CMCRolePrincipal BANNED_ROLE = new CMCRolePrincipal(
047: "banned");
048: public static final CMCRolePrincipal DISABLED_ROLE = new CMCRolePrincipal(
049: "disabled");
050: public static final CMCRolePrincipal DELETED_ROLE = new CMCRolePrincipal(
051: "deleted");
052:
053: private static final Map ALL_ROLE_MAP;
054:
055: static {
056: ALL_ROLE_MAP = new HashMap();
057: ALL_ROLE_MAP.put(OWNER_ROLE.toString(), OWNER_ROLE);
058: ALL_ROLE_MAP.put(MEMBER_ROLE.toString(), MEMBER_ROLE);
059: ALL_ROLE_MAP.put(VISITOR_ROLE.toString(), VISITOR_ROLE);
060: ALL_ROLE_MAP.put(INVITED_ROLE.toString(), INVITED_ROLE);
061: ALL_ROLE_MAP.put(REJECTED_ROLE.toString(), REJECTED_ROLE);
062: ALL_ROLE_MAP.put(PENDING_ROLE.toString(), PENDING_ROLE);
063: ALL_ROLE_MAP.put(BANNED_ROLE.toString(), BANNED_ROLE);
064: ALL_ROLE_MAP.put(DISABLED_ROLE.toString(), DISABLED_ROLE);
065: ALL_ROLE_MAP.put(DELETED_ROLE.toString(), DELETED_ROLE);
066: }
067:
068: public static final Set ALL_ROLES = new HashSet(ALL_ROLE_MAP
069: .values());
070:
071: private CMCRolePrincipal(String name) {
072: this .name = name;
073: }
074:
075: public boolean equals(Object o) {
076: if (!(o instanceof CMCRolePrincipal)) {
077: return false;
078: }
079:
080: CMCRolePrincipal other = (CMCRolePrincipal) o;
081:
082: if (getName().equals(other.getName())) {
083: return true;
084: }
085:
086: return false;
087: }
088:
089: public String getName() {
090: return name;
091: }
092:
093: public int hashCode() {
094: return toString().hashCode();
095: }
096:
097: /**
098: * Get the string representation of this community role.
099: */
100: public String toString() {
101: return name;
102: }
103:
104: /**
105: * Get the RolePrincipal
106: * object based on the given string community role
107: * string representation.
108: */
109: public static CMCRolePrincipal valueOf(String r) {
110: return (CMCRolePrincipal) ALL_ROLE_MAP.get(r);
111: }
112:
113: /**
114: * Get the RolePrincipals
115: * objects based on the given strings community role
116: * string representation.
117: */
118: public static Set valueSet(Set roles) {
119: Set rolePrincipals = new HashSet();
120: for (Iterator i = roles.iterator(); i.hasNext();) {
121: String role = (String) i.next();
122: CMCRolePrincipal rp = CMCRolePrincipal.valueOf(role);
123: rolePrincipals.add(rp);
124: }
125:
126: return rolePrincipals;
127: }
128:
129: public int compareTo(Object o) {
130: CMCRolePrincipal other = (CMCRolePrincipal) o;
131: return toString().compareTo(other.toString());
132: }
133: }
|