|
-
July 11th, 2007, 04:58 PM
#1
Issues deleting a value from a mutlimap.
I'm having an issue deleting from a map where the KEY is the Serial data member and i'm trying to delete where the KEY is somthing and the PackID is equal to a value.
here's me trying to do it:
Code:
When I run the following test:
public class MainTest
{
public static void main(String [] args)
{
MessageDB testDB = new MessageDB();
Message msg = null;
for(int i = 0; i < 2; i++)
{
msg = new Message();
msg.Serial = "333";
msg.Identifer = "UPDATE " + i;
testDB.add(msg);
}
testDB.delete(msg, 1);
testDB.print();
}
}
It will print out both messages, without deleting
the message with Serial 333, and PacketID = 1.
Here's the whole class including the add function:
Code:
package tester;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.io.*;
//this class should store all the Messages or "Events" and
//you can access them based on their Serial key.
public class MessageDB {
//database to hold the information
// holds the Alerts/messages
Map<String, List><Message>> AlertMap;
//Constructor
MessageDB() {
AlertMap = new HashMap<String, List><Message>>();
}
//print, outputs the contents of the hashMap
public void print() {
//want to print out the Key and all the Messages
//associated with that key
//print, outputs the contents of the hashMap
if(AlertMap.isEmpty())
System.out.println("Map Is Empty");
for (String key : AlertMap.keySet())
System.out.println("\n\nSerial (key): " + key
+ "\nValues in Map: \n" + AlertMap.get(key));
}
void delete(Message msg)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key))
{
System.out.println("Successfully removed: " + key);
AlertMap.remove(key);
}
}
}
void delete(Message msg, int packetID)
{
for (String key : AlertMap.keySet())
{
if(msg.Serial.equals(key) && msg.PacketID == packetID)
{
System.out.println("Successfully removed: " + key + "with PacketID: " + packetID);
AlertMap.remove(key);
break;
}
else
System.out.println("Couldn't find!");
}
}
void add(Message msg) {
//getting the position of the List by EntityID if avaiable
List<Message> AlertList = AlertMap.get(msg.Serial);
//checking to see if there is a unique Key already in the Map.
if (AlertList == null) {
//if there isnt a key in the map, add a new key, and a new List mapping
//to the key EntityID;
AlertList = new ArrayList<Message>();
AlertMap.put(msg.Serial, AlertList);
AlertList.add(msg);
} else {
//adding message to List
AlertList.add(msg);
}
}
}
Here's where I set the packetID, anytime a new
message is created it will automatically increment the number.
Code:
package server;
//this class will hold the Event/Message
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import java.util.ArrayList;
import java.util.Set;
import java.util.Iterator;
public class Message {
String Identifer;
String Serial;
String Node;
String NodeAlias;
String Manager;
String Agent;
String AlertGroup;
String AlertKey;
String Severity;
String Summary;
String StateChange;
String FirstOccurance;
String LastOccurance;
String InternalLast;
String EventId;
String LocalNodeAlias;
//these are datamembers that will be used during
//the sending of the message to the reciever
//PacketID should be incremented each time a new message is created
int SizeOfPacket;
String PacketType;
private int PacketID;
private static int nextPacketID = 1;
private static synchronized int getNextId()
{
return nextPacketID++;
}
Message() {
Identifer = "";
Serial = "";
Node = "";
NodeAlias = "";
Manager = "";
Agent = "";
AlertGroup = "";
AlertKey = "";
Severity = "";
Summary = "";
StateChange = "";
FirstOccurance = "";
LastOccurance = "";
InternalLast = "";
EventId = "";
LocalNodeAlias = "";
SizeOfPacket = 0;
PacketType = "";
//increment the message count
PacketID = getNextId();
}
void print() {
System.out.println("\nIdentifer: " + this.Identifer + '\n' + "Serial: "
+ this.Serial + '\n' + "Node: " + this.Node + '\n'
+ "NodeAlias: " + this.NodeAlias + '\n' + "Manager: "
+ this.Manager + '\n' + "Agent: " + this.Agent + '\n'
+ "AlertGroup: " + this.AlertGroup + '\n' + "AlertKey: "
+ this.AlertKey + '\n' + "Severity: " + this.Severity + '\n'
+ "Summary: " + this.Summary + '\n' + "StateChange: "
+ this.StateChange + '\n' + "FirstOccurance: "
+ this.FirstOccurance + '\n' + "LastOccurance: "
+ this.LastOccurance + '\n' + "InternalLast: "
+ this.InternalLast + '\n' + "EventId: " + this.EventId + '\n'
+ "LocalNodeAlias: " + this.LocalNodeAlias + '\n'
+ "PacketType: " + this.PacketType + '\n' + "SizeOfPacket: "
+ this.SizeOfPacket + '\n' + "PacketID: " + this.PacketID
+ "\n");
}
void printV2()
{
System.out.println("Identifer: " + this.Identifer + "," + "Serial: "
+ this.Serial + "," + "Node: " + this.Node + ","
+ "NodeAlias: " + this.NodeAlias + "," + "Manager: "
+ this.Manager + "," + "Agent: " + this.Agent + ","
+ "AlertGroup: " + this.AlertGroup + "," + "AlertKey: "
+ this.AlertKey + "," + "Severity: " + this.Severity + ","
+ "Summary: " + this.Summary + "," + "StateChange: "
+ this.StateChange + "," + "FirstOccurance: "
+ this.FirstOccurance + "," + "LastOccurance: "
+ this.LastOccurance + "," + "InternalLast: "
+ this.InternalLast + "," + "EventId: " + this.EventId + ","
+ "LocalNodeAlias: " + this.LocalNodeAlias + ","
+ "PacketType: " + this.PacketType + "," + "SizeOfPacket: "
+ this.SizeOfPacket + "," + "PacketID: " + this.PacketID
+ "\n");
}
public String toString() {
return ("PacketType: " + this.PacketType + " , " + "SizeOfPacket: "
+ this.SizeOfPacket + " , " + "PacketID: " + this.PacketID
+ ", " + "Identifer: " + this.Identifer + " , " + "Serial: "
+ this.Serial + " , " + "Node: " + this.Node + " , "
+ "NodeAlias: " + this.NodeAlias + " , " + "Manager: "
+ this.Manager + " , " + "Agent: " + this.Agent + " , "
+ "AlertGroup: " + this.AlertGroup + " , " + "AlertKey: "
+ this.AlertKey + " , " + "Severity: " + this.Severity + " , "
+ "Summary: " + this.Summary + "," + "StateChange: "
+ this.StateChange + " , " + "FirstOccurance: "
+ this.FirstOccurance + " , " + "LastOccurance: "
+ this.LastOccurance + " , " + "InternalLast: "
+ this.InternalLast + " , " + "EventId: " + this.EventId + " , "
+ "LocalNodeAlias: " + this.LocalNodeAlias);
}
}
Any help would be great!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|