MINOR: Use EnumMap/EnumSet if possible (#3919)

They are more efficient than HashMap/HashSet.

Reviewers: Ismael Juma <ismael@juma.me.uk>
This commit is contained in:
Koen De Groote 2017-12-29 13:39:18 +01:00 committed by Ismael Juma
parent a3aea3cf4d
commit 2bc780b959
3 changed files with 8 additions and 7 deletions

View File

@ -22,6 +22,7 @@ import java.math.BigDecimal;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -31,7 +32,7 @@ public class ConnectSchema implements Schema {
/** /**
* Maps Schema.Types to a list of Java classes that can be used to represent them. * Maps Schema.Types to a list of Java classes that can be used to represent them.
*/ */
private static final Map<Type, List<Class>> SCHEMA_TYPE_CLASSES = new HashMap<>(); private static final Map<Type, List<Class>> SCHEMA_TYPE_CLASSES = new EnumMap<>(Type.class);
/** /**
* Maps known logical types to a list of Java classes that can be used to represent them. * Maps known logical types to a list of Java classes that can be used to represent them.
*/ */

View File

@ -42,6 +42,7 @@ import java.math.BigDecimal;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.EnumMap;
import java.util.HashMap; import java.util.HashMap;
import java.util.Iterator; import java.util.Iterator;
import java.util.Map; import java.util.Map;
@ -55,7 +56,7 @@ public class JsonConverter implements Converter {
private static final String SCHEMAS_CACHE_SIZE_CONFIG = "schemas.cache.size"; private static final String SCHEMAS_CACHE_SIZE_CONFIG = "schemas.cache.size";
private static final int SCHEMAS_CACHE_SIZE_DEFAULT = 1000; private static final int SCHEMAS_CACHE_SIZE_DEFAULT = 1000;
private static final HashMap<Schema.Type, JsonToConnectTypeConverter> TO_CONNECT_CONVERTERS = new HashMap<>(); private static final Map<Schema.Type, JsonToConnectTypeConverter> TO_CONNECT_CONVERTERS = new EnumMap<>(Schema.Type.class);
static { static {
TO_CONNECT_CONVERTERS.put(Schema.Type.BOOLEAN, new JsonToConnectTypeConverter() { TO_CONNECT_CONVERTERS.put(Schema.Type.BOOLEAN, new JsonToConnectTypeConverter() {

View File

@ -32,9 +32,8 @@ import org.apache.kafka.connect.errors.DataException;
import org.apache.kafka.connect.transforms.util.SchemaUtil; import org.apache.kafka.connect.transforms.util.SchemaUtil;
import org.apache.kafka.connect.transforms.util.SimpleConfig; import org.apache.kafka.connect.transforms.util.SimpleConfig;
import java.util.Arrays; import java.util.EnumSet;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.Map; import java.util.Map;
@ -79,9 +78,9 @@ public abstract class Cast<R extends ConnectRecord<R>> implements Transformation
private static final String PURPOSE = "cast types"; private static final String PURPOSE = "cast types";
private static final Set<Schema.Type> SUPPORTED_CAST_TYPES = new HashSet<>( private static final Set<Schema.Type> SUPPORTED_CAST_TYPES = EnumSet.of(
Arrays.asList(Schema.Type.INT8, Schema.Type.INT16, Schema.Type.INT32, Schema.Type.INT64, Schema.Type.INT8, Schema.Type.INT16, Schema.Type.INT32, Schema.Type.INT64,
Schema.Type.FLOAT32, Schema.Type.FLOAT64, Schema.Type.BOOLEAN, Schema.Type.STRING) Schema.Type.FLOAT32, Schema.Type.FLOAT64, Schema.Type.BOOLEAN, Schema.Type.STRING
); );
// As a special case for casting the entire value (e.g. the incoming key is a int64 but you know it could be an // As a special case for casting the entire value (e.g. the incoming key is a int64 but you know it could be an