📄 addressbookmodel.java
字号:
package elegate.cn.edu.nju;
import java.io.*;
import java.util.*;
/**
* a model to store the address data
* @author Elegate,elegate@gmail.com
* @author cs department of NJU
*/
final public class AddressBookModel implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
/**
* hash map of the address entries
*/
private HashMap<Long,AddressEntry> addressEntries;
/**
* hash map of the group entries
*/
private HashMap<String,GroupEntry> groupEntries;
/**
* constructor to generate some data structure to save data
*
*/
public AddressBookModel()
{
this.addressEntries=new HashMap<Long,AddressEntry>();
this.groupEntries=new HashMap<String,GroupEntry>();
this.groupEntries.put("All",new GroupEntry("All"));
}
/**
* update the existing address entry
* @param addressEntry the new address entry
*/
public void updateAddressEntry(AddressEntry addressEntry)
{
long id=addressEntry.getID();
this.addressEntries.put(id,addressEntry);
ArrayList<GroupEntry> belongs=addressEntry.getGroupsBelongsTo();
for(GroupEntry ele:belongs)
{
this.groupEntries.get(ele.getGroupName()).addGroupMember(id);
}
}
/**
* add a member to a group
* @param groupName the group name
* @param id the id of the address entry
* @return if the group exists return true,false otherwise
*/
public boolean addGroupMember(String groupName,long id)
{
GroupEntry group=this.groupEntries.get(groupName);
if(group==null)
return false;
group.addGroupMember(id);
this.addressEntries.get(id).joinGroup(group);
return true;
}
/**
* add an address entry
* @param addressEntry add a new address entry to the model
* @return true if no same address entry exists,false otherwise
* @see #removeAddressEntry(long)
*/
public boolean addAddressEntry(AddressEntry addressEntry)
{
if(addressEntry==null)
return false;
long id=addressEntry.getID();
addressEntry.joinGroup(this.getGroupEntry("All"));
if(!this.addressEntries.containsKey(id))
{
this.addressEntries.put(id,addressEntry);
ArrayList<GroupEntry> belongs=addressEntry.getGroupsBelongsTo();
for(GroupEntry ele:belongs)
{
this.groupEntries.get(ele.getGroupName()).addGroupMember(id);
}
return true;
}
else
return false;
}
/**
* get the address entry with the specified id
* @param id the id of the address entry
* @return the address entry with the specified id
*/
public AddressEntry getAddressEntry(long id)
{
return this.addressEntries.get(id);
}
/**
* remove a group from the model
* @param group the group entry
* @return true if the group exists,false otherwise
*/
public boolean removeGroupEntry(GroupEntry group)
{
if(group==null)
return false;
if(group.getGroupName().equals("All"))
return false;
LinkedList<Long> members=group.getGroupMembers();
for(Long ele:members)
this.addressEntries.get(ele).quitGroup(group);
this.groupEntries.remove(group.getGroupName());
return true;
}
/**
* remove an address entry forever
* @param id the id of the address entry to be removed
* @return if the address entry exists return true,false otherwise
* @see #addAddressEntry(AddressEntry)
*/
public boolean removeAddressEntry(long id)
{
AddressEntry addressEntry
= this.addressEntries.remove(id);
if(addressEntry==null)
return false;
ArrayList<GroupEntry> belongs=addressEntry.getGroupsBelongsTo();
for(GroupEntry ele:belongs)
ele.removeMember(addressEntry.getID());
return true;
}
/**
* remove the address entry from the specified group
* @param groupName the group name
* @param id the address entry's id
* @return true if the address entry exists,false otherwise
*/
public boolean removeAddressEntryFromGroup(String groupName,long id)
{
if(groupName.equals("All"))
return false;
GroupEntry group=this.groupEntries.get(groupName);
if(group==null)
return false;
group.removeMember(id);
this.addressEntries.get(id).quitGroup(group);
return true;
}
/**
* get the collection of the all address entries
* @return the collection of the all address entries
*/
public Collection<AddressEntry> getAllAddressEntries()
{
return this.addressEntries.values();
}
/**
* get the specified group entry
* @param groupName the group name
* @return the group entry with the specified group name
* @see #addGroupEntry(String)
*/
public GroupEntry getGroupEntry(String groupName)
{
return this.groupEntries.get(groupName);
}
/**
* add a group to the group entries hash map
* @param groupName the group name
* @return if the group not exists return true,false otherwise
* @see #getGroupEntry(String)
*/
public boolean addGroupEntry(String groupName)
{
if(this.groupEntries.containsKey(groupName))
return false;
this.groupEntries.put(groupName,new GroupEntry(groupName));
return true;
}
/**
* get the collection of all the group members
* with the specified group name
* @param groupName the group name
* @return the collection of the all the group members
* with the specified group name
*/
public Collection<AddressEntry> getGroupMembers(String groupName)
{
LinkedList<AddressEntry> members=new LinkedList<AddressEntry>();
GroupEntry groupEntry=this.groupEntries.get(groupName);
LinkedList<Long> memberIdList=groupEntry.getGroupMembers();
if(memberIdList.size()==0)
return members;
Iterator i=memberIdList.iterator();
while(i.hasNext())
{
members.add(this.addressEntries.get(i.next()));
}
return members;
}
/**
* get all the group entries
* @return all the group entries
*/
public Collection<GroupEntry> getAllGroupEntries()
{
return this.groupEntries.values();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -