uk.org.retep.xmpp.muc.beans.BasicMucRoomSet Maven / Gradle / Ivy
/*
* Copyright (c) 1998-2010, Peter T Mount
* All rights reserved.
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
*
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
*
*
* You should have received a copy of the GNU General Public License along
* with this program. If not, see .
*
*
*
* GNU GENERAL PUBLIC LICENSE - CLASSPATH EXCEPTION
*
*
*
* Linking this library statically or dynamically with other modules
* is making a combined work based on this library. Thus, the terms
* and conditions of the GNU General Public License cover the whole
* combination.
*
*
*
* As a special exception, the copyright holders of this library give
* you permission to link this library with independent modules to
* produce an executable, regardless of the license terms of these
* independent modules, and to copy and distribute the resulting
* executable under terms of your choice, provided that you also meet,
* for each linked independent module, the terms and conditions of the
* license of that module.
*
*
*
* An independent module is a module which is either not derived from or based
* on this library, or a module who's classes extend those within this library
* as part of the implementation of the library.
*
*
*
* If you modify this library, you may extend this exception to your version
* of the library, but you are not obligated to do so. If you do not wish to
* do so, delete this exception statement from your version.
*
*/
package uk.org.retep.xmpp.muc.beans;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.annotation.concurrent.ThreadSafe;
import uk.org.retep.annotations.ReadLock;
import uk.org.retep.annotations.WriteLock;
import uk.org.retep.annotations.cluster.InstrumentedClass;
import uk.org.retep.util.concurrent.ReadWriteConcurrencySupport;
import uk.org.retep.util.messaging.MessageException;
import uk.org.retep.xmpp.JID;
import uk.org.retep.xmpp.muc.MucRoom;
import uk.org.retep.xmpp.muc.MucRoomSet;
/**
* A collection of BasicMucRoom instances
* @param Type of MucRoom contained in the set
* @author peter
*/
@ThreadSafe
@InstrumentedClass
public class BasicMucRoomSet
extends ReadWriteConcurrencySupport
implements MucRoomSet
{
private JID jid;
private final Map rooms = new HashMap();
private int capacity = 0;
private int maxRoomsReturned = 0;
public BasicMucRoomSet()
{
this( null );
}
public BasicMucRoomSet( final JID jid )
{
this.jid = jid;
}
@WriteLock
public void setJid( final JID jid )
{
this.jid = jid;
}
@ReadLock
@Override
public JID getJid()
{
return jid;
}
@ReadLock
@Override
public T get( final String name )
throws MessageException
{
return rooms.get( name );
}
@WriteLock
@Override
public boolean create( final String name )
throws MessageException
{
MucRoom room = rooms.get( name );
if( room == null )
{
// If we have a limit, are we full?
if( capacity > 0 && rooms.size() >= capacity )
{
return false;
}
rooms.put( name, createRoomImpl( name ) );
}
return true;
}
protected T createRoomImpl( final String name )
throws MessageException
{
final JID roomJid = jid.copyWithoutNodeOrResource().setNode( name );
@SuppressWarnings( "unchecked" )
final T room = (T) new BasicMucRoom( roomJid );
return room;
}
@WriteLock
@Override
public boolean add( final T room )
throws MessageException
{
final String name = room.getName();
if( !rooms.containsKey( name ) )
{
rooms.put( name, room );
return true;
}
else
{
return false;
}
}
@WriteLock
@Override
public boolean remove( final String name )
throws MessageException
{
return rooms.remove( name ) != null;
}
protected final Map getRooms()
{
return rooms;
}
@ReadLock
@Override
public Collection getRoomNames()
throws MessageException
{
if( capacity < 1 )
{
return new ArrayList( rooms.keySet() );
}
else
{
final List l = new ArrayList( capacity );
int i = capacity;
for( String name : rooms.keySet() )
{
l.add( name );
i--;
if( i < 1 )
{
break;
}
}
return l;
}
}
@ReadLock
@Override
public int getSize()
{
return rooms.size();
}
@ReadLock
@Override
public int getCapacity()
{
return capacity;
}
@WriteLock
@Override
public void setCapacity( final int capacity )
{
this.capacity = capacity;
}
@ReadLock
@Override
public int getMaxRoomsReturned()
{
return maxRoomsReturned;
}
@WriteLock
@Override
public void setMaxRoomsReturned( final int maxRoomsReturned )
{
this.maxRoomsReturned = maxRoomsReturned;
}
}