Example usage for com.google.common.collect ImmutableSortedMap naturalOrder

List of usage examples for com.google.common.collect ImmutableSortedMap naturalOrder

Introduction

In this page you can find the example usage for com.google.common.collect ImmutableSortedMap naturalOrder.

Prototype

public static <K extends Comparable<?>, V> Builder<K, V> naturalOrder() 

Source Link

Usage

From source file:org.pircbotx.Configuration.java

/**
 * Use {@link Configuration.Builder#buildConfiguration() }.
 *
 * @param builder//from   w w w  .  j  a va2s . c o m
 * @see Configuration.Builder#buildConfiguration()
 */
protected Configuration(Builder builder) {
    //Check for basics
    if (builder.isWebIrcEnabled()) {
        checkNotNull(builder.getWebIrcAddress(), "Must specify WEBIRC address if enabled");
        checkArgument(StringUtils.isNotBlank(builder.getWebIrcHostname()),
                "Must specify WEBIRC hostname if enabled");
        checkArgument(StringUtils.isNotBlank(builder.getWebIrcUsername()),
                "Must specify WEBIRC username if enabled");
        checkArgument(StringUtils.isNotBlank(builder.getWebIrcPassword()),
                "Must specify WEBIRC password if enabled");
    }
    checkArgument(StringUtils.isNotBlank(builder.getName()), "Must specify name");
    checkArgument(StringUtils.isNotBlank(builder.getLogin()), "Must specify login");
    checkArgument(StringUtils.isNotBlank(builder.getVersion()), "Must specify version");
    checkArgument(StringUtils.isNotBlank(builder.getFinger()), "Must specify finger");
    checkArgument(StringUtils.isNotBlank(builder.getRealName()), "Must specify realName");
    checkArgument(StringUtils.isNotBlank(builder.getChannelPrefixes()), "Must specify channel prefixes");
    checkNotNull(StringUtils.isNotBlank(builder.getUserLevelPrefixes()),
            "Channel mode message prefixes cannot be null");
    checkNotNull(builder.getDccPorts(), "DCC ports list cannot be null");
    checkArgument(builder.getDccAcceptTimeout() > 0, "dccAcceptTimeout must be positive");
    checkArgument(builder.getDccResumeAcceptTimeout() > 0, "dccResumeAcceptTimeout must be positive");
    checkArgument(builder.getDccTransferBufferSize() > 0, "dccTransferBufferSize must be positive");
    checkNotNull(builder.getServers(), "Servers list cannot be null");
    checkArgument(!builder.getServers().isEmpty(), "Must specify servers to connect to");
    for (ServerEntry serverEntry : builder.getServers()) {
        checkArgument(StringUtils.isNotBlank(serverEntry.getHostname()), "Must specify server hostname");
        checkArgument(serverEntry.getPort() > 0 && serverEntry.getPort() <= 65535,
                "Port must be between 1 and 65535");
    }
    checkNotNull(builder.getSocketFactory(), "Socket factory cannot be null");
    checkNotNull(builder.getEncoding(), "Encoding cannot be null");
    checkNotNull(builder.getLocale(), "Locale cannot be null");
    checkArgument(builder.getSocketTimeout() > 0, "Socket timeout must greater than 0");
    checkArgument(builder.getMaxLineLength() > 0, "Max line length must be positive");
    checkArgument(builder.getMessageDelay() >= 0, "Message delay must be positive");
    checkNotNull(builder.getAutoJoinChannels(), "Auto join channels map cannot be null");
    for (Map.Entry<String, String> curEntry : builder.getAutoJoinChannels().entrySet())
        if (StringUtils.isBlank(curEntry.getKey()))
            throw new RuntimeException("Channel must not be blank");
    if (builder.getNickservPassword() != null)
        checkArgument(StringUtils.isNotBlank(builder.getNickservPassword()),
                "Nickserv password cannot be empty");
    checkArgument(StringUtils.isNotBlank(builder.getNickservOnSuccess()),
            "Nickserv on success cannot be blank");
    checkArgument(StringUtils.isNotBlank(builder.getNickservNick()), "Nickserv nick cannot be blank");
    checkArgument(builder.getAutoReconnectAttempts() > 0, "setAutoReconnectAttempts must be greater than 0");
    checkArgument(builder.getAutoReconnectDelay() >= 0, "setAutoReconnectDelay must be positive or 0");
    checkNotNull(builder.getListenerManager(), "Must specify listener manager");
    checkNotNull(builder.getCapHandlers(), "Cap handlers list cannot be null");
    checkNotNull(builder.getChannelModeHandlers(), "Channel mode handlers list cannot be null");
    checkNotNull(builder.getBotFactory(), "Must specify bot factory");

    this.webIrcEnabled = builder.isWebIrcEnabled();
    this.webIrcUsername = builder.getWebIrcUsername();
    this.webIrcHostname = builder.getWebIrcHostname();
    this.webIrcAddress = builder.getWebIrcAddress();
    this.webIrcPassword = builder.getWebIrcPassword();
    this.name = builder.getName();
    this.login = builder.getLogin();
    this.version = builder.getVersion();
    this.finger = builder.getFinger();
    this.realName = builder.getRealName();
    this.channelPrefixes = builder.getChannelPrefixes().trim();
    this.userLevelPrefixes = builder.getUserLevelPrefixes().trim();
    this.snapshotsEnabled = builder.isSnapshotsEnabled();
    this.dccFilenameQuotes = builder.isDccFilenameQuotes();
    this.dccPorts = ImmutableList.copyOf(builder.getDccPorts());
    this.dccLocalAddress = builder.getDccLocalAddress();
    this.dccAcceptTimeout = builder.getDccAcceptTimeout();
    this.dccResumeAcceptTimeout = builder.getDccResumeAcceptTimeout();
    this.dccTransferBufferSize = builder.getDccTransferBufferSize();
    this.dccPassiveRequest = builder.isDccPassiveRequest();
    this.servers = ImmutableList.copyOf(builder.getServers());
    this.serverPassword = builder.getServerPassword();
    this.socketFactory = builder.getSocketFactory();
    this.localAddress = builder.getLocalAddress();
    this.encoding = builder.getEncoding();
    this.locale = builder.getLocale();
    this.socketTimeout = builder.getSocketTimeout();
    this.maxLineLength = builder.getMaxLineLength();
    this.autoSplitMessage = builder.isAutoSplitMessage();
    this.autoNickChange = builder.isAutoNickChange();
    this.messageDelay = builder.getMessageDelay();
    this.identServerEnabled = builder.isIdentServerEnabled();
    this.nickservPassword = builder.getNickservPassword();
    this.nickservOnSuccess = builder.getNickservOnSuccess();
    this.nickservNick = builder.getNickservNick();
    this.nickservDelayJoin = builder.isNickservDelayJoin();
    this.autoReconnect = builder.isAutoReconnect();
    this.autoReconnectDelay = builder.getAutoReconnectDelay();
    this.autoReconnectAttempts = builder.getAutoReconnectAttempts();
    this.listenerManager = builder.getListenerManager();
    this.autoJoinChannels = ImmutableMap.copyOf(builder.getAutoJoinChannels());
    this.capEnabled = builder.isCapEnabled();
    this.capHandlers = ImmutableList.copyOf(builder.getCapHandlers());
    ImmutableSortedMap.Builder<Character, ChannelModeHandler> channelModeHandlersBuilder = ImmutableSortedMap
            .naturalOrder();
    for (ChannelModeHandler curHandler : builder.getChannelModeHandlers())
        channelModeHandlersBuilder.put(curHandler.getMode(), curHandler);
    this.channelModeHandlers = channelModeHandlersBuilder.build();
    this.shutdownHookEnabled = builder.isShutdownHookEnabled();
    this.botFactory = builder.getBotFactory();
}

