package org.jboss.util.property;
import java.util.Properties;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.util.Set;
import java.util.HashSet;
import java.util.Iterator;
import org.jboss.util.NullArgumentException;
public class PropertyGroup
extends PropertyMap
{
protected final String basename;
public PropertyGroup(final String basename, final Properties container) {
super(container);
if (basename == null)
throw new NullArgumentException("basename");
this.basename = basename;
}
public final String getBaseName() {
return basename;
}
private String makePropertyName(final String suffix) {
return basename + PropertyMap.PROPERTY_NAME_SEPARATOR + suffix;
}
private String makePropertyName(final Object suffix) {
return makePropertyName(String.valueOf(suffix));
}
public boolean containsKey(final Object name) {
if (name == null)
throw new NullArgumentException("name");
return super.containsKey(makePropertyName(name));
}
public Object put(final Object name, final Object value) {
if (name == null)
throw new NullArgumentException("name");
return super.put(makePropertyName(name), value);
}
public Object get(final Object name) {
if (name == null)
throw new NullArgumentException("name");
return super.get(makePropertyName(name));
}
public Object remove(final Object name) {
if (name == null)
throw new NullArgumentException("name");
return super.remove(makePropertyName(name));
}
public Set entrySet()
{
final Set superSet = super.entrySet(true);
return new java.util.AbstractSet()
{
private boolean isInGroup(Map.Entry entry)
{
String key = (String)entry.getKey();
return key.startsWith(basename);
}
public int size()
{
Iterator iter = superSet.iterator();
int count = 0;
while (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
if (isInGroup(entry)) {
count++;
}
}
return count;
}
public Iterator iterator()
{
return new Iterator()
{
private Iterator iter = superSet.iterator();
private Object next;
public boolean hasNext()
{
if (next != null)
return true;
while (next == null) {
if (iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
if (isInGroup(entry)) {
next = entry;
return true;
}
}
else {
break;
}
}
return false;
}
public Object next()
{
if (next == null)
throw new java.util.NoSuchElementException();
Object obj = next;
next = null;
return obj;
}
public void remove()
{
iter.remove();
}
};
}
};
}
protected void addPropertyListener(final BoundPropertyListener listener) {
String name = makePropertyName(listener.getPropertyName());
List list = (List)boundListeners.get(name);
if (list == null) {
list = new ArrayList();
boundListeners.put(name, list);
}
if (! list.contains(listener)) {
list.add(listener);
listener.propertyBound(this);
}
}
protected boolean removePropertyListener(final BoundPropertyListener listener) {
String name = makePropertyName(listener.getPropertyName());
List list = (List)boundListeners.get(name);
boolean removed = false;
if (list != null) {
removed = list.remove(listener);
if (removed) listener.propertyUnbound(this);
}
return removed;
}
}