package com.perforce.jdev.cm;
import com.perforce.api.Client;
import java.io.IOException;
import java.util.Iterator;
import java.util.NoSuchElementException;
import oracle.jdeveloper.cm.CMException;
import oracle.jdeveloper.cm.ConnectionManager;
import oracle.jdeveloper.cm.ConnectionWrapper;
/**
* Iterates over existing P4 connections.
*
* @author Paul Krause
* @version $Revision: #2 $ $Date: 2002/10/08 $
*/
public class P4ConnectionIterator implements Iterator {
private final String[] clientNames;
private int nameIndex = 0;
public P4ConnectionIterator() throws CMException, IOException {
final ConnectionManager cm = ConnectionManager.getInstance();
final String[] names = cm.getConnectionNamesForClass(P4ConnectionType.CLASS);
if (names.length == 0) {
final Client dc = P4ConnectionFactory.defaultClient();
clientNames = new String[] { dc.getName() };
return;
}
clientNames = names;
}
/**
* @return <code>true</code> if ConnectionManager has another connection
*/
public boolean hasNext() {
return nameIndex < clientNames.length;
}
/**
* Gets the next connection name.
* @return a <code>String</code> value
* @exception NoSuchElementException if out of connections
*/
public Object next() throws NoSuchElementException {
try {
return clientNames[nameIndex++];
} catch (ArrayIndexOutOfBoundsException e) {
throw new NoSuchElementException();
}
}
/**
* Describe <code>next</code> method here.
*
* @param presentation a <code>String</code> value
* @return an <code>Object</code> value
* @exception NoSuchElementException if no more connections
* @exception CMException if an unable to get ConnectionManager
* @exception IOException if an unable to get Connection
*/
public Object next(final String presentation)
throws NoSuchElementException, CMException, IOException
{
final ConnectionManager cm = ConnectionManager.getInstance();
final ConnectionWrapper cw = cm.getConnection((String) next());
return cw.getPresentation(presentation);
}
/** Not supported. */
public void remove() {
throw new UnsupportedOperationException();
}
}