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

Gh-308: Remove Guava dependency #309

Closed
wants to merge 4 commits into from
Closed
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
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,13 +16,13 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code FirstItem} is a {@link KorypheFunction} that returns the first item from a provided
Expand All @@ -40,7 +40,7 @@ public T apply(final Iterable<T> input) {
throw new IllegalArgumentException("Input cannot be null");
}
try {
return Iterables.getFirst(input, null);
return IterableUtil.getFirst(input);
} finally {
CloseableUtil.close(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2020-2022 Crown Copyright
* Copyright 2020-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,14 +17,14 @@
package uk.gov.gchq.koryphe.impl.function;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.collect.Iterables;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

import java.util.function.Predicate;

Expand Down Expand Up @@ -53,7 +53,7 @@ public FirstValid(final Predicate predicate) {
public I_ITEM apply(final Iterable<I_ITEM> iterable) {
if (nonNull(iterable) && nonNull(predicate)) {
try {
return Iterables.getFirst(filter(iterable, predicate), null);
return IterableUtil.getFirst(filter(iterable, predicate));
} finally {
CloseableUtil.close(iterable);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2022 Crown Copyright
* Copyright 2019-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -17,14 +17,14 @@
package uk.gov.gchq.koryphe.impl.function;

import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.google.common.collect.Lists;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.FunctionComposite;
import uk.gov.gchq.koryphe.tuple.function.TupleAdaptedFunction;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.function.Function;

Expand All @@ -43,7 +43,7 @@ public FunctionChain() {
}

public FunctionChain(final Function... functions) {
this(Lists.newArrayList(functions));
this(Arrays.asList(functions));
}

public FunctionChain(final List<Function> functions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,13 +16,13 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code IsEmpty} is a {@link KorypheFunction} which returns a boolean based
Expand All @@ -38,7 +38,7 @@ public Boolean apply(final Iterable input) {
throw new IllegalArgumentException("Input cannot be null");
}
try {
return Iterables.isEmpty(input);
return IterableUtil.isEmpty(input);
} finally {
CloseableUtil.close(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,13 +16,13 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code LastItem} is a {@link KorypheFunction} that returns the last item from a provided
Expand All @@ -41,7 +41,7 @@ public T apply(final Iterable<T> input) {
throw new IllegalArgumentException("Input cannot be null");
}
try {
return Iterables.getLast(input, null);
return IterableUtil.getLast(input);
} finally {
CloseableUtil.close(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,7 +16,6 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

Expand Down Expand Up @@ -58,9 +57,9 @@ public Integer apply(final Object value) {
length = ((Object[]) value).length;
} else if (value instanceof Iterable) {
if (null != maxLength) {
length = Iterables.size(IterableUtil.limit((Iterable) value, 0, maxLength, true));
length = IterableUtil.size(IterableUtil.limit((Iterable) value, 0, maxLength, true));
} else {
length = Iterables.size((Iterable) value);
length = IterableUtil.size((Iterable) value);
}
} else if (value instanceof Map) {
length = ((Map) value).size();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,7 +16,6 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
Expand All @@ -25,6 +24,7 @@
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code NthItem} is a {@link KorypheFunction} that returns an item based on user selection,
Expand Down Expand Up @@ -53,7 +53,7 @@ public T apply(final Iterable<T> input) {
throw new IllegalArgumentException("Input cannot be null");
}
try {
return Iterables.get(input, selection);
return IterableUtil.toList(input).get(selection);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This approach could be less efficient, depending on how Guava implements it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is guava's implementation. Ideally we will want to do something similar as converting to a list will be less efficient if the underlying object is not a List itself.

} finally {
CloseableUtil.close(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2017-2020 Crown Copyright
* Copyright 2017-2023 Crown Copyright
*
* 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,13 +16,13 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.CloseableUtil;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code Size} is a {@link KorypheFunction} which returns the size of a provided
Expand All @@ -38,7 +38,7 @@ public Integer apply(final Iterable input) {
throw new IllegalArgumentException("Input cannot be null");
}
try {
return Iterables.size(input);
return IterableUtil.size(input);
} finally {
CloseableUtil.close(input);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 Crown Copyright
* Copyright 2018-2023 Crown Copyright
*
* 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,11 +16,10 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Iterables;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.IterableUtil;

/**
* A {@code ToArray} is a {@link java.util.function.Function} that takes
Expand All @@ -43,7 +42,7 @@ public Object[] apply(final Object value) {
}

if (value instanceof Iterable) {
return Iterables.toArray(((Iterable) value), Object.class);
return IterableUtil.toList(((Iterable) value)).toArray();
}

return new Object[]{value};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 Crown Copyright
* Copyright 2019-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,7 +19,6 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.google.common.base.Charsets;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
Expand All @@ -30,6 +29,7 @@

import java.nio.charset.Charset;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Objects.isNull;
import static java.util.Objects.nonNull;

Expand All @@ -41,7 +41,7 @@
@Since("1.8.0")
@Summary("Extracts the bytes from a string")
public class ToBytes extends KorypheFunction<String, byte[]> {
public static final Charset DEFAULT_CHARSET = Charsets.UTF_8;
public static final Charset DEFAULT_CHARSET = UTF_8;

@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private Charset charset;
Expand Down
18 changes: 8 additions & 10 deletions core/src/main/java/uk/gov/gchq/koryphe/impl/function/ToList.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 Crown Copyright
* Copyright 2018-2023 Crown Copyright
*
* 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,12 +16,13 @@

package uk.gov.gchq.koryphe.impl.function;

import com.google.common.collect.Lists;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.util.IterableUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -37,20 +38,17 @@ public class ToList extends KorypheFunction<Object, List<?>> {
@Override
public List<?> apply(final Object value) {
if (null == value) {
return Lists.newArrayList((Object) null);
return Arrays.asList((Object) null);
}

if (value instanceof Object[]) {
return Lists.newArrayList((Object[]) value);
return new ArrayList<>(Arrays.asList((Object[]) value));
}

if (value instanceof Iterable) {
if (value instanceof List) {
return (List<?>) value;
}
return Lists.newArrayList((Iterable) value);
return IterableUtil.toList((Iterable) value);
}

return Lists.newArrayList(value);
return new ArrayList<>(Arrays.asList(value));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2018-2020 Crown Copyright
* Copyright 2018-2023 Crown Copyright
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -19,13 +19,14 @@
import com.fasterxml.jackson.annotation.JsonGetter;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonSetter;
import com.google.common.collect.Sets;

import uk.gov.gchq.koryphe.Since;
import uk.gov.gchq.koryphe.Summary;
import uk.gov.gchq.koryphe.function.KorypheFunction;
import uk.gov.gchq.koryphe.serialisation.json.SimpleClassNameIdResolver;
import uk.gov.gchq.koryphe.util.IterableUtil;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;
Expand Down Expand Up @@ -76,9 +77,9 @@ public Set<?> apply(final Object value) {
} else if (implementation.isInstance(value)) {
return (Set) value;
} else if (value instanceof Object[]) {
setImpl.addAll(Sets.newHashSet((Object[]) value));
setImpl.addAll(new HashSet<>(Arrays.asList((Object[]) value)));
} else if (value instanceof Iterable) {
setImpl.addAll(Sets.newHashSet((Iterable) value));
setImpl.addAll(new HashSet<>(IterableUtil.toList((Iterable) value)));
} else {
setImpl.add(value);
}
Expand Down
Loading