]> nmode's Git Repositories - signal-cli/blob - lib/src/main/java/org/asamk/signal/manager/helper/Context.java
Cleanup manager package
[signal-cli] / lib / src / main / java / org / asamk / signal / manager / helper / Context.java
1 package org.asamk.signal.manager.helper;
2
3 import org.asamk.signal.manager.AttachmentStore;
4 import org.asamk.signal.manager.AvatarStore;
5 import org.asamk.signal.manager.JobExecutor;
6 import org.asamk.signal.manager.SignalDependencies;
7 import org.asamk.signal.manager.storage.stickerPacks.StickerPackStore;
8 import org.asamk.signal.manager.storage.SignalAccount;
9
10 import java.util.function.Supplier;
11
12 public class Context {
13
14 private final Object LOCK = new Object();
15
16 private final SignalAccount account;
17 private final SignalDependencies dependencies;
18 private final AvatarStore avatarStore;
19 private final StickerPackStore stickerPackStore;
20 private final AttachmentStore attachmentStore;
21 private final JobExecutor jobExecutor;
22
23 private AccountHelper accountHelper;
24 private AttachmentHelper attachmentHelper;
25 private ContactHelper contactHelper;
26 private GroupHelper groupHelper;
27 private GroupV2Helper groupV2Helper;
28 private IdentityHelper identityHelper;
29 private IncomingMessageHandler incomingMessageHandler;
30 private PinHelper pinHelper;
31 private PreKeyHelper preKeyHelper;
32 private ProfileHelper profileHelper;
33 private ReceiveHelper receiveHelper;
34 private RecipientHelper recipientHelper;
35 private SendHelper sendHelper;
36 private StickerHelper stickerHelper;
37 private StorageHelper storageHelper;
38 private SyncHelper syncHelper;
39 private UnidentifiedAccessHelper unidentifiedAccessHelper;
40
41 public Context(
42 final SignalAccount account,
43 final SignalDependencies dependencies,
44 final AvatarStore avatarStore,
45 final AttachmentStore attachmentStore,
46 final StickerPackStore stickerPackStore
47 ) {
48 this.account = account;
49 this.dependencies = dependencies;
50 this.avatarStore = avatarStore;
51 this.stickerPackStore = stickerPackStore;
52 this.attachmentStore = attachmentStore;
53 this.jobExecutor = new JobExecutor(this);
54 }
55
56 public SignalAccount getAccount() {
57 return account;
58 }
59
60 public SignalDependencies getDependencies() {
61 return dependencies;
62 }
63
64 public AvatarStore getAvatarStore() {
65 return avatarStore;
66 }
67
68 public StickerPackStore getStickerPackStore() {
69 return stickerPackStore;
70 }
71
72 AttachmentStore getAttachmentStore() {
73 return attachmentStore;
74 }
75
76 JobExecutor getJobExecutor() {
77 return jobExecutor;
78 }
79
80 public AccountHelper getAccountHelper() {
81 return getOrCreate(() -> accountHelper, () -> accountHelper = new AccountHelper(this));
82 }
83
84 public AttachmentHelper getAttachmentHelper() {
85 return getOrCreate(() -> attachmentHelper, () -> attachmentHelper = new AttachmentHelper(this));
86 }
87
88 public ContactHelper getContactHelper() {
89 return getOrCreate(() -> contactHelper, () -> contactHelper = new ContactHelper(account));
90 }
91
92 GroupV2Helper getGroupV2Helper() {
93 return getOrCreate(() -> groupV2Helper, () -> groupV2Helper = new GroupV2Helper(this));
94 }
95
96 public GroupHelper getGroupHelper() {
97 return getOrCreate(() -> groupHelper, () -> groupHelper = new GroupHelper(this));
98 }
99
100 public IdentityHelper getIdentityHelper() {
101 return getOrCreate(() -> identityHelper, () -> identityHelper = new IdentityHelper(this));
102 }
103
104 public IncomingMessageHandler getIncomingMessageHandler() {
105 return getOrCreate(() -> incomingMessageHandler,
106 () -> this.incomingMessageHandler = new IncomingMessageHandler(this));
107 }
108
109 PinHelper getPinHelper() {
110 return getOrCreate(() -> pinHelper, () -> pinHelper = new PinHelper(dependencies.getKeyBackupService()));
111 }
112
113 public PreKeyHelper getPreKeyHelper() {
114 return getOrCreate(() -> preKeyHelper, () -> preKeyHelper = new PreKeyHelper(account, dependencies));
115 }
116
117 public ProfileHelper getProfileHelper() {
118 return getOrCreate(() -> profileHelper, () -> profileHelper = new ProfileHelper(this));
119 }
120
121 public ReceiveHelper getReceiveHelper() {
122 return getOrCreate(() -> receiveHelper, () -> receiveHelper = new ReceiveHelper(this));
123 }
124
125 public RecipientHelper getRecipientHelper() {
126 return getOrCreate(() -> recipientHelper, () -> recipientHelper = new RecipientHelper(this));
127 }
128
129 public SendHelper getSendHelper() {
130 return getOrCreate(() -> sendHelper, () -> sendHelper = new SendHelper(this));
131 }
132
133 public StickerHelper getStickerHelper() {
134 return getOrCreate(() -> stickerHelper, () -> stickerHelper = new StickerHelper(this));
135 }
136
137 public StorageHelper getStorageHelper() {
138 return getOrCreate(() -> storageHelper, () -> storageHelper = new StorageHelper(this));
139 }
140
141 public SyncHelper getSyncHelper() {
142 return getOrCreate(() -> syncHelper, () -> syncHelper = new SyncHelper(this));
143 }
144
145 UnidentifiedAccessHelper getUnidentifiedAccessHelper() {
146 return getOrCreate(() -> unidentifiedAccessHelper,
147 () -> unidentifiedAccessHelper = new UnidentifiedAccessHelper(this));
148 }
149
150 private <T> T getOrCreate(Supplier<T> supplier, Callable creator) {
151 var value = supplier.get();
152 if (value != null) {
153 return value;
154 }
155
156 synchronized (LOCK) {
157 value = supplier.get();
158 if (value != null) {
159 return value;
160 }
161 creator.call();
162 return supplier.get();
163 }
164 }
165
166 private interface Callable {
167
168 void call();
169 }
170 }