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