001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.camel.util.component;
018
019import java.util.Collections;
020import java.util.Map;
021import java.util.Set;
022import java.util.stream.Collectors;
023
024/**
025 * Base class for a collection of ApiMethods. Meant to be extended by Components to create the api name map.
026 */
027public abstract class ApiCollection<E extends Enum<E> & ApiName, T> {
028
029    private Map<E, ApiMethodHelper<? extends ApiMethod>> apiHelpers = Collections.emptyMap();
030    private Map<Class<? extends ApiMethod>, E> apiMethods = Collections.emptyMap();
031    private Set<String> apiNames = Collections.emptySet();
032
033    public final Map<E, ApiMethodHelper<? extends ApiMethod>> getApiHelpers() {
034        return apiHelpers;
035    }
036
037    public final Map<Class<? extends ApiMethod>, E> getApiMethods() {
038        return apiMethods;
039    }
040
041    /**
042     * Returns a {@link ApiMethodHelper} for a particular API.
043     * @param apiName name of the API
044     * @return helper class to work with {@link ApiMethod}
045     */
046    public final ApiMethodHelper<? extends ApiMethod> getHelper(E apiName) {
047        return apiHelpers.get(apiName);
048    }
049
050    /**
051     * Returns a list of API name strings.
052     * @return list of API names.
053     */
054    public final Set<String> getApiNames() {
055        return apiNames;
056    }
057
058    public final E getApiName(Class<? extends ApiMethod> apiMethod) {
059        return apiMethods.get(apiMethod);
060    }
061
062    /**
063     * Creates an endpoint configuration for a particular API
064     * @param apiName name of the API.
065     * @return Endpoint configuration object for the API.
066     */
067    public abstract T getEndpointConfiguration(E apiName);
068
069    protected final void setApiHelpers(Map<E, ApiMethodHelper<? extends ApiMethod>> apiHelpers) {
070        this.apiHelpers = Collections.unmodifiableMap(apiHelpers);
071
072        this.apiNames = Collections.unmodifiableSet(
073            apiHelpers.keySet()
074                .stream()
075                .map(api -> api.getName())
076                .collect(Collectors.toSet())
077        );
078    }
079
080    protected final void setApiMethods(Map<Class<? extends ApiMethod>, E> apiMethods) {
081        this.apiMethods = Collections.unmodifiableMap(apiMethods);
082    }
083}