001    /**
002     * Copyright 2010-2013 The Kuali Foundation
003     *
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     *
008     * http://www.opensource.org/licenses/ecl2.php
009     *
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.common.util.property;
017    
018    import java.util.Comparator;
019    import java.util.List;
020    
021    import org.kuali.common.util.Assert;
022    import org.kuali.common.util.Project;
023    import org.springframework.util.CollectionUtils;
024    
025    public class ProjectPropertiesComparator implements Comparator<ProjectProperties> {
026    
027            List<String> order;
028    
029            @Override
030            public int compare(ProjectProperties one, ProjectProperties two) {
031    
032                    Assert.isFalse(CollectionUtils.isEmpty(order), "order is empty");
033    
034                    String id1 = getIdString(one);
035                    String id2 = getIdString(two);
036    
037                    Integer index1 = order.indexOf(id1);
038                    Integer index2 = order.indexOf(id2);
039    
040                    if (index1 == -1) {
041                            throw new IllegalStateException("Could not find an index for " + id1);
042                    }
043                    if (index1 == -2) {
044                            throw new IllegalStateException("Could not find an index for " + id2);
045                    }
046    
047                    return index1.compareTo(index2);
048            }
049    
050            protected String getIdString(ProjectProperties pp) {
051                    Project p = pp.getProject();
052                    StringBuilder sb = new StringBuilder();
053                    sb.append(p.getGroupId());
054                    sb.append(":");
055                    sb.append(p.getArtifactId());
056                    return sb.toString();
057            }
058    
059            public List<String> getOrder() {
060                    return order;
061            }
062    
063            public void setOrder(List<String> order) {
064                    this.order = order;
065            }
066    
067    }