org.osgi.util.converter.ConverterBuilderImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of aem-sdk-api Show documentation
Show all versions of aem-sdk-api Show documentation
The Adobe Experience Manager SDK
/*******************************************************************************
* Copyright (c) Contributors to the Eclipse Foundation
*
* Licensed 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.
*
* SPDX-License-Identifier: Apache-2.0
*******************************************************************************/
package org.osgi.util.converter;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.lang.reflect.WildcardType;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author $Id: 2d1145de942d2b638253bf7fc0ff735290b0b4b3 $
*/
class ConverterBuilderImpl implements ConverterBuilder {
private final InternalConverter converter;
private final Map> rules = new HashMap<>();
private final List catchAllRules = new ArrayList<>();
private final List errorHandlers = new ArrayList<>();
public ConverterBuilderImpl(InternalConverter c) {
this.converter = c;
}
@Override
public InternalConverter build() {
return new CustomConverterImpl(converter, rules, catchAllRules,
errorHandlers);
}
@Override
public ConverterBuilder errorHandler(ConverterFunction func) {
errorHandlers.add(func);
return this;
}
@Override
public ConverterBuilder rule(ConverterFunction func) {
catchAllRules.add(func);
return this;
}
@Override
public ConverterBuilder rule(Type t, ConverterFunction func) {
getRulesList(t).add(func);
return this;
}
@Override
public ConverterBuilder rule(TargetRule rule) {
Type type = rule.getTargetType();
getRulesList(type).add(rule.getFunction());
if (type instanceof ParameterizedType) {
ParameterizedType pt = (ParameterizedType) type;
boolean containsWildCard = false;
for (Type t : pt.getActualTypeArguments()) {
if (t instanceof WildcardType) {
containsWildCard = true;
break;
}
}
// If the parameterized type is a wildcard (e.g. '?') then register
// also the raw
// type for the rule. I.e Class> will also be registered under
// bare Class.
if (containsWildCard)
getRulesList(pt.getRawType()).add(rule.getFunction());
}
return this;
}
private List getRulesList(Type type) {
List l = rules.get(type);
if (l == null) {
l = new ArrayList<>();
rules.put(type, l);
}
return l;
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy