2021-10-07 15:48:52 +02:00
|
|
|
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-11-03 13:30:16 +01:00
|
|
|
package org.pgpainless.signature.consumer;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
import java.util.Comparator;
|
|
|
|
|
|
|
|
import org.bouncycastle.openpgp.PGPSignature;
|
|
|
|
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* Comparator which can be used to sort signatures with regard to their creation time.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
public class SignatureCreationDateComparator implements Comparator<PGPSignature> {
|
|
|
|
|
2021-04-30 10:23:12 +02:00
|
|
|
public static final Order DEFAULT_ORDER = Order.OLD_TO_NEW;
|
2021-04-26 13:38:12 +02:00
|
|
|
|
|
|
|
public enum Order {
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* Oldest signatures first.
|
|
|
|
*/
|
2021-04-30 10:23:12 +02:00
|
|
|
OLD_TO_NEW,
|
2021-05-03 13:37:47 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Newest signatures first.
|
|
|
|
*/
|
2021-04-30 10:23:12 +02:00
|
|
|
NEW_TO_OLD
|
2021-04-26 13:38:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private final Order order;
|
|
|
|
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* Create a new comparator which sorts signatures old to new.
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
public SignatureCreationDateComparator() {
|
|
|
|
this(DEFAULT_ORDER);
|
|
|
|
}
|
|
|
|
|
2021-05-03 13:37:47 +02:00
|
|
|
/**
|
|
|
|
* Create a new comparator which sorts signatures according to the passed ordering.
|
|
|
|
* @param order ordering
|
|
|
|
*/
|
2021-04-26 13:38:12 +02:00
|
|
|
public SignatureCreationDateComparator(Order order) {
|
|
|
|
this.order = order;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int compare(PGPSignature one, PGPSignature two) {
|
2021-04-30 10:23:12 +02:00
|
|
|
return order == Order.OLD_TO_NEW
|
2021-04-26 13:38:12 +02:00
|
|
|
? one.getCreationTime().compareTo(two.getCreationTime())
|
|
|
|
: two.getCreationTime().compareTo(one.getCreationTime());
|
|
|
|
}
|
|
|
|
}
|