001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.auth;
019:
020: import java.io.Serializable;
021: import java.security.Principal;
022:
023: import org.apache.harmony.auth.internal.nls.Messages;
024:
025: /**
026: * This class represents a unix groups by its group id.
027: */
028: public class UnixNumericGroupPrincipal implements Serializable,
029: Principal {
030:
031: private static final long serialVersionUID = -535408497353506159L;
032:
033: // Group id
034: private long gid;
035: // Group name
036: private String gname;
037:
038: /**
039: * Creates the object using a String representation of gid.
040: * @param gid string representation of gid
041: * @param primary shows whether the group is primary
042: * throws NullPointerException if gid is null
043: */
044: public UnixNumericGroupPrincipal(String gid, boolean primary) {
045: if (gid == null) {
046: throw new NullPointerException(Messages
047: .getString("auth.07")); //$NON-NLS-1$
048: }
049: this .gid = Long.parseLong(gid);
050: }
051:
052: /**
053: * Creates the object using gid passed.
054: * @param gid gid
055: * @param primary shows whether the group is primary
056: */
057: public UnixNumericGroupPrincipal(long gid, boolean primary) {
058: this .gid = gid;
059: }
060:
061: /**
062: * Creates the object using gid and group's name passed.
063: * @param gid gid
064: * @param gname group name
065: * @param primary shows whether the group is primary
066: */
067: public UnixNumericGroupPrincipal(long gid, String gname,
068: boolean primary) {
069: this .gid = gid;
070: this .gname = gname;
071: }
072:
073: /**
074: * Returns String representation of the stored GID.
075: */
076: public String getName() {
077: return Long.toString(gid);
078: }
079:
080: /**
081: * Returns group name.
082: */
083: public String getObjectName() {
084: return gname;
085: }
086:
087: /**
088: * Returns numeric representation of the stored gid.
089: */
090: public long longValue() {
091: return gid;
092: }
093:
094: /**
095: * Returns String representation of this object.
096: */
097: @Override
098: public String toString() {
099: if (gname == null) {
100: return "UnixNumericGroupPrincipal, gid=" + gid; //$NON-NLS-1$
101: }
102: return "UnixNumericGroupPrincipal, gid=" + gid + "; name=" + gname; //$NON-NLS-1$ //$NON-NLS-2$
103: }
104:
105: /**
106: * Tests objects for equality.<br>
107: * The objects are considered equals if they both are of type
108: * UnixNumericGroupPrincipal and have the same gid.
109: */
110: @Override
111: public boolean equals(Object o) {
112: if (o instanceof UnixNumericGroupPrincipal) {
113: return ((UnixNumericGroupPrincipal) o).gid == gid;
114: }
115: return false;
116: }
117:
118: /**
119: * Returns hash code of this object.
120: */
121: @Override
122: public int hashCode() {
123: return (int) gid;
124: }
125: }
|