Replace Guava Enum.getIfPresent with builtin version. (#4659)

* Replace Guava Enum.getIfPresent with builtin version.

This is useful for running in Hadoop environments that use Guava 11. Some
code is also simplified.

* Code review
This commit is contained in:
Gian Merlino 2017-08-09 17:20:00 -07:00 committed by GitHub
parent fe3421032b
commit 7c89e12ca9
5 changed files with 49 additions and 6 deletions

View File

@ -110,7 +110,11 @@
<property name="illegalPattern" value="true"/>
<property name="message" value="Use Comparators.naturalNullsFirst() instead of Ordering.natural().nullsFirst()"/>
</module>
<module name="Regexp">
<property name="format" value="Enums\.getIfPresent\("/>
<property name="illegalPattern" value="true"/>
<property name="message" value="Use GuavaUtils.getEnumIfPresent() instead of Enums.getIfPresent()"/>
</module>
<module name="Regexp">
<property name="format" value="(Byte|Character|Short|Integer|Long|Float|Double)\.TYPE"/>
<property name="illegalPattern" value="true"/>

View File

@ -21,6 +21,7 @@ package io.druid.common.guava;
import com.google.common.base.Charsets;
import com.google.common.base.Function;
import com.google.common.base.Preconditions;
import com.google.common.base.Strings;
import com.google.common.collect.Iterables;
import com.google.common.io.CharStreams;
@ -109,4 +110,26 @@ public class GuavaUtils
? null
: Longs.tryParse(string.charAt(0) == '+' ? string.substring(1) : string);
}
/**
* Like Guava's Enums.getIfPresent, with some differences.
* <ul>
* <li>Returns nullable rather than Optional</li>
* <li>Does not require Guava 12</li>
* </ul>
*/
@Nullable
public static <T extends Enum<T>> T getEnumIfPresent(final Class<T> enumClass, final String value)
{
Preconditions.checkNotNull(enumClass, "enumClass");
Preconditions.checkNotNull(value, "value");
for (T enumValue : enumClass.getEnumConstants()) {
if (enumValue.name().equals(value)) {
return enumValue;
}
}
return null;
}
}

View File

@ -25,8 +25,15 @@ import org.junit.Test;
public class GuavaUtilsTest
{
enum MyEnum
{
ONE,
TWO,
BUCKLE_MY_SHOE
}
@Test
public void testParsLong()
public void testParseLong()
{
Assert.assertNull(Longs.tryParse("+100"));
Assert.assertNull(GuavaUtils.tryParseLong(""));
@ -37,4 +44,13 @@ public class GuavaUtilsTest
Assert.assertEquals((Object) Long.parseLong("-100"), GuavaUtils.tryParseLong("-100"));
Assert.assertNotEquals(new Long(100), GuavaUtils.tryParseLong("+101"));
}
@Test
public void testGetEnumIfPresent()
{
Assert.assertEquals(MyEnum.ONE, GuavaUtils.getEnumIfPresent(MyEnum.class, "ONE"));
Assert.assertEquals(MyEnum.TWO, GuavaUtils.getEnumIfPresent(MyEnum.class, "TWO"));
Assert.assertEquals(MyEnum.BUCKLE_MY_SHOE, GuavaUtils.getEnumIfPresent(MyEnum.class, "BUCKLE_MY_SHOE"));
Assert.assertEquals(null, GuavaUtils.getEnumIfPresent(MyEnum.class, "buckle_my_shoe"));
}
}

View File

@ -19,11 +19,11 @@
package io.druid.query.groupby;
import com.google.common.base.Enums;
import com.google.common.base.Function;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
import io.druid.collections.NonBlockingPool;
import io.druid.common.guava.GuavaUtils;
import io.druid.data.input.MapBasedInputRow;
import io.druid.data.input.MapBasedRow;
import io.druid.data.input.Row;
@ -245,7 +245,7 @@ public class GroupByQueryHelper
final String typeName = aggregatorFactory.getTypeName();
final ValueType valueType;
if (typeName != null) {
valueType = Enums.getIfPresent(ValueType.class, StringUtils.toUpperCase(typeName)).orNull();
valueType = GuavaUtils.getEnumIfPresent(ValueType.class, StringUtils.toUpperCase(typeName));
} else {
valueType = null;
}

View File

@ -20,7 +20,6 @@
package io.druid.segment.incremental;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.base.Enums;
import com.google.common.base.Function;
import com.google.common.base.Supplier;
import com.google.common.collect.ImmutableList;
@ -31,6 +30,7 @@ import com.google.common.collect.Maps;
import com.google.common.primitives.Ints;
import com.google.common.primitives.Longs;
import io.druid.collections.NonBlockingPool;
import io.druid.common.guava.GuavaUtils;
import io.druid.data.input.InputRow;
import io.druid.data.input.MapBasedRow;
import io.druid.data.input.Row;
@ -146,7 +146,7 @@ public abstract class IncrementalIndex<AggregatorType> implements Iterable<Row>,
final ObjectColumnSelector rawColumnSelector = baseSelectorFactory.makeObjectColumnSelector(column);
if ((Enums.getIfPresent(ValueType.class, StringUtils.toUpperCase(typeName)).isPresent() && !typeName.equalsIgnoreCase(ValueType.COMPLEX.name()))
if ((GuavaUtils.getEnumIfPresent(ValueType.class, StringUtils.toUpperCase(typeName)) != null && !typeName.equalsIgnoreCase(ValueType.COMPLEX.name()))
|| !deserializeComplexMetrics) {
return rawColumnSelector;
} else {