-
Notifications
You must be signed in to change notification settings - Fork 0
/
CustomerRepository.java
56 lines (45 loc) · 1.78 KB
/
CustomerRepository.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
package one.microstream.android.data;
import android.content.Context;
import com.github.javafaker.Faker;
import java.nio.file.Path;
import one.microstream.reference.Lazy;
import one.microstream.storage.embedded.types.EmbeddedStorage;
import one.microstream.storage.embedded.types.EmbeddedStorageManager;
public class CustomerRepository {
private EmbeddedStorageManager storage;
private CustomerRoot customerRoot = new CustomerRoot();
private Faker faker;
public CustomerRepository(Context context) {
Path filesDir = context.getFilesDir().toPath();
storage = EmbeddedStorage.start(customerRoot, filesDir);
faker = new Faker();
}
public void clearLazyRefence(Integer customerId) {
Lazy<Customer> lazy = customerRoot.getCustomerMap().get(customerId);
if (lazy != null) {
lazy.clear();
}
}
public Customer findCustomerById(Integer id) {
Customer customer;
Lazy<Customer> lazyCustomer = customerRoot.getCustomerMap().get(id);
if (lazyCustomer != null) {
customer = lazyCustomer.get();
} else {
customer = new Customer();
customer.setFirstName(faker.name().firstName());
customer.setLastName(faker.name().lastName());
customer.setCity(faker.address().city());
customer.setStreet(faker.address().streetName());
customer.setStreetNumber(faker.address().streetAddressNumber());
customer.setZip(faker.address().zipCode());
customer.setId(id);
customerRoot.getCustomerMap().put(id, Lazy.Reference(customer));
storage.store(customerRoot.getCustomerMap());
}
return customer;
}
public void shutdownStorage() {
storage.shutdown();
}
}