View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util.property;
17  
18  import java.util.Comparator;
19  import java.util.List;
20  
21  import org.kuali.common.util.Assert;
22  import org.kuali.common.util.Project;
23  import org.springframework.util.CollectionUtils;
24  
25  public class ProjectPropertiesComparator implements Comparator<ProjectProperties> {
26  
27  	List<String> order;
28  
29  	@Override
30  	public int compare(ProjectProperties one, ProjectProperties two) {
31  
32  		Assert.isFalse(CollectionUtils.isEmpty(order), "order is empty");
33  
34  		String id1 = getIdString(one);
35  		String id2 = getIdString(two);
36  
37  		Integer index1 = order.indexOf(id1);
38  		Integer index2 = order.indexOf(id2);
39  
40  		if (index1 == -1) {
41  			throw new IllegalStateException("Could not find an index for " + id1);
42  		}
43  		if (index1 == -2) {
44  			throw new IllegalStateException("Could not find an index for " + id2);
45  		}
46  
47  		return index1.compareTo(index2);
48  	}
49  
50  	protected String getIdString(ProjectProperties pp) {
51  		Project p = pp.getProject();
52  		StringBuilder sb = new StringBuilder();
53  		sb.append(p.getGroupId());
54  		sb.append(":");
55  		sb.append(p.getArtifactId());
56  		return sb.toString();
57  	}
58  
59  	public List<String> getOrder() {
60  		return order;
61  	}
62  
63  	public void setOrder(List<String> order) {
64  		this.order = order;
65  	}
66  
67  }