From source file:org.openqa.selenium.firefox.FirefoxOptions.java

@Override
public Map<String, ?> asMap() {
    TreeMap<String, Object> toReturn = new TreeMap<>();
    toReturn.putAll(super.asMap());

    ImmutableSortedMap.Builder<String, Object> w3cOptions = ImmutableSortedMap.naturalOrder();
    w3cOptions.put("args", args);

    if (binary != null) {
        w3cOptions.put("binary", binary.asPath());
    }// w  w w . j ava 2  s.c o  m

    if (logLevel != null) {
        w3cOptions.put("log", ImmutableMap.of("level", logLevel));
    }

    if (profile != null) {
        for (Map.Entry<String, Boolean> pref : booleanPrefs.entrySet()) {
            profile.setPreference(pref.getKey(), pref.getValue());
        }
        for (Map.Entry<String, Integer> pref : intPrefs.entrySet()) {
            profile.setPreference(pref.getKey(), pref.getValue());
        }
        for (Map.Entry<String, String> pref : stringPrefs.entrySet()) {
            profile.setPreference(pref.getKey(), pref.getValue());
        }
        try {
            w3cOptions.put("profile", profile.toJson());
        } catch (IOException e) {
            throw new WebDriverException(e);
        }
    } else {
        ImmutableMap.Builder<String, Object> allPrefs = ImmutableMap.builder();
        allPrefs.putAll(booleanPrefs);
        allPrefs.putAll(intPrefs);
        allPrefs.putAll(stringPrefs);
        w3cOptions.put("prefs", allPrefs.build());
    }

    toReturn.put(FIREFOX_OPTIONS, w3cOptions.build());

    return toReturn;
}

From source file:gobblin.metrics.InnerMetricContext.java

@SuppressWarnings("unchecked")
private <T extends com.codahale.metrics.Metric> SortedMap<String, T> getSimplyNamedMetrics(Class<T> mClass,
        Optional<MetricFilter> filter) {
    ImmutableSortedMap.Builder<String, T> builder = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, InnerMetric> entry : this.contextAwareMetrics.entrySet()) {
        if (mClass.isInstance(entry.getValue())) {
            if (filter.isPresent()
                    && !filter.get().matches(entry.getKey(), entry.getValue().getContextAwareMetric())) {
                continue;
            }/*from w ww  .jav  a 2  s . c  om*/
            builder.put(entry.getKey(), (T) entry.getValue());
        }
    }
    return builder.build();
}

From source file:com.facebook.buck.features.haskell.HaskellLibraryDescription.java

private HaskellPackageRule createPackage(BuildTarget target, ProjectFilesystem projectFilesystem,
        BuildRuleParams baseParams, ActionGraphBuilder graphBuilder, SourcePathResolver pathResolver,
        SourcePathRuleFinder ruleFinder, HaskellPlatformsProvider haskellPlatformsProvider,
        HaskellPlatform platform, HaskellLibraryDescriptionArg args, ImmutableSet<BuildRule> deps,
        Linker.LinkableDepType depType, boolean hsProfile) {

    ImmutableSortedSet<SourcePath> libraries;
    BuildRule library;/*from   w  w w .ja  v  a  2  s  . c o  m*/
    switch (depType) {
    case SHARED:
        library = requireSharedLibrary(getBaseBuildTarget(haskellPlatformsProvider, target), projectFilesystem,
                baseParams, graphBuilder, pathResolver, ruleFinder, haskellPlatformsProvider, platform, args,
                deps, hsProfile);
        libraries = ImmutableSortedSet.of(library.getSourcePathToOutput());
        break;
    case STATIC:
    case STATIC_PIC:
        library = requireStaticLibrary(getBaseBuildTarget(haskellPlatformsProvider, target), projectFilesystem,
                baseParams, graphBuilder, pathResolver, ruleFinder, haskellPlatformsProvider, platform, args,
                deps, depType, false);

        if (hsProfile) {
            if (!(Linker.LinkableDepType.STATIC == depType || Linker.LinkableDepType.STATIC_PIC == depType)) {
                throw new IllegalStateException();
            }

            BuildRule profiledLibrary = requireStaticLibrary(
                    getBaseBuildTarget(haskellPlatformsProvider, target), projectFilesystem, baseParams,
                    graphBuilder, pathResolver, ruleFinder, haskellPlatformsProvider, platform, args, deps,
                    depType, true);

            libraries = ImmutableSortedSet.of(library.getSourcePathToOutput(),
                    profiledLibrary.getSourcePathToOutput());

        } else {
            libraries = ImmutableSortedSet.of(library.getSourcePathToOutput());
        }
        break;
    default:
        throw new IllegalStateException();
    }

    ImmutableSortedMap.Builder<String, HaskellPackage> depPackagesBuilder = ImmutableSortedMap.naturalOrder();
    for (BuildRule rule : deps) {
        if (rule instanceof HaskellCompileDep) {
            ImmutableList<HaskellPackage> packages = ((HaskellCompileDep) rule)
                    .getCompileInput(platform, depType, hsProfile).getPackages();
            for (HaskellPackage pkg : packages) {
                depPackagesBuilder.put(pkg.getInfo().getIdentifier(), pkg);
            }
        }
    }

    ImmutableSortedMap<String, HaskellPackage> depPackages = depPackagesBuilder.build();

    ImmutableSortedSet<SourcePath> interfaces;
    ImmutableSortedSet<SourcePath> objects;
    HaskellCompileRule compileRule = requireCompileRule(target, projectFilesystem, baseParams, graphBuilder,
            pathResolver, ruleFinder, platform, args, deps, depType, false);

    if (hsProfile) {
        HaskellCompileRule profiledCompileRule = requireCompileRule(target, projectFilesystem, baseParams,
                graphBuilder, pathResolver, ruleFinder, platform, args, deps, depType, true);

        interfaces = ImmutableSortedSet.of(compileRule.getInterfaces(), profiledCompileRule.getInterfaces());
        objects = ImmutableSortedSet.of(compileRule.getObjectsDir(), profiledCompileRule.getObjectsDir());
    } else {
        interfaces = ImmutableSortedSet.of(compileRule.getInterfaces());
        objects = ImmutableSortedSet.of(compileRule.getObjectsDir());
    }

    return HaskellPackageRule.from(target, projectFilesystem, baseParams, ruleFinder,
            platform.getPackager().resolve(graphBuilder), platform.getHaskellVersion(), depType,
            getPackageInfo(platform, target), depPackages, compileRule.getModules(), libraries, interfaces,
            objects);
}

