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

org.eclipse.persistence.jpa.jpql.NumericResolver Maven / Gradle / Ivy

There is a newer version: 5.0.0-B07
Show newest version
/*******************************************************************************
 * Copyright (c) 2006, 2013 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.jpql;

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

/**
 * This {@link Resolver} is responsible to return the numeric type for a list of {@link Resolver
 * Resolvers}.
 * 

* The result of a CASE expression, COALESCE expression, NULLIF expression, or * arithmetic expression (+, -, *, /) is determined by applying the following rule to its operands. *

*

    *
  • If there is an operand of type Double or double, the result of the * operation is of type Double; *
  • otherwise, if there is an operand of type Float or float, the * result of the operation is of type Float; *
  • otherwise, if there is an operand of type BigDecimal, the result of the * operation is of type BigDecimal; *
  • otherwise, if there is an operand of type BigInteger, the result of the * operation is of type BigInteger, unless the operator is / (division), in which * case the numeric result type is not further defined; *
  • otherwise, if there is an operand of type Long or long, the result * of the operation is of type Long, unless the operator is / (division), in which * case the numeric result type is not further defined; *
  • otherwise, if there is an operand of integral type, the result of the operation is of type * Integer, unless the operator is / (division), in which case the numeric result * type is not further defined. *
* * @version 2.4 * @since 2.3 * @author Pascal Filion */ public class NumericResolver extends Resolver { /** * The {@link Resolver resolvers} used to calculate the numeric type. */ private final Collection resolvers; /** * Creates a new NumericResolver. * * @param parent The parent {@link Resolver}, which is never null * @param typeResolvers The {@link Resolver resolvers} used to calculate the numeric type */ public NumericResolver(Resolver parent, Collection typeResolvers) { super(parent); this.resolvers = typeResolvers; } /** * Creates a new NumericResolver. * * @param parent The parent {@link Resolver}, which is never null * @param resolver The {@link Resolver} used to calculate the numeric type */ public NumericResolver(Resolver parent, Resolver resolver) { this(parent, Collections.singleton(resolver)); } /** * {@inheritDoc} */ @Override public void accept(ResolverVisitor visitor) { } /** * {@inheritDoc} */ @Override protected IType buildType() { List types = new ArrayList(resolvers.size()); TypeHelper helper = getTypeHelper(); IType unresolvableType = helper.unknownType(); // Convert any primitive types into its Class type and any non-number into Object for (Resolver typeResolver : resolvers) { IType type = typeResolver.getType(); // Only a resolvable type will be added to the list if (type != unresolvableType) { // Non-numeric type cannot be added if (!helper.isNumericType(type)) { type = helper.objectType(); } types.add(type); } } if (types.isEmpty()) { return helper.unknownType(); } // Comparing the types will result in putting the // result at the beginning of the list Collections.sort(types, new NumericTypeComparator(helper)); return types.get(0); } /** * {@inheritDoc} */ @Override protected ITypeDeclaration buildTypeDeclaration() { return getType().getTypeDeclaration(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy