From e8df66f08412616d51056601832e4560cdac9910 Mon Sep 17 00:00:00 2001 From: David Noble Date: Fri, 13 Sep 2019 03:52:44 -0700 Subject: [PATCH] Progressed on port from dotnet to java. --- .../azure/data/cosmos/core/Utf8String.java | 33 +++------ java/src/main/java/tangible/Func0Param.java | 9 --- .../main/java/tangible/TryParseHelper.java | 71 ------------------- 3 files changed, 9 insertions(+), 104 deletions(-) delete mode 100644 java/src/main/java/tangible/Func0Param.java delete mode 100644 java/src/main/java/tangible/TryParseHelper.java diff --git a/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java b/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java index 03d613b..07530c1 100644 --- a/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java +++ b/java/src/main/java/com/azure/data/cosmos/core/Utf8String.java @@ -16,6 +16,7 @@ import com.fasterxml.jackson.databind.ser.std.StdSerializer; import com.google.common.base.Objects; import com.google.common.base.Suppliers; import com.google.common.base.Utf8; +import com.google.common.collect.Streams; import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBufHolder; import io.netty.buffer.Unpooled; @@ -145,30 +146,14 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable return other == null ? 0 : -1; } - final int length = this.length(); - final int otherLength = other.length(); - final int limit = Math.min(length, otherLength); + // TODO: DANOBLE: Consider optimizing this method based on the cost of zipping boxed int streams - if (limit > 0) { + Optional compare = Streams + .zip(this.codePoints().boxed(), other.codePoints().boxed(), (x, y) -> x - y) + .filter(delta -> delta != 0) + .findFirst(); - final CodePointIterable iterable = new CodePointIterable(this.buffer, this.utf16CodeUnitCount.get()); - int index = 0; - - for (int codePoint : iterable) { - - final int otherCodePoint = other.codePointAt(index++); - - if (codePoint != otherCodePoint) { - return codePoint - otherCodePoint; - } - - if (index >= limit) { - break; - } - } - } - - return length - otherLength; + return compare.orElse(this.length() - other.length()); } /** @@ -547,7 +532,7 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable private int skip = 0; @Override - public boolean process(byte value) throws Exception { + public boolean process(byte value) { if (this.skip > 0) { this.skip--; @@ -678,7 +663,7 @@ public final class Utf8String implements ByteBufHolder, CharSequence, Comparable if (!Character.isDefined(this.codePoint)) { this.codePoint = REPLACEMENT_CHARACTER; - }; + } return false; } diff --git a/java/src/main/java/tangible/Func0Param.java b/java/src/main/java/tangible/Func0Param.java deleted file mode 100644 index 4fa0067..0000000 --- a/java/src/main/java/tangible/Func0Param.java +++ /dev/null @@ -1,9 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package tangible; - -@FunctionalInterface -public interface Func0Param { - TResult invoke(); -} \ No newline at end of file diff --git a/java/src/main/java/tangible/TryParseHelper.java b/java/src/main/java/tangible/TryParseHelper.java deleted file mode 100644 index 14c6f9b..0000000 --- a/java/src/main/java/tangible/TryParseHelper.java +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright (c) Microsoft Corporation. All rights reserved. -// Licensed under the MIT License. - -package tangible; - -import com.azure.data.cosmos.core.Out; - -public final class TryParseHelper { - public static boolean tryParseBoolean(String s, Out result) { - try { - result.setAndGet(Boolean.parseBoolean(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseByte(String s, Out result) { - try { - result.setAndGet(Byte.parseByte(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseDouble(String s, Out result) { - try { - result.setAndGet(Double.parseDouble(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseFloat(String s, Out result) { - try { - result.setAndGet(Float.parseFloat(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseInt(String s, Out result) { - try { - result.setAndGet(Integer.parseInt(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseLong(String s, Out result) { - try { - result.setAndGet(Long.parseLong(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } - - public static boolean tryParseShort(String s, Out result) { - try { - result.setAndGet(Short.parseShort(s)); - return true; - } catch (NumberFormatException e) { - return false; - } - } -} \ No newline at end of file