Skip to content

Commit

Permalink
Statically init instances of OpenSearchDataType.
Browse files Browse the repository at this point in the history
Signed-off-by: Yury-Fridlyand <[email protected]>
  • Loading branch information
Yury-Fridlyand committed Feb 17, 2023
1 parent b968514 commit be7e9c7
Showing 1 changed file with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.function.BiConsumer;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import org.apache.commons.lang3.EnumUtils;
import org.opensearch.sql.data.type.ExprCoreType;
import org.opensearch.sql.data.type.ExprType;

Expand Down Expand Up @@ -88,6 +89,14 @@ public ExprType getExprType() {
*/
private static final Map<String, OpenSearchDataType> instances = new HashMap<>();

static {
EnumUtils.getEnumList(MappingType.class).stream()
.filter(t -> t != MappingType.Invalid).forEach(t ->
instances.put(t.toString(), OpenSearchDataType.of(t)));
EnumUtils.getEnumList(ExprCoreType.class).forEach(t ->
instances.put(t.toString(), OpenSearchDataType.of(t)));
}

/**
* A constructor function which builds proper `OpenSearchDataType` for given mapping `Type`.
* @param mappingType A mapping type.
Expand All @@ -112,7 +121,6 @@ public static OpenSearchDataType of(MappingType mappingType) {
}
res = new OpenSearchDataType(mappingType);
res.exprCoreType = exprCoreType;
instances.put(mappingType.toString(), res);
return res;
}

Expand Down Expand Up @@ -154,9 +162,7 @@ public static OpenSearchDataType of(ExprType type) {
if (res != null) {
return res;
}
res = new OpenSearchDataType((ExprCoreType) type);
instances.put(type.toString(), res);
return res;
return new OpenSearchDataType((ExprCoreType) type);
}

protected OpenSearchDataType(ExprCoreType type) {
Expand Down

0 comments on commit be7e9c7

Please sign in to comment.