/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.table.factories;

import org.apache.flink.table.api.TableException;
import org.apache.flink.table.catalog.Catalog;
import org.apache.flink.table.catalog.CatalogTable;
import org.apache.flink.table.sinks.TableSink;
import org.apache.flink.table.sources.TableSource;

import java.util.Map;
import java.util.Optional;

/**
 * Utility for dealing with {@link TableFactory} using the {@link TableFactoryService}.
 */
public class TableFactoryUtil {

	/**
	 * Returns a table source matching the descriptor.
	 */
	@SuppressWarnings("unchecked")
	public static <T> TableSource<T> findAndCreateTableSource(TableSourceFactory.Context context) {
		try {
			return TableFactoryService
					.find(TableSourceFactory.class, context.getTable().toProperties())
					.createTableSource(context);
		} catch (Throwable t) {
			throw new TableException("findAndCreateTableSource failed.", t);
		}
	}

	/**
	 * Returns a table sink matching the context.
	 */
	@SuppressWarnings("unchecked")
	public static <T> TableSink<T> findAndCreateTableSink(TableSinkFactory.Context context) {
		try {
			return TableFactoryService
					.find(TableSinkFactory.class, context.getTable().toProperties())
					.createTableSink(context);
		} catch (Throwable t) {
			throw new TableException("findAndCreateTableSink failed.", t);
		}
	}

	/**
	 * Returns a table source matching the properties.
	 */
	@SuppressWarnings("unchecked")
	private static <T> TableSource<T> findAndCreateTableSource(Map<String, String> properties) {
		try {
			return TableFactoryService
				.find(TableSourceFactory.class, properties)
				.createTableSource(properties);
		} catch (Throwable t) {
			throw new TableException("findAndCreateTableSource failed.", t);
		}
	}

	@SuppressWarnings("unchecked")
	private static <T> TableSink<T> findAndCreateTableSink(Map<String, String> properties) {
		TableSink tableSink;
		try {
			tableSink = TableFactoryService
				.find(TableSinkFactory.class, properties)
				.createTableSink(properties);
		} catch (Throwable t) {
			throw new TableException("findAndCreateTableSink failed.", t);
		}

		return tableSink;
	}

	/**
	 * Creates a table sink for a {@link CatalogTable} using table factory associated with the catalog.
	 */
	public static Optional<TableSink> createTableSinkForCatalogTable(Catalog catalog, TableSinkFactory.Context context) {
		TableFactory tableFactory = catalog.getTableFactory().orElse(null);
		if (tableFactory instanceof TableSinkFactory) {
			return Optional.ofNullable(((TableSinkFactory) tableFactory).createTableSink(context));
		}
		return Optional.empty();
	}

}

@@ -1095,30 +1095,8 @@
 e;%0A%0A
-import java.util.Map;%0A
 impo
@@ -2109,809 +2109,8 @@
 %09%7D%0A%0A
-%09/**%0A%09 * Returns a table source matching the properties.%0A%09 */%0A%09@SuppressWarnings(%22unchecked%22)%0A%09private static %3CT%3E TableSource%3CT%3E findAndCreateTableSource(Map%3CString, String%3E properties) %7B%0A%09%09try %7B%0A%09%09%09return TableFactoryService%0A%09%09%09%09.find(TableSourceFactory.class, properties)%0A%09%09%09%09.createTableSource(properties);%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09throw new TableException(%22findAndCreateTableSource failed.%22, t);%0A%09%09%7D%0A%09%7D%0A%0A%09@SuppressWarnings(%22unchecked%22)%0A%09private static %3CT%3E TableSink%3CT%3E findAndCreateTableSink(Map%3CString, String%3E properties) %7B%0A%09%09TableSink tableSink;%0A%09%09try %7B%0A%09%09%09tableSink = TableFactoryService%0A%09%09%09%09.find(TableSinkFactory.class, properties)%0A%09%09%09%09.createTableSink(properties);%0A%09%09%7D catch (Throwable t) %7B%0A%09%09%09throw new TableException(%22findAndCreateTableSink failed.%22, t);%0A%09%09%7D%0A%0A%09%09return tableSink;%0A%09%7D%0A%0A
 %09/**

Clean TableFactoryUtil