-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ContactService.java
103 lines (87 loc) · 3.7 KB
/
ContactService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package com.vaadin.tutorial.addressbook.backend;
import org.apache.commons.beanutils.BeanUtils;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
/** Separate Java service class.
* Backend implementation for the address book application, with "detached entities"
* simulating real world DAO. Typically these something that the Java EE
* or Spring backend services provide.
*/
// Backend service class. This is just a typical Java backend implementation
// class and nothing Vaadin specific.
public class ContactService {
// Create dummy data by randomly combining first and last names
static String[] fnames = { "Peter", "Alice", "John", "Mike", "Olivia",
"Nina", "Alex", "Rita", "Dan", "Umberto", "Henrik", "Rene", "Lisa",
"Linda", "Timothy", "Daniel", "Brian", "George", "Scott",
"Jennifer" };
static String[] lnames = { "Smith", "Johnson", "Williams", "Jones",
"Brown", "Davis", "Miller", "Wilson", "Moore", "Taylor",
"Anderson", "Thomas", "Jackson", "White", "Harris", "Martin",
"Thompson", "Young", "King", "Robinson" };
private static ContactService instance;
public static ContactService createDemoService() {
if (instance == null) {
final ContactService contactService = new ContactService();
Random r = new Random(0);
Calendar cal = Calendar.getInstance();
for (int i = 0; i < 100; i++) {
Contact contact = new Contact();
contact.setFirstName(fnames[r.nextInt(fnames.length)]);
contact.setLastName(lnames[r.nextInt(fnames.length)]);
contact.setEmail(contact.getFirstName().toLowerCase() + "@"
+ contact.getLastName().toLowerCase() + ".com");
contact.setPhone("+ 358 555 " + (100 + r.nextInt(900)));
cal.set(1930 + r.nextInt(70),
r.nextInt(11), r.nextInt(28));
contact.setBirthDate(cal.getTime());
contactService.save(contact);
}
instance = contactService;
}
return instance;
}
private HashMap<Long, Contact> contacts = new HashMap<>();
private long nextId = 0;
public synchronized List<Contact> findAll(String stringFilter) {
ArrayList arrayList = new ArrayList();
for (Contact contact : contacts.values()) {
try {
boolean passesFilter = (stringFilter == null || stringFilter.isEmpty())
|| contact.toString().toLowerCase()
.contains(stringFilter.toLowerCase());
if (passesFilter) {
arrayList.add(contact.clone());
}
} catch (CloneNotSupportedException ex) {
Logger.getLogger(ContactService.class.getName()).log(
Level.SEVERE, null, ex);
}
}
Collections.sort(arrayList, new Comparator<Contact>() {
@Override
public int compare(Contact o1, Contact o2) {
return (int) (o2.getId() - o1.getId());
}
});
return arrayList;
}
public synchronized long count() {
return contacts.size();
}
public synchronized void delete(Contact value) {
contacts.remove(value.getId());
}
public synchronized void save(Contact entry) {
if (entry.getId() == null) {
entry.setId(nextId++);
}
try {
entry = (Contact) BeanUtils.cloneBean(entry);
} catch (Exception ex) {
throw new RuntimeException(ex);
}
contacts.put(entry.getId(), entry);
}
}