blob: d466391e3916811acc47694667f79bc56d1a082c [file] [log] [blame]
Googler74d894a2017-12-07 10:38:45 -08001// Copyright 2015 The Bazel Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14package com.google.devtools.build.lib.skyframe;
15
16import com.google.common.collect.ImmutableSortedSet;
17import com.google.devtools.build.lib.cmdline.Label;
Googlerab5f2332017-12-11 15:32:29 -080018import com.google.devtools.build.lib.cmdline.LabelCodec;
Googler74d894a2017-12-07 10:38:45 -080019import com.google.devtools.build.lib.skyframe.TestSuiteExpansionValue.TestSuiteExpansionKey;
Googler74d894a2017-12-07 10:38:45 -080020import com.google.devtools.build.lib.skyframe.serialization.ObjectCodec;
21import com.google.devtools.build.lib.skyframe.serialization.SerializationException;
22import com.google.protobuf.CodedInputStream;
23import com.google.protobuf.CodedOutputStream;
24import java.io.IOException;
25import java.util.Comparator;
26
27/** Custom serialization for {@link TestSuiteExpansionKey}. */
28class TestSuiteExpansionKeyCodec implements ObjectCodec<TestSuiteExpansionKey> {
29 public static final TestSuiteExpansionKeyCodec INSTANCE = new TestSuiteExpansionKeyCodec();
30
31 private TestSuiteExpansionKeyCodec() {}
32
33 @Override
34 public Class<TestSuiteExpansionKey> getEncodedClass() {
35 return TestSuiteExpansionKey.class;
36 }
37
38 @Override
39 public void serialize(TestSuiteExpansionKey key, CodedOutputStream codedOut)
40 throws IOException, SerializationException {
41 // Writes the target count to the stream so deserialization knows when to stop.
42 codedOut.writeInt32NoTag(key.getTargets().size());
43 for (Label label : key.getTargets()) {
44 LabelCodec.INSTANCE.serialize(label, codedOut);
45 }
46 }
47
48 @Override
49 public TestSuiteExpansionKey deserialize(CodedInputStream codedIn)
50 throws SerializationException, IOException {
51 int size = codedIn.readInt32();
52 ImmutableSortedSet.Builder<Label> builder =
53 new ImmutableSortedSet.Builder<>(Comparator.naturalOrder());
54 for (int i = 0; i < size; ++i) {
55 builder.add(LabelCodec.INSTANCE.deserialize(codedIn));
56 }
57 return new TestSuiteExpansionKey(builder.build());
58 }
59}