001package io.konik.csv.pdf; 002 003import java.util.Comparator; 004 005import javax.annotation.Nullable; 006 007import com.google.common.base.Function; 008 009import io.konik.harness.FileAppender; 010 011/** 012 * {@link FileAppender} {@link Comparator} that uses {@code FileAppender.getPriority()} method 013 * to define correct order. 014 * 015 * Objects with higher priority come first. 016 */ 017public class FileAppenderPriorityComparator implements Comparator<FileAppender> { 018 019 public enum Order { 020 DESC(new Function<Integer, Integer>() { 021 public Integer apply(Integer input) { 022 return input * -1; 023 } 024 }), 025 ASC(new Function<Integer, Integer>() { 026 public Integer apply(@Nullable Integer input) { 027 return input; 028 } 029 }); 030 031 private Function<Integer, Integer> transform; 032 033 Order(Function<Integer, Integer> transform) { 034 this.transform = transform; 035 } 036 037 public Integer apply(Integer value) { 038 return this.transform.apply(value); 039 } 040 } 041 042 private final Order order; 043 044 public FileAppenderPriorityComparator(Order order) { 045 this.order = order; 046 } 047 048 @Override 049 public int compare(FileAppender fa1, FileAppender fa2) { 050 if (fa1 != null && fa2 != null) { 051 return order.apply(fa1.getPriority() - fa2.getPriority()); 052 } 053 054 if (fa1 != null) { 055 return order.apply(1); 056 } 057 058 return 0; 059 } 060}