001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2008
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.shared.security;
034:
035: import com.flexive.shared.FxArrayUtils;
036:
037: import java.io.Serializable;
038: import java.util.ArrayList;
039: import java.util.List;
040: import java.util.Arrays;
041:
042: /**
043: * A class for transporting and processing a list of groups.
044: *
045: * @author Gregor Schober (gregor.schober@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
046: */
047: public class UserGroupList implements Serializable {
048: private static final long serialVersionUID = -7462354889674331522L;
049:
050: private UserGroup[] list = null;
051:
052: /**
053: * Constructor
054: *
055: * @param groups the groups in the list
056: */
057: public UserGroupList(UserGroup[] groups) {
058: if (groups == null)
059: groups = new UserGroup[0];
060: this .list = FxArrayUtils.clone(groups);
061: }
062:
063: /**
064: * Constructor
065: */
066: public UserGroupList() {
067: this .list = new UserGroup[0];
068: }
069:
070: /**
071: * Returns the amount of groups in the list.
072: *
073: * @return the amount of groups in the list
074: */
075: public int size() {
076: return list.length;
077: }
078:
079: /**
080: * Returns all groups in the list.
081: *
082: * @return all groups in the list
083: */
084: public UserGroup[] getGroups() {
085: return FxArrayUtils.clone(list);
086: }
087:
088: /**
089: * Returns the group at the given position.
090: *
091: * @param pos the position
092: * @return the group at the given position
093: */
094: public UserGroup get(int pos) {
095: return this .list[pos];
096: }
097:
098: /**
099: * Returns a array holding the id's of all groups in the list.
100: *
101: * @return a array holding the id's of all groups in the list
102: */
103: public long[] toLongArray() {
104: long[] idArr = new long[list.length];
105: for (int i = 0; i < list.length; i++)
106: idArr[i] = list[i].getId();
107: return idArr;
108: }
109:
110: /**
111: * Returns a array holding the names of all groups in the list.
112: *
113: * @return a array holding the names of all groups in the list
114: */
115: public String[] toNameArray() {
116: if (list == null)
117: return new String[0];
118: String idArr[] = new String[list.length];
119: for (int i = 0; i < list.length; i++)
120: idArr[i] = list[i].getName();
121: return idArr;
122: }
123:
124: /**
125: * Removes a group from the list.
126: *
127: * @param group the group to remove from the list
128: */
129: public void remove(UserGroup group) {
130: remove(group.getId());
131: }
132:
133: /**
134: * Removes groups from the list.
135: *
136: * @param group the groups to remove from the list
137: */
138: public void remove(UserGroup[] group) {
139: for (UserGroup aGroup : group)
140: remove(aGroup.getId());
141: }
142:
143: /**
144: * Removes groups identified by the unique id from the list.
145: *
146: * @param groupId the groups to remove from the list
147: */
148: public void remove(long[] groupId) {
149: for (long aGroupId : groupId)
150: remove(aGroupId);
151: }
152:
153: /**
154: * Returns true if the group is contained in the group list.
155: *
156: * @param groupId the id of the group to look for
157: * @return true if the group is contained in the group list.
158: */
159: public boolean contains(long groupId) {
160: for (UserGroup aList : list)
161: if (aList.getId() == groupId)
162: return true;
163: return false;
164: }
165:
166: /**
167: * Removes a group identified by its unique id from the list.
168: *
169: * @param groupId the group to remove from the list
170: */
171: public void remove(long groupId) {
172: if (list != null) {
173: int elementFound = 0;
174: while (elementFound != -1) {
175: // Look for the elemenet
176: elementFound = -1;
177: for (int i = 0; i < list.length; i++) {
178: if (list[i].getId() == groupId) {
179: elementFound = i;
180: break;
181: }
182: }
183: // Delete the element
184: if (elementFound != -1) {
185: UserGroup tmp[] = new UserGroup[list.length - 1];
186: int pos = 0;
187: for (int i = 0; i < list.length; i++) {
188: if (i != elementFound)
189: tmp[pos++] = list[i];
190: }
191: list = tmp;
192: }
193: }
194: }
195: }
196:
197: public List<UserGroup> getList() {
198: ArrayList<UserGroup> result = new ArrayList<UserGroup>(
199: list == null ? 0 : list.length);
200: if (list != null) {
201: result.addAll(Arrays.asList(list));
202: }
203: return result;
204: }
205:
206: /**
207: * Returns a string representation of the groups.
208: *
209: * @return a string representation of the groups
210: */
211: @Override
212: public String toString() {
213: return UserGroupList.class + "@[" + toNameString() + "]";
214: }
215:
216: /**
217: * Returns a comma seperated list of the group names.
218: *
219: * @return a comma seperated list of the group names.
220: */
221: public String toNameString() {
222: String result = "";
223: for (UserGroup aList : list) {
224: if (result.length() > 0)
225: result += ",";
226: result += aList.getName();
227: }
228: return result;
229: }
230:
231: }
|