From source file:com.google.template.soy.TemplateMetadataSerializer.java

private static SoyType fromProto(SoyTypeP proto, SoyTypeRegistry typeRegistry, String filePath,
        ErrorReporter errorReporter) {/*from  www. j  av a  2 s.c  om*/
    switch (proto.getTypeKindCase()) {
    case PRIMITIVE:
        switch (proto.getPrimitive()) {
        case ANY:
            return AnyType.getInstance();
        case UNKNOWN:
            return UnknownType.getInstance();
        case INT:
            return IntType.getInstance();
        case NULL:
            return NullType.getInstance();
        case BOOL:
            return BoolType.getInstance();
        case FLOAT:
            return FloatType.getInstance();
        case STRING:
            return StringType.getInstance();
        case HTML:
            return HtmlType.getInstance();
        case ATTRIBUTES:
            return AttributesType.getInstance();
        case JS:
            return JsType.getInstance();
        case CSS:
            return StyleType.getInstance();
        case URI:
            return UriType.getInstance();
        case TRUSTED_RESOURCE_URI:
            return TrustedResourceUriType.getInstance();
        case VE_DATA:
            return VeDataType.getInstance();
        case UNRECOGNIZED:
        case UNKNOWN_PRIMITIVE_TYPE:
            // fall-through
        }
        throw new AssertionError("Unknown primitive: " + proto.getPrimitive());
    case LIST_ELEMENT:
        return typeRegistry
                .getOrCreateListType(fromProto(proto.getListElement(), typeRegistry, filePath, errorReporter));

    case LEGACY_OBJECT_MAP:
        return typeRegistry.getOrCreateLegacyObjectMapType(
                fromProto(proto.getLegacyObjectMap().getKey(), typeRegistry, filePath, errorReporter),
                fromProto(proto.getLegacyObjectMap().getValue(), typeRegistry, filePath, errorReporter));
    case MAP:
        return typeRegistry.getOrCreateMapType(
                fromProto(proto.getMap().getKey(), typeRegistry, filePath, errorReporter),
                fromProto(proto.getMap().getValue(), typeRegistry, filePath, errorReporter));
    case PROTO: {
        SoyType type = typeRegistry.getType(proto.getProto());
        if (type == null) {
            errorReporter.report(new SourceLocation(filePath), UNABLE_TO_FIND_TYPE, "proto", proto.getProto());
            return ErrorType.getInstance();
        }
        // allow unknown to support message extraction which configures the DEFAULT_UNKNOWN type
        // registry
        if (type instanceof SoyProtoType || type == UnknownType.getInstance()) {
            return type;
        }
        errorReporter.report(new SourceLocation(filePath), UNEXPECTED_TYPE, proto.getProto(), "proto",
                type.getKind());
        return ErrorType.getInstance();
    }
    case PROTO_ENUM: {
        SoyType type = typeRegistry.getType(proto.getProtoEnum());
        if (type == null) {
            errorReporter.report(new SourceLocation(filePath), UNABLE_TO_FIND_TYPE, "proto enum",
                    proto.getProtoEnum());
            return ErrorType.getInstance();
        }
        // allow unknown to support message extraction which configures the DEFAULT_UNKNOWN type
        // registry
        if (type instanceof SoyProtoEnumType || type == UnknownType.getInstance()) {
            return type;
        }
        errorReporter.report(new SourceLocation(filePath), UNEXPECTED_TYPE, proto.getProtoEnum(), "proto enum",
                type.getKind());
        return ErrorType.getInstance();
    }
    case RECORD: {
        ImmutableSortedMap.Builder<String, SoyType> members = ImmutableSortedMap.naturalOrder();
        for (Map.Entry<String, SoyTypeP> entry : proto.getRecord().getFieldMap().entrySet()) {
            members.put(entry.getKey(), fromProto(entry.getValue(), typeRegistry, filePath, errorReporter));
        }
        return typeRegistry.getOrCreateRecordType(members.build());
    }
    case UNION: {
        List<SoyType> members = new ArrayList<>(proto.getUnion().getMemberCount());
        for (SoyTypeP member : proto.getUnion().getMemberList()) {
            members.add(fromProto(member, typeRegistry, filePath, errorReporter));
        }
        return typeRegistry.getOrCreateUnionType(members);
    }
    case VE:
        return typeRegistry.getOrCreateVeType(proto.getVe());
    case TYPEKIND_NOT_SET:
        // fall-through
    }
    throw new AssertionError("unhandled typeKind: " + proto.getTypeKindCase());
}

