com.facebook.buck.core.build.engine.manifest.ManifestUtil.java Source code

Java tutorial

Introduction

Here is the source code for com.facebook.buck.core.build.engine.manifest.ManifestUtil.java

Source

/*
 * Copyright 2018-present Facebook, Inc.
 *
 * 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.buck.core.build.engine.manifest;

import com.facebook.buck.core.rulekey.RuleKey;
import com.facebook.buck.util.types.Pair;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableMap.Builder;
import com.google.common.hash.HashCode;
import java.util.Map.Entry;

/** Test only Utilility around {@link Manifest} that provides access of test only methods */
public class ManifestUtil {

    private ManifestUtil() {
    }

    public static ImmutableMap<RuleKey, ImmutableMap<String, HashCode>> toMap(Manifest manifest) {
        Builder<RuleKey, ImmutableMap<String, HashCode>> builder = ImmutableMap.builder();
        for (Pair<RuleKey, int[]> entry : manifest.entries) {
            Builder<String, HashCode> entryBuilder = ImmutableMap.builder();
            for (int hashIndex : entry.getSecond()) {
                Pair<Integer, HashCode> hashEntry = manifest.hashes.get(hashIndex);
                String input = manifest.inputs.get(hashEntry.getFirst());
                HashCode inputHash = hashEntry.getSecond();
                entryBuilder.put(input, inputHash);
            }
            builder.put(entry.getFirst(), entryBuilder.build());
        }
        return builder.build();
    }

    public static Manifest fromMap(RuleKey key, ImmutableMap<RuleKey, ImmutableMap<String, HashCode>> map) {
        Manifest manifest = new Manifest(key);
        for (Entry<RuleKey, ImmutableMap<String, HashCode>> entry : map.entrySet()) {
            int entryHashIndex = 0;
            int[] entryHashIndices = new int[entry.getValue().size()];
            for (Entry<String, HashCode> innerEntry : entry.getValue().entrySet()) {
                entryHashIndices[entryHashIndex++] = manifest.addHash(innerEntry.getKey(), innerEntry.getValue());
            }
            manifest.entries.add(new Pair<RuleKey, int[]>(entry.getKey(), entryHashIndices));
        }
        return manifest;
    }
}