jthashtable.java
来自「Java Pattern Oriented Framework (Jt) 是为了」· Java 代码 · 共 341 行
JAVA
341 行
package Jt;
import java.util.*;
/**
* Handles hash tables.
*/
public class JtHashTable extends JtObject {
private static final long serialVersionUID = 1L;
public static final String JtCLASS_NAME = JtHashTable.class.getName();
public static final String JtGET = "JtGET";
public static final String JtPUT = "JtPUT";
public static final String JtCLEAR = "JtCLEAR";
public static final String JtGET_KEYS = "JtGET_KEYS";
protected HashMap hashmap = null;
public JtHashTable() {
}
/**
* Void operation.
*/
public void setSize (int size) {
}
/**
* Returns the number of elements in the hash table.
*/
public int getSize () {
return (hashmap != null ? hashmap.size (): 0);
}
/**
* Specifies the HashMap object used to represent this object
*/
public void setHashmap (HashMap hashmap) {
this.hashmap = hashmap; // void operation
}
/**
* Returns the HashMap.
*/
public HashMap getHashmap () {
return (hashmap);
}
private Object getKeys () {
JtIterator jit = new JtIterator ();
Collection col;
HashMap tmp;
tmp = getHashmap ();
if (tmp == null)
return (null);
col = tmp.keySet ();
if (col == null)
//if (col == null || (col.size () == 0))
return (null);
jit.setIterator (col.iterator ());
return jit;
}
/**
* Returns a JtIterator.
*/
public Object getIterator () {
JtIterator jit;
Collection values;
jit = new JtIterator ();
if (hashmap == null)
return (null);
values = hashmap.values ();
if (values == null)
return (null);
jit.setIterator(values.iterator ());
return (jit);
}
/**
* void operation.
*/
public void setIterator (Object iterator) {
}
// Broadcast a message
private void broadcastMessage (JtMessage msg)
{
Collection values;
Iterator it;
if (msg == null || hashmap == null)
return;
values = hashmap.values ();
if (values == null)
return;
it = values.iterator ();
while (it.hasNext()) {
sendMessage (it.next (), msg);
}
}
/**
* Process object messages.
* <ul>
* <li> JtPUT - Adds the object specified by msgContent to this hash table. It associates
* this object with the key specified by msgData
* <li> JtREMOVE_OBJECT - Removes the object specified by msgData.
* <li> JtCLEAR - Removes all the objects from this hash table
* <li> JtGET - Returns the value to which the specified key (msgData) is mapped to
* <li> JtBROADCAST - Broadcast the message specified by msgContent to all the objects
* in this hash table
* <li> JtGET_KEYS - Returns a JtIterator over the keys found in the hash table
* </ul>
* @param message Jt Message
*/
public Object processMessage (Object message) {
String msgid = null;
JtMessage e = (JtMessage) message;
Object content;
Object data;
if (e == null)
return null;
msgid = (String) e.getMsgId ();
if (msgid == null)
return null;
content = e.getMsgContent();
data = e.getMsgData ();
// Remove this object
if (msgid.equals (JtObject.JtREMOVE)) {
if (hashmap != null)
hashmap.clear();
return (null);
}
if (msgid.equals (JtHashTable.JtPUT)) {
// Add an object to the hash table
if (content == null) {
handleWarning
("JtHashTable.processMessage(JtPUT):adding null value");
//return (this);
}
if (hashmap == null)
hashmap = new HashMap ();
// col = new Hashtable ();
hashmap.put (data, content);
return (this);
}
if (msgid.equals (JtHashTable.JtGET)) {
if (data == null) {
handleWarning
("JtHashTable.processMessage(JtGET):invalid value (null)");
return (this);
}
if (hashmap == null)
return (null);
return (hashmap.get (data));
}
if (msgid.equals (JtHashTable.JtCLEAR)) {
if (hashmap != null) {
hashmap.clear ();
}
return (this);
}
if (msgid.equals (JtHashTable.JtREMOVE_OBJECT)) {
// Remove an object from the collection
if (data == null) {
handleWarning
("JtHashTable.processMessage(JtREMOVE_OBJECT):invalid key (null).");
return (null);
}
if (hashmap.get(data) == null) {
handleError ("element not found " + content);
return (null);
}
hashmap.remove (data);
return (null);
}
// Broadcast a message to all the members
// of the hash table
if (msgid.equals (JtObject.JtBROADCAST)) {
if (hashmap == null) {
return (this);
}
broadcastMessage ((JtMessage) content);
return (this);
}
if (msgid.equals (JtHashTable.JtGET_KEYS)) {
return (getKeys ());
}
return (super.processMessage (message));
}
/**
* Demonstrates the messages processed by this class.
*/
public static void main(String[] args) {
JtFactory factory = new JtFactory ();
JtMessage msg1;
Integer count;
// Create an instance of JtHashTable
factory.createObject (JtHashTable.JtCLASS_NAME, "hashtable");
factory.createObject (JtMessage.JtCLASS_NAME, "message");
factory.setValue ("message", "msgId", JtHashTable.JtPUT);
factory.setValue ("message", "msgContent", new Integer (1));
factory.setValue ("message", "msgData", "one");
// Add an object to the hashtable
factory.sendMessage ("hashtable", "message");
factory.setValue ("message", "msgId", JtHashTable.JtGET);
factory.setValue ("message", "msgData", "one");
System.err.println ("JtHashTable(JtGET):" + factory.sendMessage ("hashtable", "message"));
factory.setValue ("message", "msgId", JtHashTable.JtPUT);
factory.setValue ("message", "msgContent", new Integer (2));
factory.setValue ("message", "msgData", "two");
factory.sendMessage ("hashtable", "message");
factory.sendMessage ("hashtable", new JtMessage (JtObject.JtPRINT));
count = (Integer) factory.getValue ("hashtable", "size");
System.out.println ("count:" + count);
factory.setValue ("message", "msgId", JtHashTable.JtREMOVE_OBJECT);
factory.setValue ("message", "msgData", "two");
factory.sendMessage ("hashtable", "message");
count = (Integer) factory.getValue ("hashtable", "size");
if (count.intValue () == 1)
System.err.println ("JtHashTable(JtPUT):GO");
else
System.err.println ("JtHashTable:FAILED");
// Clear the hashtable
factory.setValue ("message", "msgId", JtHashTable.JtCLEAR);
factory.sendMessage ("hashtable", "message");
count = (Integer) factory.getValue ("hashtable", "size");
if (count.intValue() == 0)
System.err.println ("JtHashTable (JtCLEAR):GO");
else
System.err.println ("JtHashTable:FAILED");
msg1 = (JtMessage) factory.createObject ("Jt.JtMessage", "message1");
factory.setValue ("message1", "msgId", "JtOBJECT");
factory.setValue ("message", "msgId", JtObject.JtBROADCAST);
factory.setValue ("message", "msgContent", msg1);
factory.sendMessage ("hashtable", "message");
factory.removeObject ("hashtable");
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?