blob: 3d0d501f7b5828ef89402d4c4f0377a511a5ac19 [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.tamaya.spisupport;
import org.apache.tamaya.spi.PropertyFilter;
import org.apache.tamaya.spi.ServiceContext;
import javax.annotation.Priority;
import java.io.Serializable;
import java.util.Comparator;
/**
* Comparator for {@link PropertyFilter}s based on their priority annotations.
*/
public final class PropertyFilterComparator implements Comparator<PropertyFilter>, Serializable {
private static final long serialVersionUID = 1L;
private static final PropertyFilterComparator INSTANCE = new PropertyFilterComparator();
/**
* Get the shared instance of the comparator.
* @return the shared instance, never null.
*/
public static PropertyFilterComparator getInstance(){
return INSTANCE;
}
private PropertyFilterComparator(){}
/**
* Compare 2 filters for ordering the filter chain.
*
* @param filter1 the first filter
* @param filter2 the second filter
* @return the comparison result
*/
private int comparePropertyFilters(PropertyFilter filter1, PropertyFilter filter2) {
int ord1 = 0;
int ord2 = 0;
if(ServiceContext.PRIORITY_ANNOTATION_AVAILABLE) {
Priority prio1 = filter1.getClass().getAnnotation(Priority.class);
Priority prio2 = filter2.getClass().getAnnotation(Priority.class);
if(prio1!=null) {
ord1 = prio1.value();
}
if(prio2!=null) {
ord2 = prio2.value();
}
}
if (ord1 < ord2) {
return -1;
} else if (ord1 > ord2) {
return 1;
} else {
return filter1.getClass().getName().compareTo(filter2.getClass().getName());
}
}
@Override
public int compare(PropertyFilter filter1, PropertyFilter filter2) {
return comparePropertyFilters(filter1, filter2);
}
}