View Javadoc
1   /*
2    * Copyright (C) 2008, Charles O'Farrell <charleso@charleso.org>
3    * Copyright (C) 2010, Google Inc. and others
4    *
5    * This program and the accompanying materials are made available under the
6    * terms of the Eclipse Distribution License v. 1.0 which is available at
7    * https://www.eclipse.org/org/documents/edl-v10.php.
8    *
9    * SPDX-License-Identifier: BSD-3-Clause
10   */
11  
12  package org.eclipse.jgit.lib;
13  
14  import java.util.ArrayList;
15  import java.util.Collection;
16  import java.util.Collections;
17  import java.util.Comparator;
18  import java.util.List;
19  
20  /**
21   * Util for sorting (or comparing) Ref instances by name.
22   * <p>
23   * Useful for command line tools or writing out refs to file.
24   */
25  public class RefComparator implements Comparator<Ref> {
26  
27  	/** Singleton instance of RefComparator */
28  	public static final RefComparatorml#RefComparator">RefComparator INSTANCE = new RefComparator();
29  
30  	/** {@inheritDoc} */
31  	@Override
32  	public int compare(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) {
33  		return compareTo(o1, o2);
34  	}
35  
36  	/**
37  	 * Sorts the collection of refs, returning a new collection.
38  	 *
39  	 * @param refs
40  	 *            collection to be sorted
41  	 * @return sorted collection of refs
42  	 */
43  	public static Collection<Ref> sort(Collection<Ref> refs) {
44  		final List<Ref> r = new ArrayList<>(refs);
45  		Collections.sort(r, INSTANCE);
46  		return r;
47  	}
48  
49  	/**
50  	 * Compare a reference to a name.
51  	 *
52  	 * @param o1
53  	 *            the reference instance.
54  	 * @param o2
55  	 *            the name to compare to.
56  	 * @return standard Comparator result of &lt; 0, 0, &gt; 0.
57  	 */
58  	public static int compareTo(Ref o1, String o2) {
59  		return o1.getName().compareTo(o2);
60  	}
61  
62  	/**
63  	 * Compare two references by name.
64  	 *
65  	 * @param o1
66  	 *            the reference instance.
67  	 * @param o2
68  	 *            the other reference instance.
69  	 * @return standard Comparator result of &lt; 0, 0, &gt; 0.
70  	 */
71  	public static int compareTo(Reff" href="../../../../org/eclipse/jgit/lib/Ref.html#Ref">Ref o1, Ref o2) {
72  		return o1.getName().compareTo(o2.getName());
73  	}
74  }