From source file:com.google.cloud.datastore.GqlQuery.java

/**
 * Returns an immutable map of named bindings.
 *///from ww  w.java 2s  . com
public Map<String, Object> getNamedBindings() {
    ImmutableMap.Builder<String, Object> builder = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, Binding> binding : namedBindings.entrySet()) {
        builder.put(binding.getKey(), binding.getValue().getCursorOrValue());
    }
    return builder.build();
}

From source file:com.squareup.javapoet.CodeWriter.java

/**
 * Returns the types that should have been imported for this code. If there were any simple name
 * collisions, that type's first use is imported.
 *//*  w ww .ja  va 2 s  .  c  o m*/
ImmutableMap<ClassName, String> suggestedImports() {
    // Find the simple names that can be imported, and the classes that they target.
    Map<String, ClassName> simpleNameToType = new LinkedHashMap<>();
    for (Type type : importableTypes) {
        if (!(type instanceof ClassName))
            continue;
        ClassName className = (ClassName) type;
        if (simpleNameToType.containsKey(className.simpleName()))
            continue;
        simpleNameToType.put(className.simpleName(), className);
    }

    // Invert the map.
    ImmutableSortedMap.Builder<ClassName, String> typeToSimpleName = ImmutableSortedMap.naturalOrder();
    for (Map.Entry<String, ClassName> entry : simpleNameToType.entrySet()) {
        typeToSimpleName.put(entry.getValue(), entry.getKey());
    }

    // TODO(jwilson): omit imports from java.lang, unless their simple names is also present in the
    //     current class's package. (Yuck.)

    return typeToSimpleName.build();
}

From source file:com.facebook.buck.features.haskell.HaskellLibraryDescription.java

private HaskellHaddockLibRule requireHaddockLibrary(BuildTarget baseTarget, ProjectFilesystem projectFilesystem,
        BuildRuleParams baseParams, ActionGraphBuilder graphBuilder, SourcePathResolver pathResolver,
        SourcePathRuleFinder ruleFinder, HaskellPlatform platform, HaskellLibraryDescriptionArg args) {
    CxxPlatform cxxPlatform = platform.getCxxPlatform();
    CxxDeps allDeps = CxxDeps.builder().addDeps(args.getDeps()).addPlatformDeps(args.getPlatformDeps()).build();
    ImmutableSet<BuildRule> deps = allDeps.get(graphBuilder, cxxPlatform);

    // Collect all Haskell deps
    ImmutableSet.Builder<SourcePath> haddockInterfaces = ImmutableSet.builder();
    ImmutableSortedMap.Builder<String, HaskellPackage> packagesBuilder = ImmutableSortedMap.naturalOrder();
    ImmutableSortedMap.Builder<String, HaskellPackage> exposedPackagesBuilder = ImmutableSortedMap
            .naturalOrder();/*from ww w .j  ava2s . c o  m*/

    // Traverse all deps to pull interfaces
    new AbstractBreadthFirstTraversal<BuildRule>(deps) {
        @Override
        public Iterable<BuildRule> visit(BuildRule rule) {
            ImmutableSet.Builder<BuildRule> traverse = ImmutableSet.builder();
            if (rule instanceof HaskellCompileDep) {
                HaskellCompileDep haskellCompileDep = (HaskellCompileDep) rule;

                // Get haddock-interfaces
                HaskellHaddockInput inp = haskellCompileDep.getHaddockInput(platform);
                haddockInterfaces.addAll(inp.getInterfaces());

                HaskellCompileInput compileInput = haskellCompileDep.getCompileInput(platform,
                        Linker.LinkableDepType.STATIC, args.isEnableProfiling());
                boolean firstOrderDep = deps.contains(rule);
                for (HaskellPackage pkg : compileInput.getPackages()) {
                    if (firstOrderDep) {
                        exposedPackagesBuilder.put(pkg.getInfo().getIdentifier(), pkg);
                    } else {
                        packagesBuilder.put(pkg.getInfo().getIdentifier(), pkg);
                    }
                }
                traverse.addAll(haskellCompileDep.getCompileDeps(platform));
            }
            return traverse.build();
        }
    }.start();

    Collection<CxxPreprocessorInput> cxxPreprocessorInputs = CxxPreprocessables
            .getTransitiveCxxPreprocessorInput(cxxPlatform, graphBuilder, deps);
    ExplicitCxxToolFlags.Builder toolFlagsBuilder = CxxToolFlags.explicitBuilder();
    PreprocessorFlags.Builder ppFlagsBuilder = PreprocessorFlags.builder();
    toolFlagsBuilder.setPlatformFlags(
            StringArg.from(CxxSourceTypes.getPlatformPreprocessFlags(cxxPlatform, CxxSource.Type.C)));
    for (CxxPreprocessorInput input : cxxPreprocessorInputs) {
        ppFlagsBuilder.addAllIncludes(input.getIncludes());
        ppFlagsBuilder.addAllFrameworkPaths(input.getFrameworks());
        toolFlagsBuilder.addAllRuleFlags(input.getPreprocessorFlags().get(CxxSource.Type.C));
    }
    ppFlagsBuilder.setOtherFlags(toolFlagsBuilder.build());

    return graphBuilder.addToIndex(HaskellHaddockLibRule
            .from(baseTarget.withAppendedFlavors(Type.HADDOCK.getFlavor(), platform.getFlavor()),
                    projectFilesystem, baseParams, ruleFinder,
                    HaskellSources.from(baseTarget, graphBuilder, pathResolver, ruleFinder, platform, "srcs",
                            args.getSrcs()),
                    platform.getHaddock().resolve(graphBuilder), args.getHaddockFlags(),
                    args.getCompilerFlags(), platform.getLinkerFlags(), haddockInterfaces.build(),
                    packagesBuilder.build(), exposedPackagesBuilder.build(),
                    getPackageInfo(platform, baseTarget), platform, CxxSourceTypes
                            .getPreprocessor(platform.getCxxPlatform(), CxxSource.Type.C).resolve(graphBuilder),
                    ppFlagsBuilder.build()));
}

