blob: 07509837ac2e8ebdb6d5764fb66995b86853adf1 [file] [log] [blame]
Damien Martin-Guillerezf88f4d82015-09-25 13:56:55 +00001// Copyright 2015 The Bazel Authors. All rights reserved.
Janak Ramakrishnandf0531f2015-09-23 17:30:04 +00002//
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.base.Predicate;
17import com.google.common.collect.ImmutableList;
18import com.google.devtools.build.lib.util.Pair;
19
20/** Simple utility for separating path and cycle from a combined iterable. */
21class CycleUtils {
22 private CycleUtils() {}
23
24 static <S> Pair<ImmutableList<S>, ImmutableList<S>> splitIntoPathAndChain(
25 Predicate<S> startOfCycle, Iterable<S> pathAndCycle) {
26 boolean inPathToCycle = true;
27 ImmutableList.Builder<S> pathToCycleBuilder = ImmutableList.builder();
28 ImmutableList.Builder<S> cycleBuilder = ImmutableList.builder();
29 for (S elt : pathAndCycle) {
30 if (startOfCycle.apply(elt)) {
31 inPathToCycle = false;
32 }
33 if (inPathToCycle) {
34 pathToCycleBuilder.add(elt);
35 } else {
36 cycleBuilder.add(elt);
37 }
38 }
39 return Pair.of(pathToCycleBuilder.build(), cycleBuilder.build());
40 }
41}