diff options
author | Henning Baldersheim <balder@yahoo-inc.com> | 2022-11-30 10:04:50 +0100 |
---|---|---|
committer | Henning Baldersheim <balder@yahoo-inc.com> | 2022-11-30 10:04:50 +0100 |
commit | 448231f18ba53edf5c0e7ab4b6732ef69328281c (patch) | |
tree | 289f528fd6adac2a39e636c449c633c26fdb838e /vespajlib/src | |
parent | 711362f17d4bbece0dc2d0833a22063374ae3e04 (diff) |
Reduce the simple usage of guava where java has caught up
Diffstat (limited to 'vespajlib/src')
25 files changed, 97 insertions, 325 deletions
diff --git a/vespajlib/src/main/java/com/yahoo/collections/ListMap.java b/vespajlib/src/main/java/com/yahoo/collections/ListMap.java index b73f50fe00f..d802012e71a 100644 --- a/vespajlib/src/main/java/com/yahoo/collections/ListMap.java +++ b/vespajlib/src/main/java/com/yahoo/collections/ListMap.java @@ -1,9 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.collections; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; - import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; import java.util.Collection; @@ -48,21 +45,13 @@ public class ListMap<K, V> { /** Puts an element into this. Multiple elements at the same position are added to the list at this key */ public void put(K key, V value) { - List<V> list = map.get(key); - if (list == null) { - list = new ArrayList<>(); - map.put(key, list); - } + List<V> list = map.computeIfAbsent(key, k -> new ArrayList<>()); list.add(value); } /** Put a key without adding a new value, such that there is an empty list of values if no values are already added */ public void put(K key) { - List<V> list = map.get(key); - if (list == null) { - list = new ArrayList<>(); - map.put(key, list); - } + map.computeIfAbsent(key, k -> new ArrayList<>()); } /** Put this map in the state where it has just the given value of the given key */ @@ -110,7 +99,7 @@ public class ListMap<K, V> { */ public List<V> get(K key) { List<V> list = map.get(key); - if (list == null) return ImmutableList.of();; + if (list == null) return List.of();; return list; } @@ -137,8 +126,8 @@ public class ListMap<K, V> { frozen = true; for (Map.Entry<K,List<V>> entry : map.entrySet()) - entry.setValue(ImmutableList.copyOf(entry.getValue())); - this.map = ImmutableMap.copyOf(this.map); + entry.setValue(List.copyOf(entry.getValue())); + this.map = Map.copyOf(this.map); } /** Returns whether this allows changes */ diff --git a/vespajlib/src/main/java/com/yahoo/errorhandling/Results.java b/vespajlib/src/main/java/com/yahoo/errorhandling/Results.java index 89dd44cb9fc..917b7251c15 100644 --- a/vespajlib/src/main/java/com/yahoo/errorhandling/Results.java +++ b/vespajlib/src/main/java/com/yahoo/errorhandling/Results.java @@ -1,11 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.errorhandling; -import com.google.common.collect.ImmutableList; - import java.util.ArrayList; import java.util.Collection; -import java.util.Collections; import java.util.List; /** @@ -17,8 +14,8 @@ public class Results<DATA, ERROR> { private final List<ERROR> errors; public Results(List<DATA> data, List<ERROR> errors) { - this.data = ImmutableList.copyOf(data); - this.errors = ImmutableList.copyOf(errors); + this.data = List.copyOf(data); + this.errors = List.copyOf(errors); } public boolean hasErrors() { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java index 50809ab3ff6..996d85b3199 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/IndexedTensor.java @@ -1,19 +1,16 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.NoSuchElementException; import java.util.Optional; import java.util.Set; -import java.util.function.DoubleBinaryOperator; /** * An indexed (dense) tensor. @@ -202,7 +199,7 @@ public abstract class IndexedTensor implements Tensor { @Override public Map<TensorAddress, Double> cells() { if (dimensionSizes.dimensions() == 0) - return Collections.singletonMap(TensorAddress.of(), get(0)); + return Map.of(TensorAddress.of(), get(0)); ImmutableMap.Builder<TensorAddress, Double> builder = new ImmutableMap.Builder<>(); Indexes indexes = Indexes.of(dimensionSizes, dimensionSizes, size()); @@ -574,7 +571,7 @@ public abstract class IndexedTensor implements Tensor { throw new IllegalArgumentException("Wrong number of indexes (" + indexes.length + ") for " + type); if (indexes.length == 0) { - firstDimension = Collections.singletonList(value); + firstDimension = List.of(value); return this; } @@ -767,7 +764,7 @@ public abstract class IndexedTensor implements Tensor { private final static class LazyCell extends Tensor.Cell { private double value; - private Indexes indexes; + private final Indexes indexes; private LazyCell(Indexes indexes, Double value) { super(null, value); @@ -919,10 +916,9 @@ public abstract class IndexedTensor implements Tensor { /** Returns an immutable list containing a copy of the indexes in this */ public List<Long> toList() { - ImmutableList.Builder<Long> builder = new ImmutableList.Builder<>(); - for (long index : indexes) - builder.add(index); - return builder.build(); + ArrayList<Long> list = new ArrayList<>(indexes.length); + for(long index : indexes) { list.add(index); } + return List.copyOf(list); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java index e7690876434..fed9f7017ed 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/MixedTensor.java @@ -11,7 +11,6 @@ import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.function.DoubleBinaryOperator; import java.util.stream.Collectors; /** diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java index d2762ad762d..2ee3d7dab60 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmax.java @@ -1,10 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.Name; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -17,21 +15,21 @@ public class Argmax<NAMETYPE extends Name> extends CompositeTensorFunction<NAMET private final List<String> dimensions; public Argmax(TensorFunction<NAMETYPE> argument) { - this(argument, Collections.emptyList()); + this(argument, List.of()); } public Argmax(TensorFunction<NAMETYPE> argument, String dimension) { - this(argument, Collections.singletonList(dimension)); + this(argument, List.of(dimension)); } public Argmax(TensorFunction<NAMETYPE> argument, List<String> dimensions) { Objects.requireNonNull(dimensions, "The dimensions cannot be null"); this.argument = argument; - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return Collections.singletonList(argument); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argument); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java index baedf41bcb8..a1e30c419e3 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Argmin.java @@ -1,10 +1,8 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.Name; -import java.util.Collections; import java.util.List; import java.util.Objects; @@ -17,21 +15,21 @@ public class Argmin<NAMETYPE extends Name> extends CompositeTensorFunction<NAMET private final List<String> dimensions; public Argmin(TensorFunction<NAMETYPE> argument) { - this(argument, Collections.emptyList()); + this(argument, List.of()); } public Argmin(TensorFunction<NAMETYPE> argument, String dimension) { - this(argument, Collections.singletonList(dimension)); + this(argument, List.of(dimension)); } public Argmin(TensorFunction<NAMETYPE> argument, List<String> dimensions) { Objects.requireNonNull(dimensions, "The dimensions cannot be null"); this.argument = argument; - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return Collections.singletonList(argument); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argument); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java index abf0d89c2b7..2a93acc19e6 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Concat.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; @@ -46,7 +45,7 @@ public class Concat<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { @@ -362,20 +361,11 @@ public class Concat<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET int b = 0; for (var how : plan.combineHow) { switch (how) { - case left: - labels[out++] = leftOnly.label(a++); - break; - case right: - labels[out++] = rightOnly.label(b++); - break; - case both: - labels[out++] = match.label(m++); - break; - case concat: - labels[out++] = String.valueOf(concatDimIdx); - break; - default: - throw new IllegalArgumentException("cannot handle: "+how); + case left -> labels[out++] = leftOnly.label(a++); + case right -> labels[out++] = rightOnly.label(b++); + case both -> labels[out++] = match.label(m++); + case concat -> labels[out++] = String.valueOf(concatDimIdx); + default -> throw new IllegalArgumentException("cannot handle: " + how); } } return TensorAddress.of(labels); @@ -419,17 +409,10 @@ public class Concat<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET int separateIdx = 0; for (int i = 0; i < how.handleDims.size(); i++) { switch (how.handleDims.get(i)) { - case common: - commonLabels[commonIdx++] = addr.label(i); - break; - case separate: - separateLabels[separateIdx++] = addr.label(i); - break; - case concat: - ccDimIndex = addr.numericLabel(i); - break; - default: - throw new IllegalArgumentException("cannot handle: "+how.handleDims.get(i)); + case common -> commonLabels[commonIdx++] = addr.label(i); + case separate -> separateLabels[separateIdx++] = addr.label(i); + case concat -> ccDimIndex = addr.numericLabel(i); + default -> throw new IllegalArgumentException("cannot handle: " + how.handleDims.get(i)); } } TensorAddress commonAddr = TensorAddress.of(commonLabels); diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java index c402a1bde5b..558b01baa02 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/DynamicTensor.java @@ -10,7 +10,6 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Objects; @@ -32,7 +31,7 @@ public abstract class DynamicTensor<NAMETYPE extends Name> extends PrimitiveTens public TensorType type(TypeContext<NAMETYPE> context) { return type; } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return Collections.emptyList(); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java index 1009177761b..61060b700a3 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Join.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.google.common.collect.Sets; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; @@ -15,7 +14,6 @@ import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; import java.util.ArrayList; -import java.util.Collections; import java.util.HashMap; import java.util.HashSet; import java.util.Iterator; @@ -60,7 +58,7 @@ public class Join<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMETYP public DoubleBinaryOperator combinator() { return combinator; } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { @@ -345,7 +343,7 @@ public class Join<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMETYP for (Iterator<Tensor.Cell> cellIterator = b.cellIterator(); cellIterator.hasNext(); ) { Tensor.Cell bCell = cellIterator.next(); TensorAddress partialCommonAddress = partialCommonAddress(bCell, bIndexesInCommon); - for (Tensor.Cell aCell : aCellsByCommonAddress.getOrDefault(partialCommonAddress, Collections.emptyList())) { + for (Tensor.Cell aCell : aCellsByCommonAddress.getOrDefault(partialCommonAddress, List.of())) { TensorAddress combinedAddress = joinAddresses(aCell.getKey(), aIndexesInJoined, bCell.getKey(), bIndexesInJoined, joinedType); if (combinedAddress == null) continue; // not combinable diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java index 3239ab1a70c..a06053c88a4 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Matmul.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.Name; @@ -27,7 +26,7 @@ public class Matmul<NAMETYPE extends Name> extends CompositeTensorFunction<NAMET } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return ImmutableList.of(argument1, argument2); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argument1, argument2); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java index 2b9dc709e0e..86d3ca50bd7 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Merge.java @@ -1,11 +1,7 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.Sets; -import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; -import com.yahoo.tensor.PartialAddress; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; import com.yahoo.tensor.TensorType; @@ -14,15 +10,10 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.ArrayList; -import java.util.Collections; -import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; -import java.util.Set; import java.util.function.DoubleBinaryOperator; /** @@ -55,7 +46,7 @@ public class Merge<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMETY public DoubleBinaryOperator merger() { return merger; } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return ImmutableList.of(argumentA, argumentB); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argumentA, argumentB); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java index 96465de6c0f..83d5187f116 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Reduce.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; @@ -37,12 +36,12 @@ public class Reduce<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET /** Creates a reduce function reducing all dimensions */ public Reduce(TensorFunction<NAMETYPE> argument, Aggregator aggregator) { - this(argument, aggregator, Collections.emptyList()); + this(argument, aggregator, List.of()); } /** Creates a reduce function reducing a single dimension */ public Reduce(TensorFunction<NAMETYPE> argument, Aggregator aggregator, String dimension) { - this(argument, aggregator, Collections.singletonList(dimension)); + this(argument, aggregator, List.of(dimension)); } /** @@ -57,7 +56,7 @@ public class Reduce<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET public Reduce(TensorFunction<NAMETYPE> argument, Aggregator aggregator, List<String> dimensions) { this.argument = Objects.requireNonNull(argument, "The argument tensor cannot be null"); this.aggregator = Objects.requireNonNull(aggregator, "The aggregator cannot be null"); - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } public static TensorType outputType(TensorType inputType, List<String> reduceDimensions) { @@ -71,7 +70,7 @@ public class Reduce<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET List<String> dimensions() { return dimensions; } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return Collections.singletonList(argument); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argument); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { @@ -174,16 +173,16 @@ public class Reduce<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET static abstract class ValueAggregator { static ValueAggregator ofType(Aggregator aggregator) { - switch (aggregator) { - case avg : return new AvgAggregator(); - case count : return new CountAggregator(); - case max : return new MaxAggregator(); - case median : return new MedianAggregator(); - case min : return new MinAggregator(); - case prod : return new ProdAggregator(); - case sum : return new SumAggregator(); - default: throw new UnsupportedOperationException("Aggregator " + aggregator + " is not implemented"); - } + return switch (aggregator) { + case avg -> new AvgAggregator(); + case count -> new CountAggregator(); + case max -> new MaxAggregator(); + case median -> new MedianAggregator(); + case min -> new MinAggregator(); + case prod -> new ProdAggregator(); + case sum -> new SumAggregator(); + default -> throw new UnsupportedOperationException("Aggregator " + aggregator + " is not implemented"); + }; } diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java index ccb437ef5a7..1048e5ab10e 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ReduceJoin.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.DimensionSizes; import com.yahoo.tensor.IndexedTensor; import com.yahoo.tensor.Tensor; @@ -47,12 +46,12 @@ public class ReduceJoin<NAMETYPE extends Name> extends CompositeTensorFunction<N this.argumentB = argumentB; this.combinator = combinator; this.aggregator = aggregator; - this.dimensions = ImmutableList.copyOf(dimensions); + this.dimensions = List.copyOf(dimensions); } @Override public List<TensorFunction<NAMETYPE>> arguments() { - return ImmutableList.of(argumentA, argumentB); + return List.of(argumentA, argumentB); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java index 023e91e424f..8d58cee5e1e 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Rename.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorAddress; import com.yahoo.tensor.TensorType; @@ -10,7 +9,6 @@ import com.yahoo.tensor.evaluation.EvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.TypeContext; -import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.List; @@ -30,7 +28,7 @@ public class Rename<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET private final Map<String, String> fromToMap; public Rename(TensorFunction<NAMETYPE> argument, String fromDimension, String toDimension) { - this(argument, ImmutableList.of(fromDimension), ImmutableList.of(toDimension)); + this(argument, List.of(fromDimension), List.of(toDimension)); } public Rename(TensorFunction<NAMETYPE> argument, List<String> fromDimensions, List<String> toDimensions) { @@ -43,8 +41,8 @@ public class Rename<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET throw new IllegalArgumentException("Rename from and to dimensions must be equal, was " + fromDimensions.size() + " and " + toDimensions.size()); this.argument = argument; - this.fromDimensions = ImmutableList.copyOf(fromDimensions); - this.toDimensions = ImmutableList.copyOf(toDimensions); + this.fromDimensions = List.copyOf(fromDimensions); + this.toDimensions = List.copyOf(toDimensions); this.fromToMap = fromToMap(fromDimensions, toDimensions); } @@ -59,7 +57,7 @@ public class Rename<NAMETYPE extends Name> extends PrimitiveTensorFunction<NAMET } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return Collections.singletonList(argument); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(argument); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java index 2639e153923..1057ffa9552 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/ScalarFunctions.java @@ -1,8 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; - import java.util.Comparator; import java.util.List; import java.util.Objects; @@ -510,9 +508,9 @@ public class ScalarFunctions { // Variable-length operators ----------------------------------------------------------------------------- public static class EqualElements implements Function<List<Long>, Double> { - private final ImmutableList<String> argumentNames; + private final List<String> argumentNames; private EqualElements(List<String> argumentNames) { - this.argumentNames = ImmutableList.copyOf(argumentNames); + this.argumentNames = List.copyOf(argumentNames); } @Override @@ -553,9 +551,9 @@ public class ScalarFunctions { } public static class SumElements implements Function<List<Long>, Double> { - private final ImmutableList<String> argumentNames; + private final List<String> argumentNames; private SumElements(List<String> argumentNames) { - this.argumentNames = ImmutableList.copyOf(argumentNames); + this.argumentNames = List.copyOf(argumentNames); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java index df8cd6d39cd..0853e1becf6 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/Softmax.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.TensorType; import com.yahoo.tensor.evaluation.Name; @@ -23,7 +22,7 @@ public class Softmax<NAMETYPE extends Name> extends CompositeTensorFunction<NAME } public static TensorType outputType(TensorType inputType, String dimension) { - return Reduce.outputType(inputType, ImmutableList.of(dimension)); + return Reduce.outputType(inputType, List.of(dimension)); } @Override diff --git a/vespajlib/src/main/java/com/yahoo/tensor/functions/XwPlusB.java b/vespajlib/src/main/java/com/yahoo/tensor/functions/XwPlusB.java index bd4fc7b8336..499cd31d700 100644 --- a/vespajlib/src/main/java/com/yahoo/tensor/functions/XwPlusB.java +++ b/vespajlib/src/main/java/com/yahoo/tensor/functions/XwPlusB.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.Name; import java.util.List; @@ -23,7 +22,7 @@ public class XwPlusB<NAMETYPE extends Name> extends CompositeTensorFunction<NAME } @Override - public List<TensorFunction<NAMETYPE>> arguments() { return ImmutableList.of(x, w, b); } + public List<TensorFunction<NAMETYPE>> arguments() { return List.of(x, w, b); } @Override public TensorFunction<NAMETYPE> withArguments(List<TensorFunction<NAMETYPE>> arguments) { diff --git a/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java b/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java index fb90fabb964..a177b07256b 100644 --- a/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java +++ b/vespajlib/src/main/java/com/yahoo/text/StringUtilities.java @@ -1,11 +1,9 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.text; -import com.google.common.collect.ImmutableSet; - import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; -import java.util.Collections; +import java.util.HashSet; import java.util.List; import java.io.ByteArrayOutputStream; import java.util.Set; @@ -212,12 +210,12 @@ public class StringUtilities { /** Splits a string on both space and comma */ public static Set<String> split(String s) { - if (s == null || s.isEmpty()) return Collections.emptySet(); - ImmutableSet.Builder<String> b = new ImmutableSet.Builder<>(); + if (s == null || s.isEmpty()) return Set.of(); + Set<String> b = new HashSet<>(); for (String item : s.split("[\\s,]")) if ( ! item.isEmpty()) b.add(item); - return b.build(); + return Set.copyOf(b); } } diff --git a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java index a5b2accec48..b8a13525159 100644 --- a/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java +++ b/vespajlib/src/test/java/com/yahoo/stream/CustomCollectorsTest.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.stream; -import com.google.common.collect.Lists; import com.yahoo.stream.CustomCollectors.DuplicateKeyException; import org.junit.Test; @@ -42,7 +41,7 @@ public class CustomCollectorsTest { @Test public void custom_map_collector_throws_exception_upon_duplicate_keys() { - List<String> duplicates = Lists.newArrayList("same", "same"); + List<String> duplicates = List.of("same", "same"); try { duplicates.stream().collect(toCustomMap(Function.identity(), Function.identity(), HashMap::new)); @@ -53,7 +52,7 @@ public class CustomCollectorsTest { } private static List<String> numberList() { - return Lists.newArrayList("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); + return List.of("zero", "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"); } private static class CustomHashMap<K,V> extends HashMap<K,V> { diff --git a/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java index ba814f7ad54..09b989e4380 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/MappedTensorTestCase.java @@ -1,9 +1,10 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.Sets; import org.junit.Test; +import java.util.Set; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -34,7 +35,7 @@ public class MappedTensorTestCase { Tensor tensor = Tensor.Builder.of(type). cell().label("x", "0").value(1). cell().label("x", "1").value(2).build(); - assertEquals(Sets.newHashSet("x"), tensor.type().dimensionNames()); + assertEquals(Set.of("x"), tensor.type().dimensionNames()); assertEquals("tensor(x{}):{0:1.0, 1:2.0}", tensor.toString()); } @@ -44,7 +45,7 @@ public class MappedTensorTestCase { Tensor tensor = Tensor.Builder.of(type). cell().label("x", "0").label("y", "0").value(1). cell().label("x", "1").label("y", "0").value(2).build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y{}):{{x:0,y:0}:1.0, {x:1,y:0}:2.0}", tensor.toString()); } diff --git a/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java index a26e56c4468..0b5b761224b 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/MixedTensorTestCase.java @@ -2,9 +2,10 @@ package com.yahoo.tensor; -import com.google.common.collect.Sets; import org.junit.Test; +import java.util.Set; + import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -40,7 +41,7 @@ public class MixedTensorTestCase { cell().label("y", 1).value(2). // {y:2} should be 0.0 and non NaN since we specify indexed size build(); - assertEquals(Sets.newHashSet("y"), tensor.type().dimensionNames()); + assertEquals(Set.of("y"), tensor.type().dimensionNames()); assertEquals("tensor(y[3]):[1.0, 2.0, 0.0]", tensor.toString()); } @@ -56,7 +57,7 @@ public class MixedTensorTestCase { cell().label("x", 1).label("y", 1).value(5). cell().label("x", 1).label("y", 2).value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x[2],y[3]):[[1.0, 2.0, 0.0], [4.0, 5.0, 6.0]]", tensor.toString()); } @@ -68,7 +69,7 @@ public class MixedTensorTestCase { cell().label("x", "0").value(1). cell().label("x", "1").value(2). build(); - assertEquals(Sets.newHashSet("x"), tensor.type().dimensionNames()); + assertEquals(Set.of("x"), tensor.type().dimensionNames()); assertEquals("tensor(x{}):{0:1.0, 1:2.0}", tensor.toString()); } @@ -83,7 +84,7 @@ public class MixedTensorTestCase { cell().label("x", "1").label("y", "1").value(5). cell().label("x", "1").label("y", "2").value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y{}):{{x:0,y:0}:1.0, {x:0,y:1}:2.0, {x:1,y:0}:4.0, {x:1,y:1}:5.0, {x:1,y:2}:6.0}", tensor.toString()); } @@ -99,7 +100,7 @@ public class MixedTensorTestCase { cell().label("x", "2").label("y", 1).value(5). cell().label("x", "2").label("y", 2).value(6). build(); - assertEquals(Sets.newHashSet("x", "y"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y[3]):{1:[1.0, 2.0, 0.0], 2:[4.0, 5.0, 6.0]}", tensor.toString()); } @@ -121,7 +122,7 @@ public class MixedTensorTestCase { cell().label("x", "x2").label("y", 2).label("z","z1").value(15). cell().label("x", "x2").label("y", 2).label("z","z2").value(16). build(); - assertEquals(Sets.newHashSet("x", "y", "z"), tensor.type().dimensionNames()); + assertEquals(Set.of("x", "y", "z"), tensor.type().dimensionNames()); assertEquals("tensor(x{},y[3],z{}):{{x:x1,y:0,z:z1}:1.0, {x:x1,y:0,z:z2}:2.0, {x:x1,y:1,z:z1}:3.0, " + "{x:x1,y:1,z:z2}:4.0, {x:x1,y:2,z:z1}:5.0, {x:x1,y:2,z:z2}:6.0, {x:x2,y:0,z:z1}:11.0, " + "{x:x2,y:0,z:z2}:12.0, {x:x2,y:1,z:z1}:13.0, {x:x2,y:1,z:z2}:14.0, {x:x2,y:2,z:z1}:15.0, {x:x2,y:2,z:z2}:16.0}", @@ -149,7 +150,7 @@ public class MixedTensorTestCase { cell().label("i", "b").label("k","d").label("j",1).label("l",0).value(15). cell().label("i", "b").label("k","d").label("j",1).label("l",1).value(16). build(); - assertEquals(Sets.newHashSet("i", "j", "k", "l"), tensor.type().dimensionNames()); + assertEquals(Set.of("i", "j", "k", "l"), tensor.type().dimensionNames()); assertEquals("tensor(i{},j[2],k{},l[2]):{{i:a,j:0,k:c,l:0}:1.0, {i:a,j:0,k:c,l:1}:2.0, " + "{i:a,j:0,k:d,l:0}:5.0, {i:a,j:0,k:d,l:1}:6.0, {i:a,j:1,k:c,l:0}:3.0, {i:a,j:1,k:c,l:1}:4.0, " + "{i:a,j:1,k:d,l:0}:7.0, {i:a,j:1,k:d,l:1}:8.0, {i:b,j:0,k:c,l:0}:9.0, {i:b,j:0,k:c,l:1}:10.0, " + diff --git a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java index 428c8a83b47..5e392a74bf3 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/TensorTestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.evaluation.MapEvaluationContext; import com.yahoo.tensor.evaluation.Name; import com.yahoo.tensor.evaluation.VariableTensor; @@ -154,7 +153,7 @@ public class TensorTestCase { Tensor xy = Tensor.from("{{x:0,y:1}:11, {x:1,y:1}:13}"); double nest1 = y.multiply(x.multiply(xy).sum("x")).sum("y").asDouble(); double nest2 = x.multiply(xy).sum("x").multiply(y).sum("y").asDouble(); - double flat = y.multiply(x).multiply(xy).sum(ImmutableList.of("x","y")).asDouble(); + double flat = y.multiply(x).multiply(xy).sum(List.of("x","y")).asDouble(); assertEquals(nest1, flat, 0.000000001); assertEquals(nest2, flat, 0.000000001); } @@ -176,8 +175,8 @@ public class TensorTestCase { assertEquals(Tensor.from("{ {x:1,y:1}:0, {x:2,y:1}:1 }"), tensor1.larger(tensor2)); assertEquals(Tensor.from("{ {y:1}:50.0 }"), tensor1.matmul(tensor2, "x")); assertEquals(Tensor.from("{ {z:1}:3, {z:2}:7 }"), tensor1.rename("x", "z")); - assertEquals(Tensor.from("{ {y:1,x:1}:8, {x:1,y:2}:12 }"), tensor1.add(tensor2).rename(ImmutableList.of("x", "y"), - ImmutableList.of("y", "x"))); + assertEquals(Tensor.from("{ {y:1,x:1}:8, {x:1,y:2}:12 }"), tensor1.add(tensor2).rename(List.of("x", "y"), + List.of("y", "x"))); assertEquals(Tensor.from("{ {x:0,y:0}:0, {x:0,y:1}:0, {x:1,y:0}:0, {x:1,y:1}:1, {x:2,y:0}:0, {x:2,y:1}:2, }"), Tensor.generate(new TensorType.Builder().indexed("x", 3).indexed("y", 2).build(), (List<Long> indexes) -> (double)indexes.get(0)*indexes.get(1))); diff --git a/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java b/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java index fb4cb659624..1ac796ae9c5 100644 --- a/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java +++ b/vespajlib/src/test/java/com/yahoo/tensor/functions/MatmulTestCase.java @@ -1,11 +1,12 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.tensor.functions; -import com.google.common.collect.ImmutableList; import com.yahoo.tensor.Tensor; import com.yahoo.tensor.TensorType; import org.junit.Test; +import java.util.List; + import static org.junit.Assert.assertEquals; /** @@ -41,7 +42,7 @@ public class MatmulTestCase { rb.cell(154,1, 1); Tensor r = rb.build(); - Tensor result = a.matmul(b.rename(ImmutableList.of("d0","d1"), ImmutableList.of("d1","d2")), "d1") + Tensor result = a.matmul(b.rename(List.of("d0","d1"), List.of("d1","d2")), "d1") .rename("d2","d1"); assertEquals(r, result); } @@ -90,7 +91,7 @@ public class MatmulTestCase { rb.cell(730,1, 1, 1); Tensor r = rb.build(); - Tensor result = a.matmul(b.rename(ImmutableList.of("d1","d2"), ImmutableList.of("d2","d3")), "d2") + Tensor result = a.matmul(b.rename(List.of("d1","d2"), List.of("d2","d3")), "d2") .rename("d3","d2"); assertEquals(r, result); } diff --git a/vespajlib/src/test/java/com/yahoo/text/Benchmark.java b/vespajlib/src/test/java/com/yahoo/text/Benchmark.java deleted file mode 100644 index 292503f462a..00000000000 --- a/vespajlib/src/test/java/com/yahoo/text/Benchmark.java +++ /dev/null @@ -1,108 +0,0 @@ -// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. -package com.yahoo.text; - -// import com.google.common.base.Preconditions; -// import com.google.inject.Provider; - -import java.lang.reflect.InvocationTargetException; -import java.util.ArrayList; -import java.util.List; -import java.util.Objects; -import java.util.concurrent.Callable; -import java.util.concurrent.CyclicBarrier; -import java.util.concurrent.Executors; -import java.util.concurrent.Future; -import java.util.concurrent.TimeUnit; - -/** - * @author Simon Thoresen Hult - */ -class Benchmark { - - public static interface Task { - public long run(CyclicBarrier barrier, int numIterations) throws Exception; - } - - - public static class TaskProvider { - final Class<? extends Task> taskClass; - public Task get() { - try { - return taskClass.getDeclaredConstructor().newInstance(); - } catch (InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { - throw new RuntimeException(e); - } - } - public TaskProvider(final Class<? extends Task> taskClass) { - this.taskClass = taskClass; - } - } - - private final TaskProvider taskProvider; - private final int numIterationsPerThread; - private final int numThreads; - - private Benchmark(Builder builder) { - Objects.requireNonNull(builder.taskProvider, "taskProvider"); -/* - Preconditions.checkArgument(builder.numIterationsPerThread > 0, "numIterationsPerThread; %s", - builder.numIterationsPerThread); - Preconditions.checkArgument(builder.numThreads > 0, "numThreads; %s", - builder.numThreads); -*/ - taskProvider = builder.taskProvider; - numIterationsPerThread = builder.numIterationsPerThread; - numThreads = builder.numThreads; - } - - public long run() throws Exception { - final CyclicBarrier barrier = new CyclicBarrier(numThreads); - List<Callable<Long>> clients = new ArrayList<>(numThreads); - for (int i = 0; i < numThreads; ++i) { - final Task task = taskProvider.get(); - clients.add(new Callable<Long>() { - - @Override - public Long call() throws Exception { - return task.run(barrier, numIterationsPerThread); - } - }); - } - long maxNanosPerClient = 0; - for (Future<Long> result : Executors.newFixedThreadPool(numThreads).invokeAll(clients)) { - maxNanosPerClient = Math.max(maxNanosPerClient, result.get()); - } - return TimeUnit.SECONDS.toNanos(1) * numThreads * numIterationsPerThread / maxNanosPerClient; - } - - public static class Builder { - - private TaskProvider taskProvider; - private int numIterationsPerThread = 1000; - private int numThreads = 1; - - public Builder setNumThreads(int numThreads) { - this.numThreads = numThreads; - return this; - } - - public Builder setNumIterationsPerThread(int numIterationsPerThread) { - this.numIterationsPerThread = numIterationsPerThread; - return this; - } - - public Builder setTaskClass(final Class<? extends Task> taskClass) { - return setTaskProvider(new TaskProvider(taskClass)); - } - - public Builder setTaskProvider(TaskProvider taskProvider) { - this.taskProvider = taskProvider; - return this; - } - - public Benchmark build() { - return new Benchmark(this); - } - } - -} diff --git a/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java b/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java index 926d19f433f..098eaddacac 100644 --- a/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java +++ b/vespajlib/src/test/java/com/yahoo/text/Utf8TestCase.java @@ -1,7 +1,6 @@ // Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. package com.yahoo.text; -import com.google.common.collect.ImmutableMap; import org.junit.Ignore; import org.junit.Test; @@ -11,9 +10,9 @@ import java.nio.ByteBuffer; import java.nio.charset.CharsetEncoder; import java.nio.charset.StandardCharsets; import java.util.Arrays; +import java.util.Map; import java.util.function.Function; -import static com.yahoo.text.Lowercase.toLowerCase; import static com.yahoo.text.Utf8.calculateBytePositions; import static com.yahoo.text.Utf8.calculateStringPositions; import static org.hamcrest.CoreMatchers.equalTo; @@ -21,7 +20,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; /** * @author <a href="mailto:borud@yahoo-inc.com">Bjorn Borud</a> @@ -34,62 +32,6 @@ public class Utf8TestCase { 0x34, 0x355, 0x2567, 0xfff, 0xe987, 0x100abc }; - public void dumpSome() throws java.io.IOException { - int i = 32; - int j = 3; - int cnt = 0; - while (i < 0x110000) { - if (i < 0xD800 || i >= 0xE000) ++cnt; - i += j; - ++j; - } - System.out.println("allocate "+cnt+" array entries"); - int codes[] = new int[cnt]; - i = 32; - j = 3; - cnt = 0; - while (i < 0x110000) { - if (i < 0xD800 || i >= 0xE000) codes[cnt++] = i; - i += j; - ++j; - } - assertEquals(cnt, codes.length); - System.out.println("fill "+cnt+" array entries"); - String str = new String(codes, 0, cnt); - byte[] arr = Utf8.toBytes(str); - java.io.FileOutputStream fos = new java.io.FileOutputStream("random-long-utf8.dat"); - fos.write(arr); - fos.close(); - } - - public void dumpMore() throws java.io.IOException { - java.text.Normalizer.Form form = java.text.Normalizer.Form.NFKC; - - java.io.FileOutputStream fos = new java.io.FileOutputStream("lowercase-table.dat"); - for (int i = 0; i < 0x110000; i++) { - StringBuilder b = new StringBuilder(); - b.appendCodePoint(i); - String n1 = b.toString(); - String n2 = java.text.Normalizer.normalize(b, form); - if (n1.equals(n2)) { - String l = toLowerCase(n1); - int chars = l.length(); - int codes = l.codePointCount(0, chars); - if (codes != 1) { - System.out.println("codepoint "+i+" transformed into "+codes+" codepoints: "+n1+" -> "+l); - } else { - int lc = l.codePointAt(0); - if (lc != i) { - String o = "lowercase( "+i+" )= "+lc+"\n"; - byte[] arr = Utf8.toBytes(o); - fos.write(arr); - } - } - } - } - fos.close(); - } - @Test public void testSimple() { String s1 = "test"; @@ -322,7 +264,7 @@ public class Utf8TestCase { for (char c=0; c < i; c++) { sb.append(c); } - assertTrue(Arrays.equals(Utf8.toBytesStd(sb.toString()), Utf8.toBytes(sb.toString()))); + assertArrayEquals(Utf8.toBytesStd(sb.toString()), Utf8.toBytes(sb.toString())); } } @@ -340,7 +282,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } @@ -348,8 +290,8 @@ public class Utf8TestCase { public void testBoolean() { assertEquals("true", String.valueOf(true)); assertEquals("false", String.valueOf(false)); - assertTrue(Arrays.equals(Utf8.toAsciiBytes(true), new Utf8String(String.valueOf(true)).getBytes())); - assertTrue(Arrays.equals(Utf8.toAsciiBytes(false), new Utf8String(String.valueOf(false)).getBytes())); + assertArrayEquals(Utf8.toAsciiBytes(true), new Utf8String(String.valueOf(true)).getBytes()); + assertArrayEquals(Utf8.toAsciiBytes(false), new Utf8String(String.valueOf(false)).getBytes()); } @Test public void testInt() @@ -358,7 +300,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } } @@ -369,7 +311,7 @@ public class Utf8TestCase { byte [] a = Utf8.toBytes(String.valueOf(l)); byte [] b = Utf8.toAsciiBytes(l); if (!Arrays.equals(a, b)) { - assertTrue(Arrays.equals(a, b)); + assertArrayEquals(a, b); } } } @@ -561,7 +503,7 @@ public class Utf8TestCase { byte[] unicode = "This is just sort of random mix. \u5370\u57df\u60c5\u5831\u53EF\u4EE5\u6709x\u00e9\u00e8".getBytes(StandardCharsets.UTF_8); int iterations = 100_000; // Use 100_000+ for benchmarking - ImmutableMap.of("ascii", ascii, "unicode", unicode).forEach((type, b) -> { + Map.of("ascii", ascii, "unicode", unicode).forEach((type, b) -> { long time1 = benchmark(() -> decode(Utf8::toString, b, iterations)); System.out.printf("Utf8::toString of %s string took %d ms\n", type, time1); long time2 = benchmark(() -> decode((b1) -> new String(b1, StandardCharsets.UTF_8), b, iterations)); @@ -578,7 +520,7 @@ public class Utf8TestCase { String unicode = "This is just sort of random mix. \u5370\u57df\u60c5\u5831\u53EF\u4EE5\u6709x\u00e9\u00e8"; int iterations = 1_000_000; // Use 1_000_000+ for benchmarking - ImmutableMap.of("ascii", ascii, "unicode", unicode).forEach((type, s) -> { + Map.of("ascii", ascii, "unicode", unicode).forEach((type, s) -> { long time1 = benchmark(() -> encode(Utf8::toBytes, s, iterations)); System.out.printf("Utf8::toBytes of %s string took %d ms\n", type, time1); long time2 = benchmark(() -> encode((s1) -> s1.getBytes(StandardCharsets.UTF_8), s, iterations)); |