From source file:com.facebook.buck.rules.CachingBuildRuleBuilder.java

ListenableFuture<BuildResult> build() {
    final AtomicReference<Long> outputSize = Atomics.newReference();

    ListenableFuture<List<BuildResult>> depResults = Futures.immediateFuture(Collections.emptyList());

    // If we're performing a deep build, guarantee that all dependencies will *always* get
    // materialized locally
    if (buildMode == CachingBuildEngine.BuildMode.DEEP
            || buildMode == CachingBuildEngine.BuildMode.POPULATE_FROM_REMOTE_CACHE) {
        depResults = buildRuleBuilderDelegate.getDepResults(rule, buildContext, executionContext);
    }/*from www. j  a v  a 2  s .c o m*/

    ListenableFuture<BuildResult> buildResult = Futures.transformAsync(depResults,
            input -> buildOrFetchFromCache(),
            serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.SCHEDULING_MORE_WORK_RESOURCE_AMOUNTS));

    // Check immediately (without posting a new task) for a failure so that we can short-circuit
    // pending work. Use .catchingAsync() instead of .catching() so that we can propagate unchecked
    // exceptions.
    buildResult = Futures.catchingAsync(buildResult, Throwable.class, throwable -> {
        Preconditions.checkNotNull(throwable);
        buildRuleBuilderDelegate.setFirstFailure(throwable);
        Throwables.throwIfInstanceOf(throwable, Exception.class);
        throw new RuntimeException(throwable);
    });

    buildResult = Futures.transform(buildResult, (result) -> {
        buildRuleBuilderDelegate.markRuleAsUsed(rule, buildContext.getEventBus());
        return result;
    }, MoreExecutors.directExecutor());

    // Setup a callback to handle either the cached or built locally cases.
    AsyncFunction<BuildResult, BuildResult> callback = input -> {

        // If we weren't successful, exit now.
        if (input.getStatus() != BuildRuleStatus.SUCCESS) {
            return Futures.immediateFuture(input);
        }

        try (Scope scope = LeafEvents.scope(buildContext.getEventBus(), "finalizing_build_rule")) {
            // We shouldn't see any build fail result at this point.
            BuildRuleSuccessType success = Preconditions.checkNotNull(input.getSuccess());

            // If we didn't build the rule locally, reload the recorded paths from the build
            // metadata.
            if (success != BuildRuleSuccessType.BUILT_LOCALLY) {
                try {
                    for (String str : onDiskBuildInfo.getValuesOrThrow(BuildInfo.MetadataKey.RECORDED_PATHS)) {
                        buildInfoRecorder.recordArtifact(Paths.get(str));
                    }
                } catch (IOException e) {
                    LOG.error(e, "Failed to read RECORDED_PATHS for %s", rule);
                    throw e;
                }
            }

            // Try get the output size now that all outputs have been recorded.
            if (success == BuildRuleSuccessType.BUILT_LOCALLY) {
                outputSize.set(buildInfoRecorder.getOutputSize());
            }

            // If the success type means the rule has potentially changed it's outputs...
            if (success.outputsHaveChanged()) {

                // The build has succeeded, whether we've fetched from cache, or built locally.
                // So run the post-build steps.
                if (rule instanceof HasPostBuildSteps) {
                    executePostBuildSteps(
                            ((HasPostBuildSteps) rule).getPostBuildSteps(buildContext.getBuildContext()));
                }

                // Invalidate any cached hashes for the output paths, since we've updated them.
                for (Path path : buildInfoRecorder.getRecordedPaths()) {
                    fileHashCache.invalidate(rule.getProjectFilesystem().resolve(path));
                }
            }

            if (SupportsInputBasedRuleKey.isSupported(rule) && success == BuildRuleSuccessType.BUILT_LOCALLY
                    && !buildInfoRecorder.getBuildMetadataFor(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY)
                            .isPresent()) {
                // Doing this here is probably not strictly necessary, however in the case of
                // pipelined rules built locally we will never do an input-based cache check.
                // That check would have written the key to metadata, and there are some asserts
                // during cache upload that try to ensure they are present.
                Optional<RuleKey> inputRuleKey = calculateInputBasedRuleKey(buildContext.getEventBus());
                if (inputRuleKey.isPresent()) {
                    buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY,
                            inputRuleKey.get().toString());
                }
            }

            // If this rule uses dep files and we built locally, make sure we store the new dep file
            // list and re-calculate the dep file rule key.
            if (useDependencyFileRuleKey() && success == BuildRuleSuccessType.BUILT_LOCALLY) {

                // Query the rule for the actual inputs it used.
                ImmutableList<SourcePath> inputs = ((SupportsDependencyFileRuleKey) rule)
                        .getInputsAfterBuildingLocally(buildContext.getBuildContext(),
                                executionContext.getCellPathResolver());

                // Record the inputs into our metadata for next time.
                // TODO(#9117006): We don't support a way to serlialize `SourcePath`s to the cache,
                // so need to use DependencyFileEntry's instead and recover them on deserialization.
                ImmutableList<String> inputStrings = inputs.stream()
                        .map(inputString -> DependencyFileEntry.fromSourcePath(inputString, pathResolver))
                        .map(MoreFunctions.toJsonFunction()).collect(MoreCollectors.toImmutableList());
                buildInfoRecorder.addMetadata(BuildInfo.MetadataKey.DEP_FILE, inputStrings);

                // Re-calculate and store the depfile rule key for next time.
                Optional<RuleKeyAndInputs> depFileRuleKeyAndInputs = calculateDepFileRuleKey(
                        Optional.of(inputStrings), /* allowMissingInputs */ false);
                if (depFileRuleKeyAndInputs.isPresent()) {
                    RuleKey depFileRuleKey = depFileRuleKeyAndInputs.get().getRuleKey();
                    buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.DEP_FILE_RULE_KEY,
                            depFileRuleKey.toString());

                    // Push an updated manifest to the cache.
                    if (useManifestCaching()) {
                        Optional<RuleKeyAndInputs> manifestKey = calculateManifestKey(
                                buildContext.getEventBus());
                        if (manifestKey.isPresent()) {
                            buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.MANIFEST_KEY,
                                    manifestKey.get().getRuleKey().toString());
                            updateAndStoreManifest(depFileRuleKeyAndInputs.get().getRuleKey(),
                                    depFileRuleKeyAndInputs.get().getInputs(), manifestKey.get(),
                                    buildContext.getArtifactCache());
                        }
                    }
                }
            }

            // If this rule was built locally, grab and record the output hashes in the build
            // metadata so that cache hits avoid re-hashing file contents.  Since we use output
            // hashes for input-based rule keys and for detecting non-determinism, we would spend
            // a lot of time re-hashing output paths -- potentially in serialized in a single step.
            // So, do the hashing here to distribute the workload across several threads and cache
            // the results.
            //
            // Also, since hashing outputs can potentially be expensive, we avoid doing this for
            // rules that are marked as uncacheable.  The rationale here is that they are likely not
            // cached due to the sheer size which would be costly to hash or builtin non-determinism
            // in the rule which somewhat defeats the purpose of logging the hash.
            if (success == BuildRuleSuccessType.BUILT_LOCALLY
                    && shouldUploadToCache(success, Preconditions.checkNotNull(outputSize.get()))) {
                ImmutableSortedMap.Builder<String, String> outputHashes = ImmutableSortedMap.naturalOrder();
                for (Path path : buildInfoRecorder.getOutputPaths()) {
                    outputHashes.put(path.toString(),
                            fileHashCache.get(rule.getProjectFilesystem().resolve(path)).toString());
                }
                buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.RECORDED_PATH_HASHES,
                        outputHashes.build());
            }

            // If this rule was fetched from cache, seed the file hash cache with the recorded
            // output hashes from the build metadata.  Since outputs which have been changed have
            // already been invalidated above, this is purely a best-effort optimization -- if the
            // the output hashes weren't recorded in the cache we do nothing.
            if (success != BuildRuleSuccessType.BUILT_LOCALLY && success.outputsHaveChanged()) {
                Optional<ImmutableMap<String, String>> hashes = onDiskBuildInfo
                        .getBuildMap(BuildInfo.MetadataKey.RECORDED_PATH_HASHES);

                // We only seed after first verifying the recorded path hashes.  This prevents the
                // optimization, but is useful to keep in place for a while to verify this optimization
                // is causing issues.
                if (hashes.isPresent() && verifyRecordedPathHashes(rule.getBuildTarget(),
                        rule.getProjectFilesystem(), hashes.get())) {

                    // Seed the cache with the hashes.
                    for (Map.Entry<String, String> ent : hashes.get().entrySet()) {
                        Path path = rule.getProjectFilesystem().getPath(ent.getKey());
                        HashCode hashCode = HashCode.fromString(ent.getValue());
                        fileHashCache.set(rule.getProjectFilesystem().resolve(path), hashCode);
                    }
                }
            }

            // Make sure the origin field is filled in.
            BuildId buildId = buildContext.getBuildId();
            if (success == BuildRuleSuccessType.BUILT_LOCALLY) {
                buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.ORIGIN_BUILD_ID, buildId.toString());
            } else if (success.outputsHaveChanged()) {
                Preconditions.checkState(
                        buildInfoRecorder.getBuildMetadataFor(BuildInfo.MetadataKey.ORIGIN_BUILD_ID)
                                .isPresent(),
                        "Cache hits must populate the %s field (%s)", BuildInfo.MetadataKey.ORIGIN_BUILD_ID,
                        success);
            }

            // Make sure that all of the local files have the same values they would as if the
            // rule had been built locally.
            buildInfoRecorder.addBuildMetadata(BuildInfo.MetadataKey.TARGET, rule.getBuildTarget().toString());
            buildInfoRecorder.addMetadata(BuildInfo.MetadataKey.RECORDED_PATHS,
                    buildInfoRecorder.getRecordedPaths().stream().map(Object::toString)
                            .collect(MoreCollectors.toImmutableList()));
            if (success.shouldWriteRecordedMetadataToDiskAfterBuilding()) {
                try {
                    boolean clearExistingMetadata = success.shouldClearAndOverwriteMetadataOnDisk();
                    buildInfoRecorder.writeMetadataToDisk(clearExistingMetadata);
                } catch (IOException e) {
                    throw new IOException(String.format("Failed to write metadata to disk for %s.", rule), e);
                }
            }

            // Give the rule a chance to populate its internal data structures now that all of
            // the files should be in a valid state.
            try {
                if (rule instanceof InitializableFromDisk) {
                    doInitializeFromDisk((InitializableFromDisk<?>) rule);
                }
            } catch (IOException e) {
                throw new IOException(
                        String.format("Error initializing %s from disk: %s.", rule, e.getMessage()), e);
            }
        }

        return Futures.immediateFuture(input);
    };
    buildResult = Futures.transformAsync(buildResult, ruleAsyncFunction(buildContext.getEventBus(), callback),
            serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.RULE_KEY_COMPUTATION_RESOURCE_AMOUNTS));

    buildResult = Futures.catchingAsync(buildResult, Throwable.class, thrown -> {
        LOG.debug(thrown, "Building rule [%s] failed.", rule.getBuildTarget());

        if (consoleLogBuildFailuresInline) {
            buildContext.getEventBus().post(ConsoleEvent.severe(getErrorMessageIncludingBuildRule(thrown)));
        }

        thrown = maybeAttachBuildRuleNameToException(thrown);
        recordFailureAndCleanUp(thrown);

        return Futures.immediateFuture(BuildResult.failure(rule, thrown));
    });

    // Do things that need to happen after either success or failure, but don't block the dependents
    // while doing so:
    buildRuleBuilderDelegate
            .addAsyncCallback(MoreFutures.addListenableCallback(buildResult, new FutureCallback<BuildResult>() {

                private void uploadToCache(BuildRuleSuccessType success) {

                    // Collect up all the rule keys we have index the artifact in the cache with.
                    Set<RuleKey> ruleKeys = new HashSet<>();

                    // If the rule key has changed (and is not already in the cache), we need to push
                    // the artifact to cache using the new key.
                    ruleKeys.add(ruleKeyFactories.getDefaultRuleKeyFactory().build(rule));

                    // If the input-based rule key has changed, we need to push the artifact to cache
                    // using the new key.
                    if (SupportsInputBasedRuleKey.isSupported(rule)) {
                        Optional<RuleKey> calculatedRuleKey = calculateInputBasedRuleKey(
                                buildContext.getEventBus());
                        Optional<RuleKey> onDiskRuleKey = onDiskBuildInfo
                                .getRuleKey(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY);
                        Optional<RuleKey> metaDataRuleKey = buildInfoRecorder
                                .getBuildMetadataFor(BuildInfo.MetadataKey.INPUT_BASED_RULE_KEY)
                                .map(RuleKey::new);
                        Preconditions.checkState(calculatedRuleKey.equals(onDiskRuleKey),
                                "%s (%s): %s: invalid on-disk input-based rule key: %s != %s",
                                rule.getBuildTarget(), rule.getType(), success, calculatedRuleKey,
                                onDiskRuleKey);
                        Preconditions.checkState(calculatedRuleKey.equals(metaDataRuleKey),
                                "%s: %s: invalid meta-data input-based rule key: %s != %s",
                                rule.getBuildTarget(), success, calculatedRuleKey, metaDataRuleKey);
                        if (calculatedRuleKey.isPresent()) {
                            ruleKeys.add(calculatedRuleKey.get());
                        }
                    }

                    // If the manifest-based rule key has changed, we need to push the artifact to cache
                    // using the new key.
                    if (useManifestCaching()) {
                        Optional<RuleKey> onDiskRuleKey = onDiskBuildInfo
                                .getRuleKey(BuildInfo.MetadataKey.DEP_FILE_RULE_KEY);
                        Optional<RuleKey> metaDataRuleKey = buildInfoRecorder
                                .getBuildMetadataFor(BuildInfo.MetadataKey.DEP_FILE_RULE_KEY).map(RuleKey::new);
                        Preconditions.checkState(onDiskRuleKey.equals(metaDataRuleKey),
                                "%s: %s: inconsistent meta-data and on-disk dep-file rule key: %s != %s",
                                rule.getBuildTarget(), success, onDiskRuleKey, metaDataRuleKey);
                        if (onDiskRuleKey.isPresent()) {
                            ruleKeys.add(onDiskRuleKey.get());
                        }
                    }

                    // Do the actual upload.
                    try {

                        // Verify that the recorded path hashes are accurate.
                        Optional<String> recordedPathHashes = buildInfoRecorder
                                .getBuildMetadataFor(BuildInfo.MetadataKey.RECORDED_PATH_HASHES);
                        if (recordedPathHashes.isPresent() && !verifyRecordedPathHashes(rule.getBuildTarget(),
                                rule.getProjectFilesystem(), recordedPathHashes.get())) {
                            return;
                        }

                        // Push to cache.
                        buildInfoRecorder.performUploadToArtifactCache(ImmutableSet.copyOf(ruleKeys),
                                buildContext.getArtifactCache(), buildContext.getEventBus());

                    } catch (Throwable t) {
                        buildContext.getEventBus().post(
                                ThrowableConsoleEvent.create(t, "Error uploading to cache for %s.", rule));
                    }
                }

                private void handleResult(BuildResult input) {
                    Optional<Long> outputSize = Optional.empty();
                    Optional<HashCode> outputHash = Optional.empty();
                    Optional<BuildRuleSuccessType> successType = Optional.empty();
                    boolean shouldUploadToCache = false;

                    BuildRuleEvent.Resumed resumedEvent = BuildRuleEvent.resumed(rule, buildRuleDurationTracker,
                            ruleKeyFactories.getDefaultRuleKeyFactory());
                    LOG.verbose(resumedEvent.toString());
                    buildContext.getEventBus().post(resumedEvent);

                    if (input.getStatus() == BuildRuleStatus.SUCCESS) {
                        BuildRuleSuccessType success = Preconditions.checkNotNull(input.getSuccess());
                        successType = Optional.of(success);

                        // Try get the output size.
                        if (success == BuildRuleSuccessType.BUILT_LOCALLY
                                || success.shouldUploadResultingArtifact()) {
                            try {
                                outputSize = Optional.of(buildInfoRecorder.getOutputSize());
                            } catch (IOException e) {
                                buildContext.getEventBus().post(ThrowableConsoleEvent.create(e,
                                        "Error getting output size for %s.", rule));
                            }
                        }

                        // Compute it's output hash for logging/tracing purposes, as this artifact will
                        // be consumed by other builds.
                        if (outputSize.isPresent() && shouldHashOutputs(success, outputSize.get())) {
                            try {
                                outputHash = Optional.of(buildInfoRecorder.getOutputHash(fileHashCache));
                            } catch (IOException e) {
                                buildContext.getEventBus().post(ThrowableConsoleEvent.create(e,
                                        "Error getting output hash for %s.", rule));
                            }
                        }

                        // Determine if this is rule is cacheable.
                        shouldUploadToCache = outputSize.isPresent()
                                && shouldUploadToCache(success, outputSize.get());

                        // Upload it to the cache.
                        if (shouldUploadToCache) {
                            uploadToCache(success);
                        }
                    }

                    boolean failureOrBuiltLocally = input.getStatus() == BuildRuleStatus.FAIL
                            || input.getSuccess() == BuildRuleSuccessType.BUILT_LOCALLY;
                    // Log the result to the event bus.
                    BuildRuleEvent.Finished finished = BuildRuleEvent.finished(resumedEvent, getBuildRuleKeys(),
                            input.getStatus(), input.getCacheResult(),
                            onDiskBuildInfo.getBuildValue(BuildInfo.MetadataKey.ORIGIN_BUILD_ID)
                                    .map(BuildId::new),
                            successType, shouldUploadToCache, outputHash, outputSize,
                            getBuildRuleDiagnosticData(failureOrBuiltLocally));
                    LOG.verbose(finished.toString());
                    buildContext.getEventBus().post(finished);
                }

                @Override
                public void onSuccess(BuildResult input) {
                    handleResult(input);

                    // Reset interrupted flag once failure has been recorded.
                    if (input.getFailure() instanceof InterruptedException) {
                        Threads.interruptCurrentThread();
                    }
                }

                @Override
                public void onFailure(@Nonnull Throwable thrown) {
                    throw new AssertionError("Dead code");
                }
            }, serviceByAdjustingDefaultWeightsTo(CachingBuildEngine.RULE_KEY_COMPUTATION_RESOURCE_AMOUNTS)));
    return buildResult;
}

