001/** 002 * Copyright 2010-2014 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 */ 016package org.kuali.common.util.metainf.model; 017 018import com.google.common.collect.ImmutableList; 019import org.kuali.common.util.metainf.spring.MetaInfDataLocation; 020import org.kuali.common.util.metainf.spring.MetaInfDataType; 021 022import java.util.Collections; 023import java.util.Comparator; 024import java.util.List; 025 026/** 027 * <p> 028 * Sort first by the data passed into the configurable comparator, then sort lexicographically by directory structure, 029 * then filename of the locations contained in each {@code MetaInfResource} 030 * </p> 031 * 032 * For example: 033 * 034 * <pre> 035 * 2 - server/demo/a/foo2.txt 1 - client/bootstrap/a/foo1.txt 036 * 3 - server/demo/a/b/foo.txt 2 - server/demo/a/foo2.txt 037 * 1 - client/bootstrap/a/foo1.txt 3 - server/demo/a/b/foo.txt 038 * </pre> 039 * 040 */ 041public class MetaInfResourceConfigurablePathComparator implements Comparator<MetaInfResource> { 042 043 private ConfigurablePathComparator comparator = ConfigurablePathComparator.builder().build(); 044 045 @Override 046 public int compare(MetaInfResource one, MetaInfResource two) { 047 return comparator.compare(one.getLocation(), two.getLocation()); 048 } 049 050 public static Builder builder() { 051 return new Builder(); 052 } 053 054 public static class Builder { 055 056 // Optional 057 private List<String> qualifierOrder = Collections.emptyList(); 058 private List<MetaInfDataLocation> locationOrder = Collections.emptyList(); 059 private List<MetaInfDataType> typeOrder = Collections.emptyList(); 060 061 public Builder qualifierOrder(List<String> qualifierOrder) { 062 this.qualifierOrder = qualifierOrder; 063 return this; 064 } 065 066 public Builder locationOrder(List<MetaInfDataLocation> locationOrder) { 067 this.locationOrder = locationOrder; 068 return this; 069 } 070 071 public Builder typeOrder(List<MetaInfDataType> typeOrder) { 072 this.typeOrder = typeOrder; 073 return this; 074 } 075 076 public MetaInfResourceConfigurablePathComparator build() { 077 this.qualifierOrder = ImmutableList.copyOf(qualifierOrder); 078 this.locationOrder = ImmutableList.copyOf(locationOrder); 079 this.typeOrder = ImmutableList.copyOf(typeOrder); 080 return new MetaInfResourceConfigurablePathComparator(this); 081 } 082 083 } 084 085 private MetaInfResourceConfigurablePathComparator(Builder builder) { 086 ConfigurablePathComparator.Builder comparatorBuilder = ConfigurablePathComparator.builder(); 087 comparatorBuilder = comparatorBuilder.qualifierOrder(builder.qualifierOrder); 088 comparatorBuilder = comparatorBuilder.locationOrder(builder.locationOrder); 089 comparatorBuilder = comparatorBuilder.typeOrder(builder.typeOrder); 090 comparator = comparatorBuilder.build(); 091 } 092 093}