chore: Improve code (#442)

* chore: Improve code

* Convert classes to records
This commit is contained in:
Rene Leonhardt 2024-04-10 13:47:38 +02:00 committed by GitHub
parent c29d3928db
commit 7d89650062
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
86 changed files with 528 additions and 976 deletions

View file

@ -10,7 +10,6 @@
******************************************************************************/
package ee.carlrobert.codegpt.telemetry.core.configuration;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.file.Path;
@ -28,7 +27,7 @@ public class ClasspathConfiguration extends FileConfiguration {
}
@Override
protected InputStream createInputStream(Path path) throws FileNotFoundException {
protected InputStream createInputStream(Path path) {
if (path == null) {
return null;
}

View file

@ -12,17 +12,15 @@ package ee.carlrobert.codegpt.telemetry.core.configuration;
import java.util.List;
import java.util.Objects;
import java.util.stream.Stream;
public abstract class CompositeConfiguration implements IConfiguration {
@Override
public String get(final String key) {
List<IConfiguration> configurations = getConfigurations();
if (configurations == null
|| configurations.isEmpty()) {
return null;
}
return configurations.stream()
return (configurations == null ? Stream.<IConfiguration>empty() : configurations.stream())
.filter(Objects::nonNull)
.map(configuration -> configuration.get(key))
.filter(Objects::nonNull)
.findFirst()

View file

@ -10,7 +10,6 @@
******************************************************************************/
package ee.carlrobert.codegpt.telemetry.core.configuration;
import com.intellij.openapi.diagnostic.Logger;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
@ -20,8 +19,6 @@ import java.time.LocalDate;
public class SaveableFileConfiguration extends FileConfiguration {
private static final Logger LOGGER = Logger.getInstance(SaveableFileConfiguration.class);
public SaveableFileConfiguration(Path file) {
super(file);
}

View file

@ -24,7 +24,7 @@ public class TelemetryConfiguration extends CompositeConfiguration {
private static final SaveableFileConfiguration FILE = new SaveableFileConfiguration(
Directories.PATH.resolve("ee.carlrobert.intellij.telemetry"));
private static TelemetryConfiguration INSTANCE = new TelemetryConfiguration();
private static final TelemetryConfiguration INSTANCE = new TelemetryConfiguration();
public static TelemetryConfiguration getInstance() {
return INSTANCE;

View file

@ -15,7 +15,6 @@ import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
public class Application {
@ -43,15 +42,14 @@ public class Application {
public Collection<AbstractMap.SimpleEntry<String, Object>> getProperties() {
return properties.entrySet().stream()
.map(entry -> new AbstractMap.SimpleEntry<String, Object>(entry.getKey(), entry.getValue()))
.collect(Collectors.toList());
.map(entry -> new AbstractMap.SimpleEntry<String, Object>(entry))
.toList();
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Application)) return false;
Application that = (Application) o;
if (!(o instanceof Application that)) return false;
return Objects.equals(name, that.name)
&& Objects.equals(version, that.version)
&& Objects.equals(properties, that.properties);

View file

@ -72,7 +72,7 @@ public class Country {
try {
ObjectMapper mapper = new ObjectMapper();
InputStream input = getClass().getResourceAsStream(file);
TypeReference<Map<String, V>> typeRef = new TypeReference<Map<String, V>>() {};
TypeReference<Map<String, V>> typeRef = new TypeReference<>() {};
return mapper.readValue(input, typeRef);
} catch (IOException e) {
LOGGER.warn("Could not load file " + file, e);

View file

@ -117,7 +117,7 @@ public class Environment {
}
}
class Buildable {
public class Buildable {
public Environment build() {
ensureIDE();
ensurePlatform();
@ -171,9 +171,8 @@ public class Environment {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Environment)) return false;
Environment that = (Environment) o;
return Objects.equals(plugin, that.plugin)
if (!(o instanceof Environment that)) return false;
return Objects.equals(plugin, that.plugin)
&& Objects.equals(ide, that.ide)
&& Objects.equals(platform, that.platform)
&& Objects.equals(timezone, that.timezone)
@ -187,4 +186,3 @@ public class Environment {
}
}

View file

@ -49,9 +49,8 @@ public class Platform {
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Platform)) return false;
Platform platform = (Platform) o;
return Objects.equals(name, platform.name)
if (!(o instanceof Platform platform)) return false;
return Objects.equals(name, platform.name)
&& Objects.equals(distribution, platform.distribution)
&& Objects.equals(version, platform.version);
}

View file

@ -13,7 +13,6 @@ package ee.carlrobert.codegpt.telemetry.core.service;
import static ee.carlrobert.codegpt.telemetry.core.configuration.TelemetryConfiguration.KEY_MODE;
import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.util.messages.MessageBusConnection;
import ee.carlrobert.codegpt.telemetry.core.IMessageBroker;
import ee.carlrobert.codegpt.telemetry.core.ITelemetryService;
@ -27,8 +26,6 @@ import java.util.concurrent.atomic.AtomicBoolean;
public class TelemetryService implements ITelemetryService {
private static final Logger LOGGER = Logger.getInstance(TelemetryService.class);
private static final int BUFFER_SIZE = 35;
private final TelemetryNotifications notifications;

View file

@ -39,7 +39,7 @@ public class UserId {
}
private String loadOrCreate(Path file) {
String uuid = null;
String uuid;
if (exists(file)) {
uuid = load(file);
if (isValid(uuid)) {
@ -61,6 +61,7 @@ public class UserId {
String uuid = null;
try(Stream<String> lines = Files.lines(uuidFile)) {
uuid = lines
.filter(l -> !l.isBlank())
.findAny()
.map(String::trim)
.orElse(null);

View file

@ -10,60 +10,8 @@
******************************************************************************/
package ee.carlrobert.codegpt.telemetry.core.service.segment;
import java.util.Objects;
/**
* Traits that the segment broker sends with am identify event.
*/
public class IdentifyTraits {
private final String locale;
private final String timezone;
private final String osName;
private final String osVersion;
private final String osDistribution;
public IdentifyTraits(String locale, String timezone, String osName, String osVersion, String osDistribution) {
this.locale = locale;
this.timezone = timezone;
this.osName = osName;
this.osVersion = osVersion;
this.osDistribution = osDistribution;
}
public String getLocale() {
return locale;
}
public String getTimezone() {
return timezone;
}
public String getOsName() {
return osName;
}
public String getOsVersion() {
return osVersion;
}
public String getOsDistribution() {
return osDistribution;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof IdentifyTraits)) return false;
IdentifyTraits identifyTraits = (IdentifyTraits) o;
return Objects.equals(locale, identifyTraits.locale)
&& Objects.equals(timezone, identifyTraits.timezone)
&& Objects.equals(osName, identifyTraits.osName)
&& Objects.equals(osVersion, identifyTraits.osVersion)
&& Objects.equals(osDistribution, identifyTraits.osDistribution);
}
@Override
public int hashCode() {
return Objects.hash(locale, timezone, osName, osVersion, osDistribution);
}
}
public record IdentifyTraits(String locale, String timezone, String osName, String osVersion, String osDistribution) {
}

View file

@ -71,6 +71,7 @@ public class IdentifyTraitsPersistence {
String event = null;
try(Stream<String> lines = getLines(file)) {
event = lines
.filter(l -> !l.isBlank())
.findAny()
.map(String::trim)
.orElse(null);

View file

@ -71,22 +71,17 @@ public class SegmentBroker implements IMessageBroker {
public abstract MessageBuilder toMessage(TelemetryEvent event, Map<String, Object> context, SegmentBroker broker);
public static SegmentType valueOf(TelemetryEvent.Type eventType) {
switch (eventType) {
case USER:
return IDENTIFY;
case ACTION:
case STARTUP:
case SHUTDOWN:
default:
return TRACK;
}
return switch (eventType) {
case USER -> IDENTIFY;
default -> TRACK;
};
}
}
private final String userId;
private final IdentifyTraitsPersistence identifyTraitsPersistence;
private final Environment environment;
private Lazy<RudderAnalytics> analytics;
private final Lazy<RudderAnalytics> analytics;
public SegmentBroker(boolean isDebug, String userId, Environment environment, ISegmentConfiguration configuration) {
this(isDebug, userId, IdentifyTraitsPersistence.INSTANCE, environment, configuration, new AnalyticsFactory());
@ -155,11 +150,11 @@ public class SegmentBroker implements IMessageBroker {
}
private Map<String, ?> addIdentifyTraits(final IdentifyTraits identifyTraits, final Map<String, String> properties) {
putIfNotNull(PROP_LOCALE, identifyTraits.getLocale(), properties);
putIfNotNull(PROP_TIMEZONE, identifyTraits.getTimezone(), properties);
putIfNotNull(PROP_OS_NAME, identifyTraits.getOsName(), properties);
putIfNotNull(PROP_OS_DISTRIBUTION, identifyTraits.getOsDistribution(), properties);
putIfNotNull(PROP_OS_VERSION, identifyTraits.getOsVersion(), properties);
putIfNotNull(PROP_LOCALE, identifyTraits.locale(), properties);
putIfNotNull(PROP_TIMEZONE, identifyTraits.timezone(), properties);
putIfNotNull(PROP_OS_NAME, identifyTraits.osName(), properties);
putIfNotNull(PROP_OS_DISTRIBUTION, identifyTraits.osDistribution(), properties);
putIfNotNull(PROP_OS_VERSION, identifyTraits.osVersion(), properties);
return properties;
}

View file

@ -17,6 +17,9 @@ import java.nio.file.Path;
public class FileUtils {
private FileUtils() {
}
/**
* Creates the file for the given path and the folder that contains it.
* Does nothing if it any of those already exist.

View file

@ -30,10 +30,9 @@ public class MapBuilder {
}
public MapBuilder pairs(Collection<AbstractMap.SimpleEntry<String, Object>> entries) {
if (entries == null) {
return this;
if (entries != null) {
entries.forEach(entry -> map.put(entry.getKey(), entry.getValue()));
}
entries.stream().forEach(entry -> map.put(entry.getKey(), entry.getValue()));
return this;
}
@ -59,10 +58,9 @@ public class MapBuilder {
}
public MapValueBuilder pairs(Collection<AbstractMap.SimpleEntry<String, Object>> entries) {
if (entries == null) {
return this;
if (entries != null) {
entries.forEach(entry -> map.put(entry.getKey(), entry.getValue()));
}
entries.stream().forEach(entry -> map.put(entry.getKey(), entry.getValue()));
return this;
}