Progressed on port from dotnet to java

This commit is contained in:
David Noble
2019-09-09 22:05:00 -07:00
parent 2db3d8d517
commit 05577333c0
65 changed files with 1514 additions and 1438 deletions

View File

@@ -0,0 +1,25 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow;
public interface ISpanResizer<T> {
/**
* Resizes an existing a buffer.
* <typeparam name="T">The type of the elements of the memory.</typeparam>
*
* @param minimumLength The minimum required length (in elements) of the memory.
* @param buffer Optional existing memory to be copied to the new buffer. Ownership of <paramref
* name="buffer" /> is
* transferred as part of this call and it should not be used by the caller after this call
* completes.
* @return A new memory whose size is <em>at least as big</em> as <paramref name="minimumLength" />
* and containing the content of <paramref name="buffer" />.
*/
Span<T> Resize(int minimumLength);
//C# TO JAVA CONVERTER NOTE: Java does not support optional parameters. Overloaded method(s) are created above:
//ORIGINAL LINE: Span<T> Resize(int minimumLength, Span<T> buffer = default);
Span<T> Resize(int minimumLength, Span<T> buffer);
}

View File

@@ -0,0 +1,55 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow;
import static com.google.common.base.Preconditions.checkArgument;
public final class MemorySpanResizer<T> implements ISpanResizer<T> {
private Memory<T> memory;
public MemorySpanResizer() {
this(0);
}
//C# TO JAVA CONVERTER NOTE: Java does not support optional parameters. Overloaded method(s) are created above:
//ORIGINAL LINE: public MemorySpanResizer(int initialCapacity = 0)
public MemorySpanResizer(int initialCapacity) {
checkArgument(initialCapacity >= 0);
//C# TO JAVA CONVERTER WARNING: Java does not allow direct instantiation of arrays of generic type parameters:
//ORIGINAL LINE: this.memory = initialCapacity == 0 ? default : new Memory<T>(new T[initialCapacity]);
this.memory = initialCapacity == 0 ? null : new Memory<T>((T[])new Object[initialCapacity]);
}
public Memory<T> getMemory() {
return this.memory;
}
/**
* <inheritdoc />
*/
public Span<T> Resize(int minimumLength) {
return Resize(minimumLength, null);
}
//C# TO JAVA CONVERTER NOTE: Java does not support optional parameters. Overloaded method(s) are created above:
//ORIGINAL LINE: public Span<T> Resize(int minimumLength, Span<T> buffer = default)
public Span<T> Resize(int minimumLength, Span<T> buffer) {
if (this.memory.Length < minimumLength) {
//C# TO JAVA CONVERTER WARNING: Java does not allow direct instantiation of arrays of generic type
// parameters:
//ORIGINAL LINE: this.memory = new Memory<T>(new T[Math.Max(minimumLength, buffer.Length)]);
this.memory = new Memory<T>((T[])new Object[Math.max(minimumLength, buffer.Length)]);
}
Span<T> next = this.memory.Span;
if (!buffer.IsEmpty && next.Slice(0, buffer.Length) != buffer) {
buffer.CopyTo(next);
}
return next;
}
}

View File

@@ -0,0 +1,84 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow.schemas;
import Newtonsoft.Json.*;
import Newtonsoft.Json.Converters.*;
import Newtonsoft.Json.Linq.*;
/**
* Helper class for parsing the polymorphic {@link PropertyType} subclasses from JSON.
*/
public class PropertySchemaConverter extends JsonConverter {
@Override
public boolean getCanWrite() {
return false;
}
@Override
public boolean CanConvert(java.lang.Class objectType) {
return objectType.isAssignableFrom(PropertyType.class);
}
@Override
public Object ReadJson(JsonReader reader, java.lang.Class objectType, Object existingValue,
JsonSerializer serializer) {
PropertyType p;
if (reader.TokenType != JsonToken.StartObject) {
throw new JsonSerializationException();
}
JObject propSchema = JObject.Load(reader);
TypeKind propType;
JToken value;
// TODO: C# TO JAVA CONVERTER: The following method call contained an unresolved 'out' keyword - these
// cannot be converted using the 'Out' helper class unless the method is within the code being modified:
if (!propSchema.TryGetValue("type", out value)) {
throw new JsonSerializationException("Required \"type\" property missing.");
}
try (JsonReader typeReader = value.CreateReader()) {
typeReader.Read(); // Move to the start token
propType = TypeKind.forValue((new StringEnumConverter(true)).ReadJson(typeReader, TypeKind.class, null,
serializer));
}
switch (propType) {
case Array:
p = new ArrayPropertyType();
break;
case SET:
p = new SetPropertyType();
break;
case MAP:
p = new MapPropertyType();
break;
case Object:
p = new ObjectPropertyType();
break;
case Tuple:
p = new TuplePropertyType();
break;
case TAGGED:
p = new TaggedPropertyType();
break;
case Schema:
p = new UdtPropertyType();
break;
default:
p = new PrimitivePropertyType();
break;
}
serializer.Populate(propSchema.CreateReader(), p);
return p;
}
// TODO: C# TO JAVA CONVERTER: Java annotations will not correspond to .NET attributes:
//ORIGINAL LINE: [ExcludeFromCodeCoverage] public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer)
@Override
public void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) {
}
}

View File

