01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package org.apache.harmony.auth;
19:
20: import java.io.Serializable;
21: import java.security.Principal;
22:
23: import org.apache.harmony.auth.internal.nls.Messages;
24:
25: /**
26: * This class represents a unix user by its user id.
27: */
28: public class UnixNumericUserPrincipal implements Serializable,
29: Principal {
30:
31: private static final long serialVersionUID = -8301723108164907822L;
32:
33: // User id
34: private long uid;
35:
36: /**
37: * Creates the object using a String representation of uid.
38: * @param id string representation of uid
39: * throws NullPointerException if uid is null
40: */
41: public UnixNumericUserPrincipal(String uid) {
42: if (uid == null) {
43: throw new NullPointerException(Messages
44: .getString("auth.06")); //$NON-NLS-1$
45: }
46: this .uid = Long.parseLong(uid);
47: }
48:
49: /**
50: * Creates the object using uid passed.
51: * @param uid uid
52: */
53: public UnixNumericUserPrincipal(long uid) {
54: this .uid = uid;
55: }
56:
57: /**
58: * Returns String representation of the stored UID.
59: */
60: public String getName() {
61: return Long.toString(uid);
62: }
63:
64: /**
65: * Returns numeric representation of the stored uid.
66: */
67: public long longValue() {
68: return uid;
69: }
70:
71: /**
72: * Returns String representation of this object.
73: */
74: @Override
75: public String toString() {
76: return "UnixNumericUserPrincipal, uid=" + uid; //$NON-NLS-1$
77: }
78:
79: /**
80: * Tests objects for equality.<br>
81: * The objects are considered equals if they both are of type
82: * UnixNumericUserPrincipal and have the same uid.
83: */
84: @Override
85: public boolean equals(Object o) {
86: if (o instanceof UnixNumericUserPrincipal) {
87: return ((UnixNumericUserPrincipal) o).uid == uid;
88: }
89: return false;
90: }
91:
92: /**
93: * Returns hash code of this object.
94: */
95: @Override
96: public int hashCode() {
97: return (int) uid;
98: }
99: }
|