Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/**
* 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.camel.util.component;
import java.lang.reflect.Array;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.camel.RuntimeCamelException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Helper class for working with {@link ApiMethod}.
*/
public final class ApiMethodHelper & ApiMethod> {
private static final Logger LOG = LoggerFactory.getLogger(ApiMethodHelper.class);
// maps method name to ApiMethod
private final Map> methodMap;
// maps method name to method arguments of the form Class type1, String name1, Class type2, String name2,...
private final Map> argumentsMap;
// maps argument name to argument type
private final Map> validArguments;
// maps aliases to actual method names
private final Map> aliasesMap;
// nullable args
private final List nullableArguments;
/**
* Create a helper to work with a {@link ApiMethod}, using optional method aliases.
* @param apiMethodEnum {@link ApiMethod} enumeration class
* @param aliases Aliases mapped to actual method names
* @param nullableArguments names of arguments that default to null value
*/
public ApiMethodHelper(Class apiMethodEnum, Map aliases, List nullableArguments) {
Map> tmpMethodMap = new HashMap<>();
Map> tmpArgumentsMap = new HashMap<>();
Map> tmpValidArguments = new HashMap<>();
Map> tmpAliasesMap = new HashMap<>();
// validate ApiMethod Enum
if (apiMethodEnum == null) {
throw new IllegalArgumentException("ApiMethod enumeration cannot be null");
}
if (nullableArguments != null && !nullableArguments.isEmpty()) {
this.nullableArguments = Collections.unmodifiableList(new ArrayList(nullableArguments));
} else {
this.nullableArguments = Collections.emptyList();
}
final Map aliasPatterns = new HashMap();
for (Map.Entry alias : aliases.entrySet()) {
if (alias.getKey() == null || alias.getValue() == null) {
throw new IllegalArgumentException("Alias pattern and replacement cannot be null");
}
aliasPatterns.put(Pattern.compile(alias.getKey()), alias.getValue());
}
LOG.debug("Processing {}", apiMethodEnum.getName());
final T[] methods = apiMethodEnum.getEnumConstants();
// load lookup maps
for (T method : methods) {
final String name = method.getName();
// add method name aliases
for (Map.Entry aliasEntry : aliasPatterns.entrySet()) {
final Matcher matcher = aliasEntry.getKey().matcher(name);
if (matcher.find()) {
// add method name alias
String alias = matcher.replaceAll(aliasEntry.getValue());
// convert first character to lowercase
assert alias.length() > 1;
final char firstChar = alias.charAt(0);
if (!Character.isLowerCase(firstChar)) {
final StringBuilder builder = new StringBuilder();
builder.append(Character.toLowerCase(firstChar)).append(alias.substring(1));
alias = builder.toString();
}
Set names = tmpAliasesMap.get(alias);
if (names == null) {
names = new HashSet();
tmpAliasesMap.put(alias, names);
}
names.add(name);
}
}
// map method name to Enum
List overloads = tmpMethodMap.get(name);
if (overloads == null) {
overloads = new ArrayList();
tmpMethodMap.put(method.getName(), overloads);
}
overloads.add(method);
// add arguments for this method
List