pgpainless/pgpainless-core/src/main/java/org/pgpainless/signature/consumer/SignatureValidityComparator...

54 lines
1.8 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.signature.consumer;
2021-04-26 13:38:12 +02:00
import java.util.Comparator;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.signature.SignatureUtils;
2021-04-26 13:38:12 +02:00
/**
* Comparator which sorts signatures based on an ordering and on revocation hardness.
*
* If a list of signatures gets ordered using this comparator, hard revocations will always
* come first.
* Further, signatures are ordered by date according to the {@link SignatureCreationDateComparator.Order}.
*/
2021-04-26 13:38:12 +02:00
public class SignatureValidityComparator implements Comparator<PGPSignature> {
private final SignatureCreationDateComparator creationDateComparator;
/**
2021-12-28 13:53:25 +01:00
* Create a new {@link SignatureValidityComparator} which orders signatures the oldest first.
* Still, hard revocations will come first.
*/
2021-04-26 13:38:12 +02:00
public SignatureValidityComparator() {
this(SignatureCreationDateComparator.DEFAULT_ORDER);
}
/**
* Create a new {@link SignatureValidityComparator} which orders signatures following the passed ordering.
* Still, hard revocations will come first.
2022-04-02 18:56:05 +02:00
*
* @param order order of creation dates
*/
2021-04-26 13:38:12 +02:00
public SignatureValidityComparator(SignatureCreationDateComparator.Order order) {
this.creationDateComparator = new SignatureCreationDateComparator(order);
}
@Override
public int compare(PGPSignature one, PGPSignature two) {
boolean oneIsHard = SignatureUtils.isHardRevocation(one);
boolean twoIsHard = SignatureUtils.isHardRevocation(two);
// both have same "hardness", so compare creation time
if (oneIsHard == twoIsHard) {
return creationDateComparator.compare(one, two);
2021-04-26 13:38:12 +02:00
}
// favor the "harder" signature
return oneIsHard ? -1 : 1;
}
}