Line data Source code
1 : // Copyright (C) 2021 The Android Open Source Project 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 : 15 : package com.google.gerrit.server.change; 16 : 17 : import static java.util.Comparator.comparing; 18 : import static java.util.stream.Collectors.toList; 19 : 20 : import java.io.IOException; 21 : import java.util.Collection; 22 : import java.util.List; 23 : import org.eclipse.jgit.lib.Ref; 24 : import org.eclipse.jgit.revwalk.RevWalk; 25 : 26 0 : public class IncludedInUtil { 27 : 28 : /** 29 : * Sorts the collection of {@code Ref} instances by its tip commit time. 30 : * 31 : * @param refs collection to be sorted 32 : * @param revWalk {@code RevWalk} instance for parsing ref's tip commit 33 : * @return sorted list of refs 34 : */ 35 : public static List<Ref> getSortedRefs(Collection<Ref> refs, RevWalk revWalk) { 36 3 : return refs.stream() 37 3 : .sorted( 38 3 : comparing( 39 : ref -> { 40 : try { 41 2 : return revWalk.parseCommit(ref.getObjectId()).getCommitTime(); 42 0 : } catch (IOException e) { 43 : // Ignore and continue to sort 44 : } 45 0 : return 0; 46 : })) 47 3 : .collect(toList()); 48 : } 49 : }