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