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

refactor(java): replace Guava's string utility methods with own implementation #1624

Merged
merged 2 commits into from
May 13, 2024
Merged
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
Expand Up @@ -19,16 +19,22 @@

package org.apache.fury.util;

import com.google.common.io.BaseEncoding;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

public class StringUtils {
private static final char[] BASE16_CHARS2 = {
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};

/** Converts a bytes array into a hexadecimal string. */
public static String encodeHexString(final byte[] data) {
return BaseEncoding.base16().lowerCase().encode(data);
StringBuilder result = new StringBuilder(data.length * 2);
for (byte b : data) {
result.append(BASE16_CHARS2[(b >>> 4) & 0xF]).append(BASE16_CHARS2[b & 0xF]);
}
return result.toString();
}

/** Format a string template by replacing all `${xxx}` into provided values. */
Expand Down Expand Up @@ -192,4 +198,55 @@ public static String repeat(String str, int numRepeat) {
}
return builder.toString();
}

// example: "variable_name" -> "variableName"
public static String lowerUnderscoreToLowerCamelCase(String lowerUnderscore) {
StringBuilder builder = new StringBuilder();
int length = lowerUnderscore.length();

int index;
int fromIndex = 0;
while ((index = lowerUnderscore.indexOf('_', fromIndex)) != -1) {
builder.append(lowerUnderscore, fromIndex, index);

if (length >= index + 1) {
char symbol = lowerUnderscore.charAt(index + 1);
if (symbol >= 'a' && symbol <= 'z') {
builder.append(Character.toUpperCase(symbol));
fromIndex = index + 2;
continue;
}
}

fromIndex = index + 1;
}

if (fromIndex < length) {
builder.append(lowerUnderscore, fromIndex, length);
}

return builder.toString();
}

// example: "variableName" -> "variable_name"
public static String lowerCamelToLowerUnderscore(String lowerCamel) {
StringBuilder builder = new StringBuilder();
int length = lowerCamel.length();

int fromIndex = 0;

for (int i = 0; i < length; i++) {
char symbol = lowerCamel.charAt(i);
if (symbol >= 'A' && symbol <= 'Z') {
builder.append(lowerCamel, fromIndex, i).append('_').append(Character.toLowerCase(symbol));
fromIndex = i + 1;
}
}

if (fromIndex < length) {
builder.append(lowerCamel, fromIndex, length);
}

return builder.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,29 @@ public void testIsBlank() {
assertTrue(StringUtils.isBlank(" "));
assertTrue(StringUtils.isBlank(null));
}

@Test
public void testLowerUnderscoreToLowerCamelCase() {
assertEquals(StringUtils.lowerUnderscoreToLowerCamelCase("some_variable"), "someVariable");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_long_variable"), "someLongVariable");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_123variable"), "some123variable");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_variable123"), "someVariable123");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_variable123"), "someVariable123");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_123_variable"), "some123Variable");
assertEquals(
StringUtils.lowerUnderscoreToLowerCamelCase("some_variable_123"), "someVariable123");
}

@Test
public void testLowerCamelToLowerUnderscore() {
assertEquals(StringUtils.lowerCamelToLowerUnderscore("someVariable"), "some_variable");
assertEquals(StringUtils.lowerCamelToLowerUnderscore("someLongVariable"), "some_long_variable");
assertEquals(StringUtils.lowerCamelToLowerUnderscore("some123variable"), "some123variable");
assertEquals(StringUtils.lowerCamelToLowerUnderscore("someVariable123"), "some_variable123");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.apache.fury.type.TypeUtils.CLASS_TYPE;
import static org.apache.fury.type.TypeUtils.getRawType;

import com.google.common.base.CaseFormat;
import java.lang.reflect.Modifier;
import java.util.SortedMap;
import org.apache.arrow.vector.types.pojo.Field;
Expand Down Expand Up @@ -218,7 +217,7 @@ public Expression buildDecodeExpression() {
}

private Descriptor getDescriptorByFieldName(String fieldName) {
String name = CaseFormat.LOWER_UNDERSCORE.to(CaseFormat.LOWER_CAMEL, fieldName);
String name = StringUtils.lowerUnderscoreToLowerCamelCase(fieldName);
return descriptorsMap.get(name);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.apache.fury.format.type.DataTypes.field;
import static org.apache.fury.type.TypeUtils.getRawType;

import com.google.common.base.CaseFormat;
import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.List;
Expand All @@ -43,6 +42,7 @@
import org.apache.fury.type.TypeUtils;
import org.apache.fury.util.DecimalUtils;
import org.apache.fury.util.Preconditions;
import org.apache.fury.util.StringUtils;

/** Arrow related type inference. */
@SuppressWarnings("UnstableApiUsage")
Expand Down Expand Up @@ -226,9 +226,7 @@ private static Field inferField(
descriptor -> {
LinkedHashSet<Class<?>> newSeenTypeSet = new LinkedHashSet<>(seenTypeSet);
newSeenTypeSet.add(rawType);
String n =
CaseFormat.LOWER_CAMEL.to(
CaseFormat.LOWER_UNDERSCORE, descriptor.getName());
String n = StringUtils.lowerCamelToLowerUnderscore(descriptor.getName());
return inferField(n, descriptor.getTypeRef(), newSeenTypeSet);
})
.collect(Collectors.toList());
Expand Down
Loading