Damien Martin-Guillerez | f88f4d8 | 2015-09-25 13:56:55 +0000 | [diff] [blame] | 1 | // Copyright 2015 The Bazel Authors. All rights reserved. |
Janak Ramakrishnan | df0531f | 2015-09-23 17:30:04 +0000 | [diff] [blame] | 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. |
| 14 | package com.google.devtools.build.lib.skyframe; |
| 15 | |
| 16 | import com.google.common.base.Predicate; |
| 17 | import com.google.common.collect.ImmutableList; |
| 18 | import com.google.devtools.build.lib.util.Pair; |
| 19 | |
| 20 | /** Simple utility for separating path and cycle from a combined iterable. */ |
| 21 | class 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 | } |