Sort files returned by getFiles by time (oldest first).

This commit is contained in:
Torsten Grote
2022-02-07 09:36:48 -03:00
parent d3beb850ef
commit 5c153aeb6c
3 changed files with 15 additions and 9 deletions

View File

@@ -11,6 +11,7 @@ import java.io.IOException;
import java.util.Collection; import java.util.Collection;
import java.util.List; import java.util.List;
import javax.annotation.Nonnull;
import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.Immutable;
interface MailboxApi { interface MailboxApi {
@@ -130,7 +131,7 @@ interface MailboxApi {
} }
@JsonSerialize @JsonSerialize
class MailboxFile { class MailboxFile implements Comparable<MailboxFile> {
public final MailboxId name; public final MailboxId name;
public final long time; public final long time;
@@ -138,6 +139,13 @@ interface MailboxApi {
this.name = name; this.name = name;
this.time = time; this.time = time;
} }
@Override
public int compareTo(@Nonnull MailboxApi.MailboxFile mailboxFile) {
//noinspection UseCompareMethod
return time < mailboxFile.time ? -1 :
(time == mailboxFile.time ? 0 : 1);
}
} }
@Immutable @Immutable

View File

@@ -17,6 +17,7 @@ import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import java.util.Collections;
import java.util.List; import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
@@ -200,6 +201,7 @@ class MailboxApiImpl implements MailboxApi {
if (time < 1) throw new ApiException(); if (time < 1) throw new ApiException();
list.add(new MailboxFile(MailboxId.fromString(name), time)); list.add(new MailboxFile(MailboxId.fromString(name), time));
} }
Collections.sort(list);
return list; return list;
} catch (JacksonException e) { } catch (JacksonException e) {
throw new ApiException(); throw new ApiException();

View File

@@ -194,14 +194,10 @@ public class MailboxIntegrationTest extends BrambleTestCase {
file3downloaded); file3downloaded);
byte[] downloadedBytes2 = readBytes(file2downloaded); byte[] downloadedBytes2 = readBytes(file2downloaded);
byte[] downloadedBytes3 = readBytes(file3downloaded); byte[] downloadedBytes3 = readBytes(file3downloaded);
// file order is not preserved, so we don't know what file is which // file order is preserved (sorted by time),
if (downloadedBytes2.length == bytes2.length) { // so we know what file is which
assertArrayEquals(bytes2, downloadedBytes2); assertArrayEquals(bytes2, downloadedBytes2);
assertArrayEquals(bytes3, downloadedBytes3); assertArrayEquals(bytes3, downloadedBytes3);
} else {
assertArrayEquals(bytes2, downloadedBytes3);
assertArrayEquals(bytes3, downloadedBytes2);
}
// contact can't download files again, even if knowing name // contact can't download files again, even if knowing name
File file2forbidden = folder.newFile(); File file2forbidden = folder.newFile();