From source file:com.palantir.lock.impl.LockServiceImpl.java

@Override
public LockResponse lock(LockClient client, LockRequest request) throws InterruptedException {
    Preconditions.checkNotNull(client);//from w  w w .  ja  v a  2  s  .c  o  m
    Preconditions.checkArgument(client != INTERNAL_LOCK_GRANT_CLIENT);
    Preconditions.checkArgument(request.getLockTimeout().compareTo(maxAllowedLockTimeout) <= 0,
            "Requested lock timeout (%s) is greater than maximum allowed lock timeout (%s)",
            request.getLockTimeout(), maxAllowedLockTimeout);
    Preconditions.checkArgument(
            (request.getBlockingMode() != BLOCK_UNTIL_TIMEOUT)
                    || (request.getBlockingDuration().compareTo(maxAllowedBlockingDuration) <= 0),
            "Requested blocking duration (%s) is greater than maximum allowed blocking duration (%s)",
            request.getBlockingDuration(), maxAllowedBlockingDuration);
    long startTime = System.currentTimeMillis();
    if (requestLogger.isDebugEnabled()) {
        requestLogger.debug("LockServiceImpl processing lock request {} for requesting thread {}", request,
                request.getCreatingThreadName());
    }
    Map<ClientAwareReadWriteLock, LockMode> locks = Maps.newLinkedHashMap();
    if (isShutDown) {
        throw new ServiceNotAvailableException("This lock server is shut down.");
    }
    try {
        boolean indefinitelyBlocking = isIndefinitelyBlocking(request.getBlockingMode());
        if (indefinitelyBlocking) {
            indefinitelyBlockingThreads.add(Thread.currentThread());
        }
        outstandingLockRequestMultimap.put(client, request);
        Map<LockDescriptor, LockClient> failedLocks = Maps.newHashMap();
        @Nullable
        Long deadline = (request.getBlockingDuration() == null) ? null
                : System.nanoTime() + request.getBlockingDuration().toNanos();
        if (request.getBlockingMode() == BLOCK_UNTIL_TIMEOUT) {
            if (request.getLockGroupBehavior() == LOCK_AS_MANY_AS_POSSIBLE) {
                tryLocks(client, request, DO_NOT_BLOCK, null, LOCK_AS_MANY_AS_POSSIBLE, locks, failedLocks);
            }
        }
        tryLocks(client, request, request.getBlockingMode(), deadline, request.getLockGroupBehavior(), locks,
                failedLocks);

        if (request.getBlockingMode() == BlockingMode.BLOCK_INDEFINITELY_THEN_RELEASE) {
            if (log.isTraceEnabled()) {
                log.trace(".lock(" + client + ", " + request + ") returns null");
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Timed out requesting {} for requesting thread {} after {} ms", request,
                        request.getCreatingThreadName(), System.currentTimeMillis() - startTime);
            }
            return new LockResponse(failedLocks);
        }

        if (locks.isEmpty() || ((request.getLockGroupBehavior() == LOCK_ALL_OR_NONE)
                && (locks.size() < request.getLockDescriptors().size()))) {
            if (log.isTraceEnabled()) {
                log.trace(".lock(" + client + ", " + request + ") returns null");
            }
            if (requestLogger.isDebugEnabled()) {
                requestLogger.debug("Failed to acquire all locks for {} for requesting thread {} after {} ms",
                        request, request.getCreatingThreadName(), System.currentTimeMillis() - startTime);
            }
            if (requestLogger.isTraceEnabled()) {
                StringBuilder sb = new StringBuilder("Current holders of the first ")
                        .append(MAX_FAILED_LOCKS_TO_LOG).append(" of ").append(failedLocks.size())
                        .append(" total failed locks were: [");
                Iterator<Entry<LockDescriptor, LockClient>> entries = failedLocks.entrySet().iterator();
                for (int i = 0; i < MAX_FAILED_LOCKS_TO_LOG; i++) {
                    if (entries.hasNext()) {
                        Entry<LockDescriptor, LockClient> entry = entries.next();
                        sb.append(" Lock: ").append(entry.getKey().toString()).append(", Holder: ")
                                .append(entry.getValue().toString()).append(";");
                    }
                }
                sb.append(" ]");
                requestLogger.trace(sb.toString());
            }
            return new LockResponse(null, failedLocks);
        }

        Builder<LockDescriptor, LockMode> lockDescriptorMap = ImmutableSortedMap.naturalOrder();
        for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
            lockDescriptorMap.put(entry.getKey().getDescriptor(), entry.getValue());
        }
        if (request.getVersionId() != null) {
            versionIdMap.put(client, request.getVersionId());
        }
        HeldLocksToken token = createHeldLocksToken(client, LockCollections.of(lockDescriptorMap.build()),
                LockCollections.of(locks), request.getLockTimeout(), request.getVersionId());
        locks.clear();
        if (log.isTraceEnabled()) {
            log.trace(".lock(" + client + ", " + request + ") returns " + token);
        }
        if (Thread.interrupted()) {
            throw new InterruptedException("Interrupted while locking.");
        }
        if (requestLogger.isDebugEnabled()) {
            requestLogger.debug("Successfully acquired locks {} for requesting thread {} after {} ms", request,
                    request.getCreatingThreadName(), System.currentTimeMillis() - startTime);
        }
        return new LockResponse(token, failedLocks);
    } finally {
        outstandingLockRequestMultimap.remove(client, request);
        indefinitelyBlockingThreads.remove(Thread.currentThread());
        try {
            for (Entry<ClientAwareReadWriteLock, LockMode> entry : locks.entrySet()) {
                entry.getKey().get(client, entry.getValue()).unlock();
            }
        } catch (Throwable e) { // (authorized)
            log.error("Internal lock server error: state has been corrupted!!", e);
            throw Throwables.throwUncheckedException(e);
        }
    }
}