Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lots of Sonar cleanup #1016

Merged
merged 1 commit into from
Aug 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static com.google.common.base.Preconditions.checkArgument;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;
import static org.kiwiproject.collect.KiwiLists.subListFrom;

Expand Down Expand Up @@ -62,7 +61,7 @@ class VaultEncryptedVariable {
}

private void parse(String encryptStringOutput) {
var lines = encryptStringOutput.lines().collect(toUnmodifiableList());
var lines = encryptStringOutput.lines().toList();
checkArgument(lines.size() > 2, INVALID_ENCRYPT_STRING_INPUT);

parseLine1(lines);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package org.kiwiproject.base.process;

import static java.util.stream.Collectors.toList;
import static org.kiwiproject.collect.KiwiLists.first;
import static org.kiwiproject.io.KiwiIO.readLinesFromInputStreamOf;
import static org.kiwiproject.io.KiwiIO.streamLinesFromInputStreamOf;
Expand Down Expand Up @@ -327,7 +326,7 @@ Collection<Long> findChildProcessIdsInternal(long parentProcessId, ProcessHelper
var process = launchPgrepWithParentPidFlag(parentProcessId, processHelper);
Stream<String> stream = streamLinesFromInputStreamOf(process);

return stream.map(Processes::getPidOrThrow).collect(toList());
return stream.map(Processes::getPidOrThrow).toList();
}

/**
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/kiwiproject/base/process/Processes.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static com.google.common.base.Preconditions.checkState;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.base.KiwiStrings.SPACE;
import static org.kiwiproject.base.KiwiStrings.format;
Expand Down Expand Up @@ -394,7 +393,7 @@ public static List<Long> pgrep(String user, String commandLine) {

return streamLinesFromInputStreamOf(process)
.map(Processes::getPidOrThrow)
.collect(toList());
.toList();
} catch (IOException e) {
throw new UncheckedIOException(
format("Error executing pgrep with user [%s] and command [%s]", user, commandLine), e);
Expand Down Expand Up @@ -495,7 +494,7 @@ public static List<Pair<Long, String>> pgrepParsedList(String commandLine) {
public static List<Pair<Long, String>> pgrepParsedList(String user, String commandLine) {
List<String> lines = pgrepList(user, commandLine);

return lines.stream().map(Processes::pairFromPgrepLine).collect(toList());
return lines.stream().map(Processes::pairFromPgrepLine).toList();
}

private static Pair<Long, String> pairFromPgrepLine(String line) {
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/kiwiproject/beans/BeanConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ protected Set<String> getPropertySet(T input, BeanWrapper inputWrapper) {

@SuppressWarnings({"unchecked", "rawtypes"})
private static <T> Set<String> getPropertyNamesAsSet(T input, BeanWrapper inputWrapper) {
if (input instanceof Map) {
return ((Map) input).keySet();
if (input instanceof Map inputMap) {
return inputMap.keySet();
}

return Stream.of(inputWrapper.getPropertyDescriptors())
Expand Down Expand Up @@ -178,8 +178,8 @@ protected <R> void writeBeanValue(R target, BeanWrapper targetWrapper, String pr
targetWrapper.setPropertyValue(propName, inputValue);
} catch (NotWritablePropertyException e) {
// try direct access if map
if (target instanceof Map) {
((Map) target).put(propName, inputValue);
if (target instanceof Map targetMap) {
targetMap.put(propName, inputValue);
} else {
// if no approach works
logOrFail("Exception trying to write value", propName, e);
Expand Down
9 changes: 4 additions & 5 deletions src/main/java/org/kiwiproject/collect/KiwiLists.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.common.base.Preconditions.checkNotNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toList;

import lombok.experimental.UtilityClass;

Expand Down Expand Up @@ -63,7 +62,7 @@ public static <T> boolean hasOneElement(List<T> items) {
*/
public static <T> List<T> sorted(List<T> items) {
checkNonNullInputList(items);
return items.stream().sorted().collect(toList());
return items.stream().sorted().toList();
}

/**
Expand All @@ -77,7 +76,7 @@ public static <T> List<T> sorted(List<T> items) {
public static <T> List<T> sorted(List<T> items, Comparator<T> comparator) {
checkNonNullInputList(items);
checkNotNull(comparator, "Comparator cannot be null");
return items.stream().sorted(comparator).collect(toList());
return items.stream().sorted(comparator).toList();
}

/**
Expand Down Expand Up @@ -264,7 +263,7 @@ public static <T> List<T> distinctOrEmpty(Collection<T> collection) {
}

private static <T> List<T> distinctListFrom(Collection<T> collection) {
return collection.stream().distinct().collect(toList());
return collection.stream().distinct().toList();
}

/**
Expand All @@ -278,7 +277,7 @@ private static <T> List<T> distinctListFrom(Collection<T> collection) {
*/
public static <T> List<T> newListStartingAtCircularOffset(List<T> input, long startOffset) {
var size = input.size();
return IntStream.range(0, size).mapToObj(i -> input.get((int) (startOffset + i) % size)).collect(toList());
return IntStream.range(0, size).mapToObj(i -> input.get((int) (startOffset + i) % size)).toList();
}

/**
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/kiwiproject/collect/KiwiProperties.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Lists.partition;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.kiwiproject.base.KiwiPreconditions.checkEvenItemCount;
import static org.kiwiproject.collect.KiwiLists.first;
import static org.kiwiproject.collect.KiwiLists.second;
Expand Down Expand Up @@ -36,7 +35,7 @@ public class KiwiProperties {
*/
public static Properties newProperties(Object... items) {
checkEvenItemCount(items);
var strings = newArrayList(items).stream().map(Object::toString).collect(toList());
var strings = newArrayList(items).stream().map(Object::toString).toList();
var kvPairs = partition(strings, 2);
return newPropertiesFromStringPairs(kvPairs);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/kiwiproject/io/KiwiIO.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;

import lombok.experimental.UtilityClass;
Expand Down Expand Up @@ -300,7 +299,7 @@ public static List<String> readLinesFromInputStreamOf(Process process, Charset c
* @return a list of strings from the input stream, encoded using the specified charset
*/
public static List<String> readLinesFrom(InputStream stream, Charset charset) {
return streamLinesFrom(stream, charset).collect(toList());
return streamLinesFrom(stream, charset).toList();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.Objects.requireNonNull;
import static java.util.stream.Collectors.toList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.base.KiwiStrings.f;

Expand Down Expand Up @@ -254,15 +253,15 @@ void cleanDirectory() {
var attemptedDeletes = Arrays.stream(filesToClean)
.map(TimeBasedDirectoryCleaner::tryDeleteIfExists)
.filter(result -> result.deleteWasAttempted)
.collect(toList());
.toList();

var numExpectedDeletes = attemptedDeletes.size();

var failedDeletes = attemptedDeletes
.stream()
.filter(FileDeleteResult::deleteAttemptedAndFailed)
.peek(this::logUnableToDelete)
.collect(toList());
.toList();

updateFileDeletionMetadata(numExpectedDeletes, failedDeletes);
}
Expand Down
3 changes: 1 addition & 2 deletions src/main/java/org/kiwiproject/jar/KiwiJars.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import static com.google.common.collect.Lists.newArrayList;
import static java.util.Collections.emptyList;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.collect.KiwiLists.isNotNullOrEmpty;
import static org.kiwiproject.collect.KiwiLists.isNullOrEmpty;
Expand Down Expand Up @@ -146,7 +145,7 @@ public static Optional<String> readSingleValueFromJarManifest(ClassLoader classL
urls = StreamSupport
.stream((urlIterable).spliterator(), false)
.filter(manifestFilter)
.collect(toUnmodifiableList());
.toList();
}

LOG.trace("Using manifest URL(s): {}", urls);
Expand Down
7 changes: 2 additions & 5 deletions src/main/java/org/kiwiproject/jaxrs/KiwiResources.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotBlank;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;
import static org.kiwiproject.base.KiwiStrings.f;
Expand Down Expand Up @@ -469,12 +468,11 @@ public static <V> List<Integer> validateOneOrMoreIntParameters(MultivaluedMap<St

return values.stream()
.map(value -> parseIntOrThrowBadRequest(value, parameterName))
.collect(toUnmodifiableList());
.toList();
}

@VisibleForTesting
static int parseIntOrThrowBadRequest(Object value, String parameterName) {
//noinspection UnstableApiUsage
var result = Optional.ofNullable(value).map(Object::toString).map(Ints::tryParse);

return result.orElseThrow(() -> newJaxrsBadRequestException("'{}' is not an integer", value, parameterName));
Expand Down Expand Up @@ -569,12 +567,11 @@ public static <V> List<Long> validateOneOrMoreLongParameters(MultivaluedMap<Stri

return values.stream()
.map(value -> parseLongOrThrowBadRequest(value, parameterName))
.collect(toUnmodifiableList());
.toList();
}

@VisibleForTesting
static long parseLongOrThrowBadRequest(Object value, String parameterName) {
//noinspection UnstableApiUsage
var result = Optional.ofNullable(value).map(Object::toString).map(Longs::tryParse);

return result.orElseThrow(() -> newJaxrsBadRequestException("'{}' is not a long", value, parameterName));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ private static String rollupMessageOrDefault(List<JaxrsException> exceptions) {
* @return new JaxrsException instance
*/
public static JaxrsException buildJaxrsException(Throwable throwable) {
return throwable instanceof JaxrsException ? (JaxrsException) throwable : new JaxrsException(throwable);
return throwable instanceof JaxrsException jaxrsException ? jaxrsException : new JaxrsException(throwable);
}

/**
Expand Down Expand Up @@ -311,12 +311,12 @@ public static int getErrorCode(Throwable throwable) {
return 500;
}

if (throwable instanceof JaxrsException) {
return ((JaxrsException) throwable).getStatusCode();
if (throwable instanceof JaxrsException jaxrsException) {
return jaxrsException.getStatusCode();
}

if (throwable instanceof WebApplicationException) {
return ((WebApplicationException) throwable).getResponse().getStatus();
if (throwable instanceof WebApplicationException webApplicationException) {
return webApplicationException.getResponse().getStatus();
}

if (throwable instanceof IllegalArgumentException) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import static java.util.Collections.unmodifiableMap;
import static java.util.Objects.isNull;
import static java.util.stream.Collectors.toUnmodifiableList;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.kiwiproject.collect.KiwiMaps.isNotNullOrEmpty;

Expand Down Expand Up @@ -188,16 +187,16 @@ private static List<ErrorMessage> extractErrorMessages(Map<String, Object> entit
return errorObjects.stream()
.map(JaxrsExceptionMapper::toErrorMessageOrNull)
.filter(Objects::nonNull)
.collect(toUnmodifiableList());
.toList();
}

return List.of();
}

@SuppressWarnings("unchecked")
private static ErrorMessage toErrorMessageOrNull(Object obj) {
if (obj instanceof ErrorMessage) {
return (ErrorMessage) obj;
if (obj instanceof ErrorMessage errorMessage) {
return errorMessage;
} else if (obj instanceof Map) {
return ErrorMessage.valueOf((Map<String, Object>) obj);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.kiwiproject.jaxrs.exception;

import static java.util.Objects.nonNull;
import static java.util.stream.Collectors.toList;
import static org.kiwiproject.base.KiwiPreconditions.checkArgumentNotNull;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -45,7 +44,7 @@ public JaxrsValidationException(String itemId, List<Map<String, String>> errorMe
var fieldName = entry.get(ErrorMessage.KEY_FIELD_NAME);
var message = entry.get(ErrorMessage.KEY_MESSAGE);
return new ErrorMessage(itemId, CODE, message, fieldName);
}).collect(toList());
}).toList();
setErrors(errorMessages);
}

Expand All @@ -61,7 +60,7 @@ public JaxrsValidationException(String itemId, Set<? extends ConstraintViolation
checkArgumentNotNull(violations);
var errorMessages = violations.stream()
.map(violation -> buildErrorMessage(itemId, violation))
.collect(toList());
.toList();
setErrors(errorMessages);
}

Expand Down Expand Up @@ -90,7 +89,7 @@ public JaxrsValidationException(String itemId,
var propertyPath = violation.getPropertyPath().toString();
var fieldNameOrNull = propertyPathMappings.get(propertyPath);
return buildErrorMessage(itemId, violation, fieldNameOrNull);
}).collect(toList());
}).toList();
setErrors(errorMessages);
}

Expand Down
Loading