From a691f021b4884e89db64a3aaac2a38d8e5e3d3fa Mon Sep 17 00:00:00 2001 From: Vinish Reddy Date: Tue, 31 Dec 2024 10:28:35 -0800 Subject: [PATCH] Refactor to findInstance --- .../catalog/ExternalCatalogConfigFactory.java | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/xtable-core/src/main/java/org/apache/xtable/catalog/ExternalCatalogConfigFactory.java b/xtable-core/src/main/java/org/apache/xtable/catalog/ExternalCatalogConfigFactory.java index 42ec6356..5a72463d 100644 --- a/xtable-core/src/main/java/org/apache/xtable/catalog/ExternalCatalogConfigFactory.java +++ b/xtable-core/src/main/java/org/apache/xtable/catalog/ExternalCatalogConfigFactory.java @@ -33,10 +33,14 @@ public class ExternalCatalogConfigFactory { public static ExternalCatalogConfig fromCatalogType( String catalogType, String catalogId, Map properties) { String catalogSyncClientImpl = - findImplClassName(CatalogSyncClient.class, catalogType, CatalogSyncClient::getCatalogType); + findInstance(CatalogSyncClient.class, catalogType, CatalogSyncClient::getCatalogType) + .getClass() + .getName(); String catalogConversionSourceImpl = - findImplClassName( - CatalogConversionSource.class, catalogType, CatalogConversionSource::getCatalogType); + findInstance( + CatalogConversionSource.class, catalogType, CatalogConversionSource::getCatalogType) + .getClass() + .getName(); return ExternalCatalogConfig.builder() .catalogType(catalogType) .catalogSyncClientImpl(catalogSyncClientImpl) @@ -46,13 +50,13 @@ public static ExternalCatalogConfig fromCatalogType( .build(); } - private static String findImplClassName( + private static T findInstance( Class serviceClass, String catalogType, Function catalogTypeExtractor) { ServiceLoader loader = ServiceLoader.load(serviceClass); for (T instance : loader) { String instanceCatalogType = catalogTypeExtractor.apply(instance); if (catalogType.equals(instanceCatalogType)) { - return instance.getClass().getName(); + return instance; } } throw new NotSupportedException("catalogType is not yet supported: " + catalogType);