📄 portset.java
字号:
/*
|
| PortSet.java
|
| PortSet class
| James Shin Young
|
| Created: December 12, 1997
|
| Copyright (c) 1997 by James Shin Young and the Regents
| of the University of California. All rights reserved.
|
| Permission to use, copy, modify, and distribute this software
| and its documentation for NON-COMMERCIAL purposes and without
| fee is hereby granted provided that this copyright notice
| appears in all copies.
|
*/
package jcp;
import java.util.Vector;
import java.util.Enumeration;
/**
* @author James Shin Young
* @see <related class>
* @see <another related class>
*/
class PortSet
{
// Vector of ports in set
private Vector ports;
// Default initial size of set
private static int defaultSize=2;
///////////////////////////////////////////////////////////////////////////////
//* Constructors
public PortSet(int num) {
ports = new Vector(num);
}
public PortSet() {
this(defaultSize);
}
///////////////////////////////////////////////////////////////////////////////
//* Public methods
public int size() { return ports.size(); }
/**
* Check to see if given argument is a member of the set.
* @return true if port is a member of the set.
*/
public boolean isMember(Port port) {
return ports.contains(port);
}
/**
* @return Array of ports in set.
*/
public synchronized Enumeration ports() {
return ports.elements();
}
/**
* Return the port at the given index
*/
public Port elementAt(int index) {
try {
return (Port)ports.elementAt(index);
} catch (ArrayIndexOutOfBoundsException e) {
return null;
}
}
///////////////////////////////////////////////////////////////////////////////
//* Protected methods
/**
* Adds a port to the set.
* @return true if the port is added successfully. false if the port is
* already a member of this set.
* @param port The port to add.
*/
protected synchronized boolean add(Port port) {
if (!ports.contains(port)) {
if (port != null) {
ports.addElement(port);
}
return true;
} else {
return false;
}
}
/**
* Removes the port at the given index
*/
protected synchronized boolean remove(int index) {
try {
ports.removeElementAt(index);
return true;
} catch (ArrayIndexOutOfBoundsException e) {
return false;
}
}
/**
* Remove the given port from the set. Does nothing if the port
* is not a member of the set.
* @return true if the remove was performed.
* @param port The port to remove.
*/
protected synchronized boolean remove(Port port) {
return (ports.removeElement(port));
}
/**
* Erases contents of set.
*/
protected synchronized void clear() {
ports.setSize(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -