-
Notifications
You must be signed in to change notification settings - Fork 214
/
DefaultPubSubConfig.java
109 lines (91 loc) · 3.2 KB
/
DefaultPubSubConfig.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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
* Copyright (c) 2019 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.internal.utils.pubsub.config;
import java.time.Duration;
import java.util.Arrays;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;
import org.eclipse.ditto.internal.utils.config.ConfigWithFallback;
import com.typesafe.config.Config;
/**
* This class is the default implementation of {@link PubSubConfig}.
*/
@Immutable
final class DefaultPubSubConfig implements PubSubConfig {
/**
* Path of the Suite Auth configuration settings.
*/
private static final String CONFIG_PATH = "pubsub";
private final String seed;
private final Duration restartDelay;
private final Duration updateInterval;
private final Duration syncInterval;
private DefaultPubSubConfig(final ConfigWithFallback config) {
seed = config.getString(ConfigValue.SEED.getConfigPath());
restartDelay = config.getDuration(ConfigValue.RESTART_DELAY.getConfigPath());
updateInterval = config.getDuration(ConfigValue.UPDATE_INTERVAL.getConfigPath());
syncInterval = config.getDuration(ConfigValue.SYNC_INTERVAL.getConfigPath());
}
static PubSubConfig of(final Config config) {
return new DefaultPubSubConfig(ConfigWithFallback.newInstance(config, CONFIG_PATH, ConfigValue.values()));
}
@Override
public String getSeed() {
return seed;
}
@Override
public Duration getRestartDelay() {
return restartDelay;
}
@Override
public Duration getUpdateInterval() {
return updateInterval;
}
@Override
public Duration getSyncInterval() {
return syncInterval;
}
private String[] getFieldNames() {
return new String[]{"seed", "restartDelay", "updateInterval", "syncInterval"};
}
private Object[] getFieldValues() {
return new Object[]{seed, restartDelay, updateInterval, syncInterval};
}
@Override
public boolean equals(@Nullable final Object other) {
if (other instanceof DefaultPubSubConfig) {
final DefaultPubSubConfig that = (DefaultPubSubConfig) other;
return Arrays.asList(getFieldValues()).equals(Arrays.asList(that.getFieldValues()));
} else {
return false;
}
}
@Override
public int hashCode() {
return Objects.hash(getFieldValues());
}
@Override
public String toString() {
final String[] fields = getFieldNames();
final Object[] values = getFieldValues();
return getClass().getSimpleName() +
"[" +
IntStream.range(0, fields.length)
.mapToObj(i -> fields[i] + "=" + values[i])
.collect(Collectors.joining(",")) +
"]";
}
}