View Javadoc
1   /**
2    * 
3    */
4   package org.kuali.student.git.model;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.List;
9   
10  import org.kuali.student.svn.model.ExternalModuleInfo;
11  
12  /**
13   * @author Kuali Student Team
14   *
15   */
16  public class ExternalsUtils {
17  
18  	/**
19  	 * 
20  	 */
21  	public ExternalsUtils() {
22  		// TODO Auto-generated constructor stub
23  	}
24  
25  	private static int indexOf (List<GitBranchData>list, String targetBranchPath) {
26  		for (int i = 0; i < list.size(); i++) {
27  
28  			GitBranchData data = list.get(i);
29  			
30  			if (data.getBranchPath().equals(targetBranchPath))
31  				return i;
32  		}
33  		
34  		return -1;
35  	}
36  	
37  	
38  	/**
39  	 * Reorder the unordered list so that branches that have externals dependencies will be placed after them in the list.
40  	 * 
41  	 * This is needed so that when we write the externals data they refer to the latest commit id's in the external branches.
42  	 * 
43  	 * @param unorderedList
44  	 * @return
45  	 */
46  	public static List<GitBranchData> computeExternalsAwareOrdering(Collection<GitBranchData>unorderedList) {
47  		
48  		List<GitBranchData> results = new ArrayList<> (unorderedList.size());
49  		
50  		results.addAll(unorderedList);
51  		
52  		if (results.size() == 1)
53  			return results;
54  		
55  		for (GitBranchData data : unorderedList) {
56  			
57  			List<ExternalModuleInfo> externals = data.getExternals();
58  			
59  			if (externals.size() > 0) {
60  				
61  				for (ExternalModuleInfo external : externals) {
62  					
63  					int indexOfDependentBranch = indexOf(results, data.getBranchPath());						
64  					
65  					int indexOfCurrentExternal = indexOf (results, external.getBranchPath());
66  					
67  					if (indexOfDependentBranch == -1 || indexOfCurrentExternal == -1)
68  						continue; // skip over any case where the external isn't represented in the current branches to be committed.
69  					
70  					if (indexOfDependentBranch < indexOfCurrentExternal) {
71  						// remove the dependent branch and insert it after the current external.
72  						
73  						if ((indexOfCurrentExternal + 1) == results.size()) {
74  							// adding at the end of the list
75  							GitBranchData obj = results.remove(indexOfDependentBranch);
76  							
77  							results.add(obj);
78  						}
79  						else {
80  							
81  							/*
82  							 * Add after the external.
83  							 */
84  							GitBranchData obj = results.get(indexOfDependentBranch);
85  							
86  							results.add(indexOfCurrentExternal+1, obj);
87  							
88  							// remove after the add so as to not mess up the indexing.
89  							results.remove(indexOfDependentBranch);
90  						}
91  					}
92  				}
93  			}
94  			
95  		}
96  		
97  		
98  		return results;
99  	}
100 }