// Copyright 2014 The Bazel Authors. All rights reserved. // // 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.google.devtools.build.lib.syntax; import static java.util.stream.Collectors.joining; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Lists; import com.google.common.collect.Ordering; import com.google.devtools.build.lib.collect.nestedset.NestedSet; import com.google.devtools.build.lib.events.Event; import com.google.devtools.build.lib.events.Location; import com.google.devtools.build.lib.skylarkinterface.Param; import com.google.devtools.build.lib.skylarkinterface.SkylarkModule; import com.google.devtools.build.lib.skylarkinterface.SkylarkModuleCategory; import com.google.devtools.build.lib.skylarkinterface.SkylarkSignature; import com.google.devtools.build.lib.syntax.EvalUtils.ComparisonException; import com.google.devtools.build.lib.syntax.SkylarkList.MutableList; import com.google.devtools.build.lib.syntax.SkylarkList.Tuple; import com.google.devtools.build.lib.syntax.Type.ConversionException; import java.util.ArrayDeque; import java.util.ArrayList; import java.util.Iterator; import java.util.List; import java.util.NoSuchElementException; import java.util.Set; import java.util.TreeSet; import javax.annotation.Nullable; /** A helper class containing built in functions for the Skylark language. */ public class MethodLibrary { private MethodLibrary() {} @SkylarkSignature( name = "min", returnType = Object.class, doc = "Returns the smallest one of all given arguments. " + "If only one argument is provided, it must be a non-empty iterable. " + "It is an error if elements are not comparable (for example int with string). " + "
min(2, 5, 4) == 2\n" + "min([5, 6, 3]) == 3", extraPositionals = @Param(name = "args", type = SkylarkList.class, doc = "The elements to be checked."), useLocation = true, useEnvironment = true ) private static final BuiltinFunction min = new BuiltinFunction("min") { @SuppressWarnings("unused") // Accessed via Reflection. public Object invoke(SkylarkList> args, Location loc, Environment env) throws EvalException { try { return findExtreme(args, EvalUtils.SKYLARK_COMPARATOR.reverse(), loc, env); } catch (ComparisonException e) { throw new EvalException(loc, e); } } }; @SkylarkSignature( name = "max", returnType = Object.class, doc = "Returns the largest one of all given arguments. " + "If only one argument is provided, it must be a non-empty iterable." + "It is an error if elements are not comparable (for example int with string). " + "
max(2, 5, 4) == 5\n" + "max([5, 6, 3]) == 6", extraPositionals = @Param(name = "args", type = SkylarkList.class, doc = "The elements to be checked."), useLocation = true, useEnvironment = true ) private static final BuiltinFunction max = new BuiltinFunction("max") { @SuppressWarnings("unused") // Accessed via Reflection. public Object invoke(SkylarkList> args, Location loc, Environment env) throws EvalException { try { return findExtreme(args, EvalUtils.SKYLARK_COMPARATOR, loc, env); } catch (ComparisonException e) { throw new EvalException(loc, e); } } }; /** Returns the maximum element from this list, as determined by maxOrdering. */ private static Object findExtreme( SkylarkList> args, Ordering