Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.
  */
 package com.facebook.presto.metadata;
 
 
 
 import java.util.List;
 import java.util.Map;
 
 import static com.facebook.presto.metadata.FunctionRegistry.canCoerce;
 import static com.facebook.presto.metadata.FunctionRegistry.getCommonSuperType;
 import static com.facebook.presto.metadata.FunctionRegistry.mangleOperatorName;
 import static com.facebook.presto.spi.type.TypeSignature.parseTypeSignature;
 import static com.facebook.presto.type.UnknownType.UNKNOWN;
 import static com.google.common.base.Preconditions.checkArgument;
 import static com.google.common.base.Preconditions.checkNotNull;
 
 public final class Signature
 {
     private final String name;
     private final List<TypeParametertypeParameters;
     private final TypeSignature returnType;
     private final List<TypeSignatureargumentTypes;
     private final boolean variableArity;
     private final boolean internal;
 
     @JsonCreator
     public Signature(
             @JsonProperty("name"String name,
             @JsonProperty("typeParameters"List<TypeParametertypeParameters,
             @JsonProperty("returnType"TypeSignature returnType,
             @JsonProperty("argumentTypes"List<TypeSignatureargumentTypes,
             @JsonProperty("variableArity"boolean variableArity,
             @JsonProperty("internal"boolean internal)
     {
         checkNotNull(name"name is null");
         checkNotNull(typeParameters"typeParameters is null");
 
         this. = name;
         this. = ImmutableList.copyOf(typeParameters);
         this. = checkNotNull(returnType"returnType is null");
         this. = ImmutableList.copyOf(checkNotNull(argumentTypes"argumentTypes is null"));
         this. = variableArity;
         this. = internal;
     }
 
     public Signature(String nameList<TypeParametertypeParametersString returnTypeList<StringargumentTypesboolean variableArityboolean internal)
     {
         this(nametypeParametersparseTypeSignature(returnType), Lists.transform(argumentTypes, TypeSignature::parseTypeSignature), variableArityinternal);
     }
 
     public Signature(String nameString returnTypeList<StringargumentTypes)
     {
         this(name, ImmutableList.<TypeParameter>of(), parseTypeSignature(returnType), Lists.transform(argumentTypes, TypeSignature::parseTypeSignature), falsefalse);
     }
 
     public Signature(String nameString returnTypeString... argumentTypes)
     {
         this(namereturnType, ImmutableList.copyOf(argumentTypes));
     }
 
     public Signature(String nameTypeSignature returnTypeList<TypeSignatureargumentTypes)
     {
         this(name, ImmutableList.<TypeParameter>of(), returnTypeargumentTypesfalsefalse);
     }
 
     public Signature(String nameTypeSignature returnTypeTypeSignature... argumentTypes)
     {
         this(namereturnType, ImmutableList.copyOf(argumentTypes));
     }
 
     public static Signature internalOperator(String nameTypeSignature returnTypeList<TypeSignatureargumentTypes)
     {
         return internalFunction(mangleOperatorName(name), returnTypeargumentTypes);
     }
 
    public static Signature internalOperator(String nameTypeSignature returnTypeTypeSignature... argumentTypes)
    {
        return internalFunction(mangleOperatorName(name), returnType, ImmutableList.copyOf(argumentTypes));
    }
    public static Signature internalFunction(String nameString returnTypeString... argumentTypes)
    {
        return internalFunction(namereturnType, ImmutableList.copyOf(argumentTypes));
    }
    public static Signature internalFunction(String nameString returnTypeList<StringargumentTypes)
    {
        return new Signature(name, ImmutableList.<TypeParameter>of(), returnTypeargumentTypesfalsetrue);
    }
    public static Signature internalFunction(String nameTypeSignature returnTypeTypeSignature... argumentTypes)
    {
        return internalFunction(namereturnType, ImmutableList.copyOf(argumentTypes));
    }
    public static Signature internalFunction(String nameTypeSignature returnTypeList<TypeSignatureargumentTypes)
    {
        return new Signature(name, ImmutableList.<TypeParameter>of(), returnTypeargumentTypesfalsetrue);
    }
    public String getName()
    {
        return ;
    }
    public TypeSignature getReturnType()
    {
        return ;
    }
    {
        return ;
    }
    public boolean isInternal()
    {
        return ;
    }
    public boolean isVariableArity()
    {
        return ;
    }
    {
        return ;
    }
    @Override
    public int hashCode()
    {
        return Objects.hash();
    }
    Signature withAlias(String name)
    {
        return new Signature(namegetReturnType(), getArgumentTypes(), );
    }
    @Override
    public boolean equals(Object obj)
    {
        if (this == obj) {
            return true;
        }
        if (obj == null || getClass() != obj.getClass()) {
            return false;
        }
        Signature other = (Signatureobj;
        return Objects.equals(this.other.name) &&
                Objects.equals(this.other.typeParameters) &&
                Objects.equals(this.other.returnType) &&
                Objects.equals(this.other.argumentTypes) &&
                Objects.equals(this.other.variableArity) &&
                Objects.equals(this.other.internal);
    }
    @Override
    public String toString()
    {
        return ( ? "%" : "") +  + (.isEmpty() ? "" : "<" + Joiner.on(",").join() + ">") + "(" + Joiner.on(",").join() + "):" + ;
    }
    @Nullable
    public Map<StringTypebindTypeParameters(Type returnTypeList<? extends Typetypesboolean allowCoercionTypeManager typeManager)
    {
        Map<StringTypeboundParameters = new HashMap<>();
        Map<StringTypeParameterunboundParameters = new HashMap<>();
        for (TypeParameter parameter : ) {
            unboundParameters.put(parameter.getName(), parameter);
        }
        if (!matchAndBind(boundParametersunboundParametersthis.returnTypeallowCoerciontypeManager)) {
            return null;
        }
        if (!matchArguments(boundParametersunboundParameterstypesallowCoerciontypeManager)) {
            return null;
        }
        return boundParameters;
    }
    @Nullable
    public Map<StringTypebindTypeParameters(List<? extends Typetypesboolean allowCoercionTypeManager typeManager)
    {
        Map<StringTypeboundParameters = new HashMap<>();
        Map<StringTypeParameterunboundParameters = new HashMap<>();
        for (TypeParameter parameter : ) {
            unboundParameters.put(parameter.getName(), parameter);
        }
        if (!matchArguments(boundParametersunboundParameterstypesallowCoerciontypeManager)) {
            return null;
        }
        return boundParameters;
    }
    private static boolean matchArguments(
            Map<StringTypeboundParameters,
            Map<StringTypeParameterunboundParameters,
            List<TypeSignatureargumentTypes,
            List<? extends Typetypes,
            boolean allowCoercion,
            boolean varArgs,
            TypeManager typeManager)
    {
        if (varArgs) {
            if (types.size() < argumentTypes.size() - 1) {
                return false;
            }
        }
        else {
            if (argumentTypes.size() != types.size()) {
                return false;
            }
        }
        // Bind the variable arity argument first, to make sure it's bound to the common super type
        if (varArgs && types.size() >= argumentTypes.size()) {
            Optional<TypesuperType = getCommonSuperType(types.subList(argumentTypes.size() - 1, types.size()));
            if (!superType.isPresent()) {
                return false;
            }
            if (!matchAndBind(boundParametersunboundParametersargumentTypes.get(argumentTypes.size() - 1), superType.get(), allowCoerciontypeManager)) {
                return false;
            }
        }
        for (int i = 0; i < types.size(); i++) {
            // Get the current argument signature, or the last one, if this is a varargs function
            TypeSignature typeSignature = argumentTypes.get(Math.min(iargumentTypes.size() - 1));
            Type type = types.get(i);
            if (!matchAndBind(boundParametersunboundParameterstypeSignaturetypeallowCoerciontypeManager)) {
                return false;
            }
        }
        return true;
    }
    private static boolean matchAndBind(Map<StringTypeboundParametersMap<StringTypeParameterunboundParametersTypeSignature signatureType typeboolean allowCoercionTypeManager typeManager)
    {
        // If this parameter is already bound, then match (with coercion)
        if (boundParameters.containsKey(signature.getBase())) {
            checkArgument(signature.getParameters().isEmpty(), "Unexpected parameteric type");
            if (allowCoercion) {
                return canCoerce(typeboundParameters.get(signature.getBase()));
            }
            else {
                return type.equals(boundParameters.get(signature.getBase()));
            }
        }
        // Recurse into component types
        if (!signature.getParameters().isEmpty()) {
            if (type.getTypeParameters().size() != signature.getParameters().size()) {
                return false;
            }
            for (int i = 0; i < signature.getParameters().size(); i++) {
                Type componentType = type.getTypeParameters().get(i);
                TypeSignature componentSignature = signature.getParameters().get(i);
                if (!matchAndBind(boundParametersunboundParameterscomponentSignaturecomponentTypeallowCoerciontypeManager)) {
                    return false;
                }
            }
        }
        if (type.equals() && allowCoercion) {
            // The unknown type can be coerced to any type, so don't bind the parameters, since nothing can be coerced to the unknown type
            return true;
        }
        // Bind parameter, if this is a free type parameter
        if (unboundParameters.containsKey(signature.getBase())) {
            TypeParameter typeParameter = unboundParameters.get(signature.getBase());
            if (!typeParameter.canBind(type)) {
                return false;
            }
            unboundParameters.remove(signature.getBase());
            boundParameters.put(signature.getBase(), type);
            return true;
        }
        // We've already checked all the components, so just match the base type
        if (!signature.getParameters().isEmpty()) {
            return type.getTypeSignature().getBase().equals(signature.getBase());
        }
        if (allowCoercion) {
            return canCoerce(typetypeManager.getType(parseTypeSignature(signature.getBase())));
        }
        else {
            return type.equals(typeManager.getType(parseTypeSignature(signature.getBase())));
        }
    }
    /*
     * similar to T extends MyClass<?...>, if Java supported varargs wildcards
     */
    public static TypeParameter withVariadicBound(String nameString variadicBound)
    {
        return new TypeParameter(namefalsefalsevariadicBound);
    }
    public static TypeParameter comparableWithVariadicBound(String nameString variadicBound)
    {
        return new TypeParameter(nametruefalsevariadicBound);
    }
    public static TypeParameter typeParameter(String name)
    {
        return new TypeParameter(namefalsefalsenull);
    }
    public static TypeParameter comparableTypeParameter(String name)
    {
        return new TypeParameter(nametruefalsenull);
    }
    public static TypeParameter orderableTypeParameter(String name)
    {
        return new TypeParameter(namefalsetruenull);
    }
New to GrepCode? Check out our FAQ X