All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.osgi.util.converter.Rule Maven / Gradle / Ivy

There is a newer version: 2024.11.18598.20241113T125352Z-241000
Show newest version
/*******************************************************************************
 * 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 org.osgi.util.function.Function;

/**
 * A rule implementation that works by capturing the type arguments via
 * subclassing. The rule supports specifying both from and to
 * types. Filtering on the from by the {@code Rule} implementation.
 * Filtering on the to is done by the converter customization
 * mechanism.
 *
 * @author $Id: 5f1dce9259e3bfad58df897f326493a56ae47abc $
 * @param  The type to convert from.
 * @param  The type to convert to.
 */
public abstract class Rule implements TargetRule {
	private final ConverterFunction function;

	/**
	 * Create an instance with a conversion function.
	 * 
	 * @param func The conversion function to use.
	 */
	public Rule(Function func) {
		function = getGenericFunction(func);
	}

	private ConverterFunction getGenericFunction(final Function func) {
		return new ConverterFunction() {
			@Override
			@SuppressWarnings("unchecked")
			public Object apply(Object obj, Type targetType) throws Exception {
				Rule< ? , ? > r = Rule.this;
				Type type = ((ParameterizedType) r.getClass()
						.getGenericSuperclass()).getActualTypeArguments()[0];

				if (type instanceof ParameterizedType) {
					type = ((ParameterizedType) type).getRawType();
				}

				Class< ? > cls = null;
				if (type instanceof Class) {
					cls = ((Class< ? >) type);
				} else {
					return ConverterFunction.CANNOT_HANDLE;
				}

				if (cls.isInstance(obj)) {
					return func.apply((F) obj);
				}
				return ConverterFunction.CANNOT_HANDLE;
			}
		};
	}

	@Override
	public ConverterFunction getFunction() {
		return function;
	}

	@Override
	public Type getTargetType() {
		Type type = ((ParameterizedType) getClass().getGenericSuperclass())
				.getActualTypeArguments()[1];
		return type;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy