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

org.eclipse.persistence.jpa.internal.jpql.FuncResolver Maven / Gradle / Ivy

/*******************************************************************************
 * Copyright (c) 2006, 2012 Oracle and/or its affiliates. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 and Eclipse Distribution License v. 1.0
 * which accompanies this distribution.
 * The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
 * and the Eclipse Distribution License is available at
 * http://www.eclipse.org/org/documents/edl-v10.php.
 *
 * Contributors:
 *     Oracle - initial API and implementation
 *
 ******************************************************************************/
package org.eclipse.persistence.jpa.internal.jpql;

import java.util.Collection;
import org.eclipse.persistence.jpa.jpql.spi.IType;
import org.eclipse.persistence.jpa.jpql.spi.ITypeDeclaration;

/**
 * This {@link Resolver} is responsible to calculate the type of a FUNC function by
 * calculating the type of each parameters.
 *
 * @version 2.3
 * @since 2.3
 * @author Pascal Filion
 */
final class FuncResolver extends Resolver {

	/**
	 * The collection of {@link Resolver Resolvers} that will be used to
	 * calculate the actual type.
	 */
	private final Collection resolvers;

	/**
	 * Creates a new FuncResolver.
	 *
	 * @param parent The parent {@link Resolver}, which is never null
	 * @param resolvers The collection of {@link Resolver Resolvers} that will be used to
	 * calculate the actual type
	 */
	FuncResolver(Resolver parent, Collection resolvers) {
		super(parent);
		this.resolvers = resolvers;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void accept(ResolverVisitor visitor) {
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	IType buildType() {

		IType stringType   = null;
		IType numericType  = null;
		IType dateTimeType = null;
		IType numericClass = getTypeHelper().numberType();
		IType dateClass    = getTypeHelper().dateType();

		for (Resolver resolver : resolvers) {
			IType parameterType = resolver.getType();

			// String parameter type
			if (getTypeHelper().isStringType(parameterType)) {
				stringType = parameterType;
			}
			// Numeric parameter type
			else if (parameterType.isAssignableTo(numericClass)) {
				numericType = numericClass;
			}
			// Date/Time parameter type
			else if (parameterType.isAssignableTo(dateClass)) {
				dateTimeType = dateClass;
			}
		}

		// String type
		if ((stringType != null) && (numericType == null) && (dateTimeType == null)) {
			return stringType;
		}

		// Numeric type
		if ((stringType == null) && (numericType != null) && (dateTimeType == null)) {
			return numericType;
		}

		// Date/Time type
		if ((stringType == null) && (numericType == null) && (dateTimeType != null)) {
			return dateTimeType;
		}

		// Unknown type
		return getTypeHelper().objectType();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	ITypeDeclaration buildTypeDeclaration() {
		return getType().getTypeDeclaration();
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy