ObjectBox.java
2.07 KB
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
package com.polysoft.nafmii.database;
import com.polysoft.nafmii.database.entity.DataEntity;
import com.polysoft.nafmii.database.entity.DataEntity_;
import com.polysoft.nafmii.utils.CtxUtils;
import java.util.Date;
import java.util.List;
import io.objectbox.Box;
import io.objectbox.BoxStore;
import io.objectbox.EntityInfo;
import io.objectbox.Property;
import io.objectbox.query.QueryBuilder;
public abstract class ObjectBox<T> {
private static BoxStore mBoxStore;
public static synchronized BoxStore getStore() {
if (null == mBoxStore) {
mBoxStore = MyObjectBox.builder()
.androidContext(CtxUtils.getApplication())
.build();
}
return mBoxStore;
}
public static Box<DataEntity> getDataBox() {
return getStore().boxFor(DataEntity.class);
}
public static QueryBuilder<DataEntity> newQuery() {
return getDataBox().query();
}
public static DataEntity findById(long id) {
return newQuery().equal(DataEntity_.id, id).build().findFirst();
}
public static DataEntity findByName(String name) {
return newQuery().equal(DataEntity_.name, name).build().findFirst();
}
public static List<DataEntity> findList(DataEntity ...data) {
QueryBuilder<DataEntity> query = newQuery();
for (int i = 0; i < data.length; i++) {
query.equal(DataEntity_.name, data[i].getName());
if ((i + 1) < data.length) {
query.or();
}
}
return query.build().find();
}
public static void put(DataEntity ...data) {
for (DataEntity item : data) {
DataEntity entity = findByName(item.getName());
if (null != entity) {
entity.setUpdateDate(new Date());
entity.setValue(item.getValue());
}
getDataBox().put(item);
}
}
public static void delete(long... ids) {
getDataBox().remove(ids);
}
public static void delete(DataEntity... data) {
getDataBox().remove(data);
}
}