Skip to content

Commit

Permalink
Replace com.google.common.base.Function (partly) (#2121)
Browse files Browse the repository at this point in the history
Mostly replace use of Guava functional expressions with Java streams and functions.

---------

Co-authored-by: Kirill Peshin <kirill.peshin@bsc-ideas.com>
  • Loading branch information
pks-1981 and Kirill Peshin committed May 31, 2023
1 parent 17a9881 commit bc09611
Show file tree
Hide file tree
Showing 38 changed files with 280 additions and 588 deletions.
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016-2022 Thomas Akehurst
* Copyright (C) 2016-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -18,18 +18,15 @@
import static com.github.tomakehurst.wiremock.admin.RequestSpec.requestSpec;
import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.github.tomakehurst.wiremock.http.RequestMethod.*;
import static com.google.common.collect.Iterables.tryFind;

import com.github.tomakehurst.wiremock.admin.tasks.*;
import com.github.tomakehurst.wiremock.extension.AdminApiExtension;
import com.github.tomakehurst.wiremock.http.RequestMethod;
import com.github.tomakehurst.wiremock.store.Stores;
import com.github.tomakehurst.wiremock.verification.notmatched.PlainTextStubNotMatchedRenderer;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableBiMap;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;

public class AdminRoutes {

Expand Down Expand Up @@ -128,48 +125,21 @@ protected void initAdditionalRoutes(Router routeBuilder) {
}

public AdminTask taskFor(final RequestMethod method, final String path) {
return tryFind(
routes.entrySet(),
new Predicate<Map.Entry<RequestSpec, AdminTask>>() {
@Override
public boolean apply(Map.Entry<RequestSpec, AdminTask> entry) {
return entry.getKey().matches(method, path);
}
})
.transform(
new Function<Map.Entry<RequestSpec, AdminTask>, AdminTask>() {
@Override
public AdminTask apply(Map.Entry<RequestSpec, AdminTask> input) {
return input.getValue();
}
})
.or(new NotFoundAdminTask());
return routes.entrySet().stream()
.filter(entry -> entry.getKey().matches(method, path))
.map(Entry::getValue)
.findFirst()
.orElse(new NotFoundAdminTask());
}

public RequestSpec requestSpecForTask(final Class<? extends AdminTask> taskClass) {
RequestSpec requestSpec =
tryFind(
routes.entrySet(),
new Predicate<Map.Entry<RequestSpec, AdminTask>>() {
@Override
public boolean apply(Map.Entry<RequestSpec, AdminTask> input) {
return input.getValue().getClass().equals(taskClass);
}
})
.transform(
new Function<Map.Entry<RequestSpec, AdminTask>, RequestSpec>() {
@Override
public RequestSpec apply(Map.Entry<RequestSpec, AdminTask> input) {
return input.getKey();
}
})
.orNull();

if (requestSpec == null) {
throw new NotFoundException("No route could be found for " + taskClass.getSimpleName());
}

return requestSpec;
return routes.entrySet().stream()
.filter(input -> input.getValue().getClass().equals(taskClass))
.map(Entry::getKey)
.findFirst()
.orElseThrow(
() ->
new NotFoundException("No route could be found for " + taskClass.getSimpleName()));
}

protected static class RouteBuilder implements Router {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2011-2021 Thomas Akehurst
* Copyright (C) 2011-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,16 +15,14 @@
*/
package com.github.tomakehurst.wiremock.client;

import static com.google.common.base.Functions.toStringFunction;
import static com.google.common.collect.FluentIterable.from;

import com.github.tomakehurst.wiremock.common.Json;
import com.github.tomakehurst.wiremock.matching.RequestPattern;
import com.github.tomakehurst.wiremock.verification.LoggedRequest;
import com.github.tomakehurst.wiremock.verification.NearMiss;
import com.github.tomakehurst.wiremock.verification.diff.Diff;
import com.google.common.base.Joiner;
import java.util.List;
import java.util.stream.Collectors;

public class VerificationException extends AssertionError {

Expand Down Expand Up @@ -56,7 +54,7 @@ public static VerificationException forUnmatchedNearMisses(List<NearMiss> nearMi
}

private static String renderList(List<?> list) {
return Joiner.on("\n\n").join(from(list).transform(toStringFunction()));
return Joiner.on("\n\n").join(list.stream().map(Object::toString).collect(Collectors.toList()));
}

private VerificationException(String messageStart, Diff diff) {
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2012-2022 Thomas Akehurst
* Copyright (C) 2012-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -16,19 +16,18 @@
package com.github.tomakehurst.wiremock.common;

import static com.google.common.base.Charsets.UTF_8;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;

import com.github.tomakehurst.wiremock.security.NotAuthorisedException;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.io.Files;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
import java.util.stream.Collectors;

public abstract class AbstractFileSource implements FileSource {

Expand Down Expand Up @@ -75,7 +74,7 @@ public URI getUri() {
@Override
public List<TextFile> listFilesRecursively() {
assertExistsAndIsDirectory();
List<File> fileList = newArrayList();
List<File> fileList = new ArrayList<>();
recursivelyAddFilesToList(rootDirectory, fileList);
return toTextFileList(fileList);
}
Expand All @@ -92,14 +91,7 @@ private void recursivelyAddFilesToList(File root, List<File> fileList) {
}

private List<TextFile> toTextFileList(List<File> fileList) {
return newArrayList(
transform(
fileList,
new Function<File, TextFile>() {
public TextFile apply(File input) {
return new TextFile(input.toURI());
}
}));
return fileList.stream().map(input -> new TextFile(input.toURI())).collect(Collectors.toList());
}

@Override
Expand Down Expand Up @@ -195,10 +187,6 @@ private void writeBinaryFileAndTranslateExceptions(byte[] contents, File toFile)
}

public static Predicate<BinaryFile> byFileExtension(final String extension) {
return new Predicate<BinaryFile>() {
public boolean apply(BinaryFile input) {
return input.name().endsWith("." + extension);
}
};
return input -> input.name().endsWith("." + extension);
}
}
Expand Up @@ -17,22 +17,20 @@

import static com.github.tomakehurst.wiremock.common.Exceptions.throwUnchecked;
import static com.google.common.base.MoreObjects.firstNonNull;
import static com.google.common.collect.Iterables.transform;
import static com.google.common.collect.Lists.newArrayList;
import static java.lang.Thread.currentThread;
import static java.util.Arrays.asList;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.FluentIterable;
import com.google.common.collect.Iterators;
import com.google.common.io.Resources;
import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

Expand Down Expand Up @@ -148,25 +146,15 @@ public URI getUri() {
public List<TextFile> listFilesRecursively() {
if (isFileSystem()) {
assertExistsAndIsDirectory();
List<File> fileList = newArrayList();
List<File> fileList = new ArrayList<>();
recursivelyAddFilesToList(rootDirectory, fileList);
return toTextFileList(fileList);
}

return FluentIterable.from(toIterable(zipFile.entries()))
.filter(
new Predicate<ZipEntry>() {
public boolean apply(ZipEntry jarEntry) {
return !jarEntry.isDirectory() && jarEntry.getName().startsWith(path);
}
})
.transform(
new Function<ZipEntry, TextFile>() {
public TextFile apply(ZipEntry jarEntry) {
return new TextFile(getUriFor(jarEntry));
}
})
.toList();
return StreamSupport.stream(toIterable(zipFile.entries()).spliterator(), false)
.filter(jarEntry -> !jarEntry.isDirectory() && jarEntry.getName().startsWith(path))
.map(jarEntry -> new TextFile(getUriFor(jarEntry)))
.collect(Collectors.toList());
}

private URI getUriFor(ZipEntry jarEntry) {
Expand All @@ -189,14 +177,7 @@ private void recursivelyAddFilesToList(File root, List<File> fileList) {
}

private List<TextFile> toTextFileList(List<File> fileList) {
return newArrayList(
transform(
fileList,
new Function<File, TextFile>() {
public TextFile apply(File input) {
return new TextFile(input.toURI());
}
}));
return fileList.stream().map(input -> new TextFile(input.toURI())).collect(Collectors.toList());
}

@Override
Expand Down
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2017-2021 Thomas Akehurst
* Copyright (C) 2017-2023 Thomas Akehurst
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -15,14 +15,13 @@
*/
package com.github.tomakehurst.wiremock.common;

import static com.google.common.collect.Lists.transform;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import java.util.List;
import java.util.function.Function;
import java.util.regex.PatternSyntaxException;
import java.util.stream.Collectors;

public class JsonException extends InvalidInputException {

Expand All @@ -46,7 +45,8 @@ public static JsonException fromJackson(JsonProcessingException processingExcept
String pointer = null;
if (processingException instanceof JsonMappingException) {
List<String> nodes =
transform(((JsonMappingException) processingException).getPath(), TO_NODE_NAMES);
((JsonMappingException) processingException)
.getPath().stream().map(TO_NODE_NAMES).collect(Collectors.toList());
pointer = '/' + Joiner.on('/').join(nodes);
}

Expand All @@ -62,14 +62,6 @@ private static Throwable getRootCause(Throwable e) {
}

private static final Function<JsonMappingException.Reference, String> TO_NODE_NAMES =
new Function<JsonMappingException.Reference, String>() {
@Override
public String apply(JsonMappingException.Reference input) {
if (input.getFieldName() != null) {
return input.getFieldName();
}

return String.valueOf(input.getIndex());
}
};
input ->
input.getFieldName() != null ? input.getFieldName() : String.valueOf(input.getIndex());
}
Expand Up @@ -17,11 +17,11 @@

import static java.lang.String.format;

import com.google.common.base.Function;
import com.google.common.base.Objects;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.function.Function;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

Expand Down

0 comments on commit bc09611

Please sign in to comment.