Sort keys by age when printing them. Also fix a generics warning.

This commit is contained in:
Andreas Schildbach 2014-10-15 14:21:21 +02:00
parent 8936338059
commit 98cc6adfc2
2 changed files with 17 additions and 3 deletions

View File

@ -90,9 +90,21 @@ import static com.google.common.base.Preconditions.*;
public class ECKey implements EncryptableItem, Serializable {
private static final Logger log = LoggerFactory.getLogger(ECKey.class);
/** Compares pub key bytes using {@link com.google.common.primitives.UnsignedBytes#lexicographicalComparator()} **/
/** Sorts oldest keys first, newest last. */
public static final Comparator<ECKey> AGE_COMPARATOR = new Comparator<ECKey>() {
@Override
public int compare(ECKey k1, ECKey k2) {
if (k1.creationTimeSeconds == k2.creationTimeSeconds)
return 0;
else
return k1.creationTimeSeconds > k2.creationTimeSeconds ? 1 : -1;
}
};
/** Compares pub key bytes using {@link com.google.common.primitives.UnsignedBytes#lexicographicalComparator()} */
public static final Comparator<ECKey> PUBKEY_COMPARATOR = new Comparator<ECKey>() {
private Comparator comparator = UnsignedBytes.lexicographicalComparator();
private Comparator<byte[]> comparator = UnsignedBytes.lexicographicalComparator();
@Override
public int compare(ECKey k1, ECKey k2) {

View File

@ -726,7 +726,9 @@ public class KeyChainGroup implements KeyBag {
public String toString(boolean includePrivateKeys) {
final StringBuilder builder = new StringBuilder();
if (basic != null) {
for (ECKey key : basic.getKeys())
List<ECKey> keys = basic.getKeys();
Collections.sort(keys, ECKey.AGE_COMPARATOR);
for (ECKey key : keys)
key.formatKeyWithAddress(includePrivateKeys, builder, params);
}
List<String> chainStrs = Lists.newLinkedList();