2021-04-26 13:38:12 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2021 Paul Schaub.
|
|
|
|
*
|
|
|
|
* Licensed 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.pgpainless.signature;
|
|
|
|
|
|
|
|
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());
|
|
|
|
}
|
|
|
|
}
|