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:
019: package org.apache.lenya.ac.impl;
020:
021: import java.util.Arrays;
022: import java.util.HashSet;
023: import java.util.Set;
024:
025: import org.apache.avalon.framework.logger.Logger;
026: import org.apache.lenya.ac.Accreditable;
027: import org.apache.lenya.ac.Group;
028: import org.apache.lenya.ac.Groupable;
029: import org.apache.lenya.ac.ItemManager;
030:
031: /**
032: * Abstract implementation for group members.
033: * @version $Id: AbstractGroupable.java 499783 2007-01-25 13:33:38Z andreas $
034: */
035: public abstract class AbstractGroupable extends AbstractItem implements
036: Groupable, Accreditable {
037:
038: /**
039: * Ctor.
040: * @param itemManager The item manager.
041: * @param logger The logger.
042: */
043: public AbstractGroupable(ItemManager itemManager, Logger logger) {
044: super (itemManager, logger);
045: }
046:
047: private Set groups = new HashSet();
048:
049: /**
050: * @see org.apache.lenya.ac.Groupable#addedToGroup(org.apache.lenya.ac.Group)
051: */
052: public void addedToGroup(Group group) {
053: assert group != null;
054: assert group.contains(this );
055: this .groups.add(group);
056: }
057:
058: /**
059: * @see org.apache.lenya.ac.Groupable#removedFromGroup(org.apache.lenya.ac.Group)
060: */
061: public void removedFromGroup(Group group) {
062: assert group != null;
063: assert !group.contains(this );
064: this .groups.remove(group);
065: }
066:
067: /**
068: * @see org.apache.lenya.ac.Groupable#getGroups()
069: */
070: public Group[] getGroups() {
071: return (Group[]) this .groups.toArray(new Group[this .groups
072: .size()]);
073: }
074:
075: /**
076: * Removes this groupable from all its groups.
077: */
078: public void removeFromAllGroups() {
079: Group[] _groups = getGroups();
080:
081: for (int i = 0; i < _groups.length; i++) {
082: _groups[i].remove(this );
083: }
084: }
085:
086: /**
087: * @see org.apache.lenya.ac.Accreditable#getAccreditables()
088: */
089: public Accreditable[] getAccreditables() {
090: Set accreditables = new HashSet();
091: accreditables.add(this );
092:
093: Group[] _groups = getGroups();
094:
095: for (int i = 0; i < _groups.length; i++) {
096: Accreditable[] groupAccreditables = _groups[i]
097: .getAccreditables();
098: accreditables.addAll(Arrays.asList(groupAccreditables));
099: }
100:
101: return (Accreditable[]) accreditables
102: .toArray(new Accreditable[accreditables.size()]);
103: }
104:
105: }
|