首页>代码>bootstrap+brite实现的html5的联系人增删改查入门教程>/ContactApp/src/main/java/com/example/contactapp/dao/ContactDao.java
package com.example.contactapp.dao;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import com.example.contactapp.entity.Contact;
import com.example.contactapp.entity.Group;
import com.google.inject.Singleton;

@Singleton
public class ContactDao {

    private long idseq = 0;
    private Map<Long,Contact> contactStore = new ConcurrentHashMap<Long, Contact>();
    private Comparator<Contact> contactComparator =new Comparator<Contact>(){
        @Override
        public int compare(Contact c1, Contact c2) {
            return (int) (c1.getId() - c2.getId()) ;
        }
        
    };
    
    
    
    public Map<Long, Contact> getContactStore() {
		return contactStore;
	}

	public ContactDao(){
        create("Bill","Gates");
        create("Larry","Page");
        create("Tim","Cook");
    }

    
    synchronized private long newId(){
        return idseq++;
    }
    
    
    public Contact create(String firstName, String lastName){
        Contact contact = new Contact();
        contact.setId(newId());
        contact.setFirstName(firstName);
        contact.setLastName(lastName);
        contact.setGroups(new ArrayList<Group>());
        
        contactStore.put(contact.getId(), contact);
        return contact;
    }
    
    public Contact get(Long id){
        return contactStore.get(id);
    }
    
    public List<Contact> list(){
        List<Contact> contactList = new ArrayList(contactStore.values());
        Collections.sort(contactList, contactComparator);
        return contactList;
    }
    
    public Contact update(Long id, String firstName, String lastName) {
    	//get the contact ,then update  
    	contactStore.get(id).setFirstName(firstName);
    	contactStore.get(id).setLastName(lastName);
    	return contactStore.get(id);
    }
    
    public void deleteGroup(Long groupId) {
    	Set<Long> ids = contactStore.keySet();
        for (Long id : ids) {
        	Contact contact = get(id);
        	ArrayList<Group> groups = contact.getGroups();
        	
        	if(groups.size() > 0) {
        		boolean flag = false;
            	int i;
            	for ( i = 0; i < groups.size(); i++) {
    				Group group = groups.get(i);
    				if(group.getId() == groupId) {
    					flag = true;
    					break;
    				}
    			}
            	
            	if(flag) {
            		groups.remove(i);
            	}
            	
        	}
        	
        }
    }
    
    public Contact setGroups(Long contactId, ArrayList<Group> groups) {
    	if(groups != null) {
    		contactStore.get(contactId).setGroups(groups);
    	} else {
    		contactStore.get(contactId).getGroups().clear();
    	}
    	return contactStore.get(contactId);
    }

    
    public Contact delete(Long contactId){
        return contactStore.remove(contactId);
    }

    
    
}
最近下载更多
天空灵  LV9 2021年12月14日
2251937068  LV5 2021年2月26日
骚气123456  LV7 2020年3月8日
卜旭凯  LV1 2019年8月19日
asdjkl2  LV13 2019年3月18日
123124gfgaereg  LV1 2019年3月13日
RobertWu  LV2 2018年12月17日
LHJ123  LV30 2017年11月6日
talang  LV3 2017年8月15日
sanstyle  LV2 2017年7月5日
最近浏览更多
tkggddm  LV3 1月27日
guoyan  LV12 2023年10月19日
adminadminsqwqe  LV7 2023年5月11日
Taoaqi  LV3 2023年2月12日
欠踹de背影  LV25 2023年2月7日
gala209  LV3 2023年2月3日
孟子大叔  LV7 2023年1月6日
kxjh星辰  LV6 2022年12月28日
3473084268  LV7 2022年12月12日
15342201772  LV7 2022年12月4日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友