1 /*
2 * ====================
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
4 *
5 * Copyright 2008-2009 Sun Microsystems, Inc. All rights reserved.
6 *
7 * The contents of this file are subject to the terms of the Common Development
8 * and Distribution License("CDDL") (the "License"). You may not use this file
9 * except in compliance with the License.
10 *
11 * You can obtain a copy of the License at
12 * http://IdentityConnectors.dev.java.net/legal/license.txt
13 * See the License for the specific language governing permissions and limitations
14 * under the License.
15 *
16 * When distributing the Covered Code, include this CDDL Header Notice in each file
17 * and include the License file at identityconnectors/legal/license.txt.
18 * If applicable, add the following below this CDDL Header, with the fields
19 * enclosed by brackets [] replaced by your own identifying information:
20 * "Portions Copyrighted [year] [name of copyright owner]"
21 * ====================
22 */
23 package org.identityconnectors.framework.common.objects;
24
25 import static org.identityconnectors.framework.common.objects.NameUtil.nameHashCode;
26 import static org.identityconnectors.framework.common.objects.NameUtil.namesEqual;
27 import static org.identityconnectors.framework.common.objects.ObjectClassUtil.createSpecialName;
28
29 import java.util.Locale;
30
31 /**
32 * An instance of <code>ObjectClass</code>
33 * specifies a <i>category or type</i> of {@link ConnectorObject}.
34 * This class predefines some common object-classes,
35 * such as <code>ACCOUNT</code> and <code>GROUP</code>.
36 *
37 * @author Will Droste
38 * @version $Revision: 1.3 $
39 * @since 1.0
40 */
41 public final class ObjectClass {
42
43 // =======================================================================
44 // Basic Types--i.e., common values of the ObjectClass attribute.
45 // =======================================================================
46
47 /**
48 * This constant defines a specific
49 * {@linkplain #getObjectClassValue value of ObjectClass}
50 * that is reserved for {@link ObjectClass#ACCOUNT}.
51 */
52 public static final String ACCOUNT_NAME = createSpecialName("ACCOUNT");
53
54 /**
55 * This constant defines a specific
56 * {@linkplain #getObjectClassValue value of ObjectClass}
57 * that is reserved for {@link ObjectClass#GROUP}.
58 */
59 public static final String GROUP_NAME = createSpecialName("GROUP");
60
61 // =======================================================================
62 // Create only after all other static initializers
63 // =======================================================================
64
65 /**
66 * Represents a human being <i>in the context of a specific system or application</i>.
67 * <p>
68 * When an attribute matching this constant is found within a <code>ConnectorObject</code>,
69 * this indicates that the <code>ConnectorObject</code> represents a human being
70 * (actual or fictional) within the context of a specific system or application.
71 * <p>
72 * Generally, an Account object records characteristics of a human user
73 * (such as loginName, password, user preferences or access privileges)
74 * that are relevant only to (or primarily to) a specific system or application.
75 */
76 public static final ObjectClass ACCOUNT = new ObjectClass(ACCOUNT_NAME);
77
78 /**
79 * Represents a collection that contains an object (such as an account).
80 * <p>
81 * When an attribute matching this constant is found within a <code>ConnectorObject</code>,
82 * this indicates that the <code>ConnectorObject</code> represents a group.
83 */
84 public static final ObjectClass GROUP = new ObjectClass(GROUP_NAME);
85
86 private final String _type;
87
88 /**
89 * Create a custom object class.
90 *
91 * @param type
92 * string representation for the name of the object class.
93 */
94 public ObjectClass(String type) {
95 if ( type == null ) {
96 throw new IllegalArgumentException("Type cannot be null.");
97 }
98 _type = type;
99 }
100
101 /**
102 * Get the name of the object class.
103 * (For example, the name of {@link ObjectClass#ACCOUNT}
104 * is the value defined by {@link ObjectClass#ACCOUNT_NAME},
105 * which is <code>"__ACCOUNT__"</code>.)
106 */
107 public String getObjectClassValue() {
108 return _type;
109 }
110
111 /**
112 * Convenience method to build the display name key for
113 * an object class.
114 *
115 * @return The display name key.
116 */
117 public String getDisplayNameKey() {
118 return "MESSAGE_OBJECT_CLASS_"+_type.toUpperCase(Locale.US);
119 }
120
121 /**
122 * Determines if the 'name' matches this {@link ObjectClass}.
123 *
124 * @param name
125 * case-insensitive string representation of the ObjectClass's
126 * type.
127 * @return <code>true</code> if the case-insensitive name is equal to
128 * that of the one in this {@link ObjectClass}.
129 */
130 public boolean is(String name) {
131 return namesEqual(_type, name);
132 }
133
134 @Override
135 public int hashCode() {
136 return nameHashCode(_type);
137 }
138
139 @Override
140 public final boolean equals(Object obj) {
141 // test identity
142 if (this == obj) {
143 return true;
144 }
145 // test for null..
146 if (obj == null) {
147 return false;
148 }
149 // test that the exact class matches
150 if (!(getClass().equals(obj.getClass()))) {
151 return false;
152 }
153
154 ObjectClass other = (ObjectClass)obj;
155
156 if(!is(other.getObjectClassValue())) {
157 return false;
158 }
159 return true;
160 }
161
162 @Override
163 public String toString() {
164 return "ObjectClass: "+_type;
165 }
166
167 }