@@ -0,0 +1,48 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow.layouts;
public class SamplingStringComparer implements IEqualityComparer<String> {
public static final SamplingStringComparer Default = new SamplingStringComparer();
public final boolean equals(String x, String y) {
checkArgument(x != null);
checkArgument(y != null);
return x.equals(y);
}
public final int hashCode(String obj) {
checkArgument(obj != null);
// TODO: C# TO JAVA CONVERTER: There is no equivalent to an 'unchecked' block in Java:
unchecked
{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint hash1 = 5381;
int hash1 = 5381;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint hash2 = hash1;
int hash2 = hash1;
final int numSamples = 4;
final int modulus = 13;
ReadOnlySpan<Character> utf16 = obj.AsSpan();
int max = Math.min(utf16.Length, numSamples);
for (int i = 0; i < max; i++) {
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint c = utf16[(i * modulus) % utf16.Length];
int c = utf16[(i * modulus) % utf16.Length];
if (i % 2 == 0) {
hash1 = ((hash1 << 5) + hash1) ^ c;
} else {
hash2 = ((hash2 << 5) + hash2) ^ c;
}
}
return hash1 + (hash2 * 1566083941);
}
}
}

View File

@@ -0,0 +1,49 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow.layouts;
public class SamplingUtf8StringComparer implements IEqualityComparer<Utf8String> {
public static final SamplingUtf8StringComparer Default = new SamplingUtf8StringComparer();
public final boolean equals(Utf8String x, Utf8String y) {
checkArgument(x != null);
checkArgument(y != null);
return x.Span.equals(y.Span);
}
public final int hashCode(Utf8String obj) {
checkArgument(obj != null);
// TODO: C# TO JAVA CONVERTER: There is no equivalent to an 'unchecked' block in Java:
unchecked
{
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint hash1 = 5381;
int hash1 = 5381;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint hash2 = hash1;
int hash2 = hash1;
final int numSamples = 4;
final int modulus = 13;
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: ReadOnlySpan<byte> utf8 = obj.Span.Span;
ReadOnlySpan<Byte> utf8 = obj.Span.Span;
int max = Math.min(utf8.Length, numSamples);
for (int i = 0; i < max; i++) {
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: uint c = utf8[(i * modulus) % utf8.Length];
int c = utf8[(i * modulus) % utf8.Length];
if (i % 2 == 0) {
hash1 = ((hash1 << 5) + hash1) ^ c;
} else {
hash2 = ((hash2 << 5) + hash2) ^ c;
}
}
return hash1 + (hash2 * 1566083941);
}
}
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow.schemas;
import Newtonsoft.Json.*;
public class StrictBooleanConverter extends JsonConverter {
@Override
public boolean getCanWrite() {
return false;
}
@Override
public boolean CanConvert(java.lang.Class objectType) {
return objectType == Boolean.class;
}
@Override
public Object ReadJson(JsonReader reader, java.lang.Class objectType, Object existingValue,
JsonSerializer serializer) {
switch (reader.TokenType) {
case JsonToken.Boolean:
return serializer.Deserialize(reader, objectType);
default:
throw new JsonSerializationException(String.format("Token \"%1$s\" of type %2$s was not a JSON bool",
reader.Value, reader.TokenType));
}
}
// TODO: C# TO JAVA CONVERTER: Java annotations will not correspond to .NET attributes:
//ORIGINAL LINE: [ExcludeFromCodeCoverage] public override void WriteJson(JsonWriter writer, object value,
// JsonSerializer serializer)
@Override
public void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) {
}
}

View File

@@ -0,0 +1,47 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package com.azure.data.cosmos.serialization.hybridrow.schemas;
import Newtonsoft.Json.*;
import java.math.BigInteger;
public class StrictIntegerConverter extends JsonConverter {
@Override
public boolean getCanWrite() {
return false;
}
@Override
public boolean CanConvert(java.lang.Class objectType) {
return StrictIntegerConverter.IsIntegerType(objectType);
}
@Override
public Object ReadJson(JsonReader reader, java.lang.Class objectType, Object existingValue,
JsonSerializer serializer) {
switch (reader.TokenType) {
case JsonToken.Integer:
return serializer.Deserialize(reader, objectType);
default:
throw new JsonSerializationException(String.format("Token \"%1$s\" of type %2$s was not a JSON " +
"integer", reader.Value, reader.TokenType));
}
}
// TODO: C# TO JAVA CONVERTER: Java annotations will not correspond to .NET attributes:
//ORIGINAL LINE: [ExcludeFromCodeCoverage] public override void WriteJson(JsonWriter writer, object value,
// JsonSerializer serializer)
@Override
public void WriteJson(JsonWriter writer, Object value, JsonSerializer serializer) {
}
private static boolean IsIntegerType(java.lang.Class type) {
//C# TO JAVA CONVERTER WARNING: Unsigned integer types have no direct equivalent in Java:
//ORIGINAL LINE: if (type == typeof(long) || type == typeof(ulong) || type == typeof(int) || type == typeof
// (uint) || type == typeof(short) || type == typeof(ushort) || type == typeof(byte) || type == typeof(sbyte)
// || type == typeof(BigInteger))
return type == Long.class || type == Long.class || type == Integer.class || type == Integer.class || type == Short.class || type == Short.class || type == Byte.class || type == Byte.class || type == BigInteger.class;
}
}