Skip to content

Commit a081734

Browse files
committed
Clean up redundant attributes / modifiers
1 parent 9069886 commit a081734

File tree

3 files changed

+28
-46
lines changed

3 files changed

+28
-46
lines changed

src/main/java/com/amplitude/Amplitude.java

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6,27 +6,23 @@
66
import java.util.concurrent.ExecutorService;
77

88
public class Amplitude {
9-
private static Map<String, Amplitude> instances = new HashMap<>();
9+
private final static Map<String, Amplitude> instances = new HashMap<>();
1010
private final String instanceName;
1111
private String apiKey;
1212
private String serverUrl;
1313

14-
private AmplitudeLog logger;
14+
private AmplitudeLog logger = new AmplitudeLog();
1515

16-
private Queue<Event> eventsToSend;
17-
private boolean aboutToStartFlushing;
16+
private final Queue<Event> eventsToSend = new ConcurrentLinkedQueue<>();
17+
private boolean aboutToStartFlushing = false;
1818

1919
private HttpCallMode httpCallMode;
20-
private HttpCall httpCall;
21-
private HttpTransport httpTransport;
20+
private final HttpTransport httpTransport;
2221
private int eventUploadThreshold = Constants.EVENT_BUF_COUNT;
2322
private int eventUploadPeriodMillis = Constants.EVENT_BUF_TIME_MILLIS;
24-
private Object eventQueueLock = new Object();
23+
private final Object eventQueueLock = new Object();
2524
private Plan plan;
2625
private IngestionMetadata ingestionMetadata;
27-
private long flushTimeout;
28-
private ExecutorService retryThreadPool;
29-
private ExecutorService sendThreadPool;
3026

3127
/**
3228
* A dictionary of key-value pairs that represent additional instructions for server save
@@ -46,10 +42,7 @@ public class Amplitude {
4642
*/
4743
private Amplitude(String name) {
4844
instanceName = name;
49-
logger = new AmplitudeLog();
50-
eventsToSend = new ConcurrentLinkedQueue<>();
51-
aboutToStartFlushing = false;
52-
httpTransport = new HttpTransport(httpCall, null, logger, flushTimeout, sendThreadPool, retryThreadPool);
45+
httpTransport = new HttpTransport(logger);
5346
}
5447

5548
/**
@@ -204,7 +197,6 @@ public Amplitude setLogger(AmplitudeLog logger) {
204197
* @param timeout the flush events threads timeout millis
205198
*/
206199
public Amplitude setFlushTimeout(long timeout) {
207-
flushTimeout = timeout;
208200
this.httpTransport.setFlushTimeout(timeout);
209201
return this;
210202
}
@@ -215,7 +207,6 @@ public Amplitude setFlushTimeout(long timeout) {
215207
* @param sendThreadPool the thread pool for sending events
216208
*/
217209
public Amplitude setSendThreadPool(ExecutorService sendThreadPool) {
218-
this.sendThreadPool = sendThreadPool;
219210
this.httpTransport.setSendThreadPool(sendThreadPool);
220211
return this;
221212
}
@@ -226,7 +217,6 @@ public Amplitude setSendThreadPool(ExecutorService sendThreadPool) {
226217
* @param retryThreadPool the thread pool for retrying events
227218
*/
228219
public Amplitude setRetryThreadPool(ExecutorService retryThreadPool) {
229-
this.retryThreadPool = retryThreadPool;
230220
this.httpTransport.setRetryThreadPool(retryThreadPool);
231221
return this;
232222
}
@@ -356,14 +346,13 @@ private void updateHttpCall(HttpCallMode updatedHttpCallMode) {
356346
httpCallMode = updatedHttpCallMode;
357347

358348
if (updatedHttpCallMode == HttpCallMode.BATCH) {
359-
httpCall =
349+
httpTransport.setHttpCall(
360350
new HttpCall(
361-
apiKey, serverUrl != null ? serverUrl : Constants.BATCH_API_URL, options, proxy);
351+
apiKey, serverUrl != null ? serverUrl : Constants.BATCH_API_URL, options, proxy));
362352
} else {
363-
httpCall =
364-
new HttpCall(apiKey, serverUrl != null ? serverUrl : Constants.API_URL, options, proxy);
353+
httpTransport.setHttpCall(
354+
new HttpCall(apiKey, serverUrl != null ? serverUrl : Constants.API_URL, options, proxy));
365355
}
366-
httpTransport.setHttpCall(httpCall);
367356
}
368357

369358
private synchronized void scheduleFlushEvents() {

src/main/java/com/amplitude/HttpTransport.java

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,31 +30,25 @@ protected EventsRetryResult(
3030

3131
class HttpTransport {
3232
// Use map to record the events are currently in retry queue.
33-
private Object throttleLock = new Object();
34-
private Map<String, Integer> throttledUserId = new HashMap<>();
35-
private Map<String, Integer> throttledDeviceId = new HashMap<>();
33+
private final Object throttleLock = new Object();
34+
private final Map<String, Integer> throttledUserId = new HashMap<>();
35+
private final Map<String, Integer> throttledDeviceId = new HashMap<>();
3636
private boolean recordThrottledId = false;
37-
private Map<String, Map<String, List<Event>>> idToBuffer = new HashMap<>();
37+
private final Map<String, Map<String, List<Event>>> idToBuffer = new HashMap<>();
3838
private int eventsInRetry = 0;
39-
private Object bufferLock = new Object();
40-
private Object counterLock = new Object();
41-
private ExecutorService retryThreadPool;
42-
private ExecutorService sendThreadPool;
43-
44-
private HttpCall httpCall;
45-
private AmplitudeLog logger;
46-
private AmplitudeCallbacks callbacks;
47-
private long flushTimeout;
48-
49-
HttpTransport(
50-
HttpCall httpCall, AmplitudeCallbacks callbacks, AmplitudeLog logger,
51-
long flushTimeout, ExecutorService sendThreadPool, ExecutorService retryThreadPool) {
52-
this.httpCall = httpCall;
53-
this.callbacks = callbacks;
39+
private final Object bufferLock = new Object();
40+
private final Object counterLock = new Object();
41+
42+
// Managed by setters
43+
private ExecutorService retryThreadPool = Executors.newFixedThreadPool(10);
44+
private ExecutorService sendThreadPool = Executors.newFixedThreadPool(40);
45+
private HttpCall httpCall = null;
46+
private long flushTimeout = 0;
47+
private AmplitudeCallbacks callbacks = null;
48+
private final AmplitudeLog logger;
49+
50+
HttpTransport(AmplitudeLog logger) {
5451
this.logger = logger;
55-
this.flushTimeout = flushTimeout;
56-
this.retryThreadPool = (retryThreadPool == null) ? Executors.newFixedThreadPool(10) : retryThreadPool;
57-
this.sendThreadPool = (sendThreadPool == null) ? Executors.newFixedThreadPool(40) : sendThreadPool;
5852
}
5953

6054
public void sendEventsWithRetry(List<Event> events) {

src/test/java/com/amplitude/HttpTransportTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.junit.jupiter.api.extension.ExtendWith;
2626
import org.junit.jupiter.params.ParameterizedTest;
2727
import org.junit.jupiter.params.provider.CsvSource;
28-
import org.mockito.BDDMockito;
2928
import org.mockito.junit.jupiter.MockitoExtension;
3029

3130
import com.amplitude.exception.AmplitudeInvalidAPIKeyException;
@@ -40,7 +39,7 @@ public class HttpTransportTest {
4039

4140
@BeforeEach
4241
public void setUp() {
43-
httpTransport = new HttpTransport(null, null, new AmplitudeLog(), 0, null, null);
42+
httpTransport = new HttpTransport(new AmplitudeLog());
4443
}
4544

4645
/**

0 commit comments

Comments
 (0)