View Javadoc
1   /**
2    * Copyright 2010-2014 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.metainf.model;
17  
18  import com.google.common.collect.ImmutableList;
19  import org.kuali.common.util.metainf.spring.MetaInfDataLocation;
20  import org.kuali.common.util.metainf.spring.MetaInfDataType;
21  
22  import java.util.Collections;
23  import java.util.Comparator;
24  import java.util.List;
25  
26  /**
27   * <p>
28   * Sort first by the data passed into the configurable comparator, then sort lexicographically by directory structure,
29   * then filename of the locations contained in each {@code MetaInfResource}
30   * </p>
31   *
32   * For example:
33   *
34   * <pre>
35   *   2 - server/demo/a/foo2.txt         1 - client/bootstrap/a/foo1.txt
36   *   3 - server/demo/a/b/foo.txt        2 - server/demo/a/foo2.txt
37   *   1 - client/bootstrap/a/foo1.txt    3 - server/demo/a/b/foo.txt
38   * </pre>
39   *
40   */
41  public class MetaInfResourceConfigurablePathComparator implements Comparator<MetaInfResource> {
42  
43      private ConfigurablePathComparator comparator = ConfigurablePathComparator.builder().build();
44  
45      @Override
46      public int compare(MetaInfResource one, MetaInfResource two) {
47          return comparator.compare(one.getLocation(), two.getLocation());
48      }
49  
50      public static Builder builder() {
51          return new Builder();
52      }
53  
54      public static class Builder {
55  
56          // Optional
57          private List<String> qualifierOrder = Collections.emptyList();
58          private List<MetaInfDataLocation> locationOrder = Collections.emptyList();
59          private List<MetaInfDataType> typeOrder = Collections.emptyList();
60  
61          public Builder qualifierOrder(List<String> qualifierOrder) {
62              this.qualifierOrder = qualifierOrder;
63              return this;
64          }
65  
66          public Builder locationOrder(List<MetaInfDataLocation> locationOrder) {
67              this.locationOrder = locationOrder;
68              return this;
69          }
70  
71          public Builder typeOrder(List<MetaInfDataType> typeOrder) {
72              this.typeOrder = typeOrder;
73              return this;
74          }
75  
76          public MetaInfResourceConfigurablePathComparator build() {
77              this.qualifierOrder = ImmutableList.copyOf(qualifierOrder);
78              this.locationOrder = ImmutableList.copyOf(locationOrder);
79              this.typeOrder = ImmutableList.copyOf(typeOrder);
80              return new MetaInfResourceConfigurablePathComparator(this);
81          }
82  
83      }
84  
85      private MetaInfResourceConfigurablePathComparator(Builder builder) {
86          ConfigurablePathComparator.Builder comparatorBuilder = ConfigurablePathComparator.builder();
87          comparatorBuilder = comparatorBuilder.qualifierOrder(builder.qualifierOrder);
88          comparatorBuilder = comparatorBuilder.locationOrder(builder.locationOrder);
89          comparatorBuilder = comparatorBuilder.typeOrder(builder.typeOrder);
90          comparator = comparatorBuilder.build();
91      }
92  
93  }