Skip to content

Commit

Permalink
Merge pull request #4497 from entur/otp2_Use_optonal_in_DataSource
Browse files Browse the repository at this point in the history
Otp2 use optional in data source
  • Loading branch information
t2gran authored Oct 4, 2022
2 parents e30e37d + d23e9ca commit 3d31c0f
Showing 5 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -30,6 +30,9 @@
* these files.
*/
public interface DataSource {
/** Used for returning unknown file size and unknown last modified time */
long UNKNOWN = -1;

/**
* @return the short name identifying the source within its scope (withing a {@link OtpDataStore}
* or {@link CompositeDataSource}) Including the file extension.
@@ -60,14 +63,14 @@ public interface DataSource {
* @return size in bytes, if unknown returns {@code -1}
*/
default long size() {
return -1;
return UNKNOWN;
}

/**
* @return last modified timestamp in ms, if unknown returns {@code -1}
*/
default long lastModified() {
return -1;
return UNKNOWN;
}

/**
Original file line number Diff line number Diff line change
@@ -46,12 +46,16 @@ public final FileType type() {

@Override
public final long size() {
return file.length();
// file.length() may return 0, map this to unknown
long value = file.length();
return value != 0L ? value : DataSource.UNKNOWN;
}

@Override
public final long lastModified() {
return file.lastModified();
// file.lastModified() may return 0, map this to unknown
long value = file.lastModified();
return value != 0L ? value : DataSource.UNKNOWN;
}

@Override
@@ -62,8 +66,8 @@ public final boolean exists() {
@Override
public boolean isWritable() {
// We assume we can write to a file if the parent directory exist, and if the
// file it self exist then it must be writable. If the file do not exist
// we assume we can create a new file and write to it - there is no check on this.
// file exist then it must be writable. If the file do not exist we assume we
// can create a new file and write to it - there is no check on this.
return file.getParentFile().exists() && (!file.exists() || file.canWrite());
}

Original file line number Diff line number Diff line change
@@ -8,12 +8,13 @@
import org.apache.http.Header;
import org.apache.http.HttpHeaders;
import org.apache.http.client.utils.DateUtils;
import org.opentripplanner.datastore.api.DataSource;
import org.opentripplanner.util.lang.ToStringBuilder;

/**
* HTTPS data source metadata returned by the HTTP server (HTTP headers).
*/
public class HttpsDataSourceMetadata {
class HttpsDataSourceMetadata {

static final String CONTENT_TYPE_APPLICATION_GZIP = "application/gzip";
static final String CONTENT_TYPE_APPLICATION_ZIP = "application/zip";
@@ -28,7 +29,7 @@ public class HttpsDataSourceMetadata {
private final long contentLength;
private final long lastModified;

public HttpsDataSourceMetadata(List<Header> headers) {
HttpsDataSourceMetadata(List<Header> headers) {
this(
headers
.stream()
@@ -37,29 +38,29 @@ public HttpsDataSourceMetadata(List<Header> headers) {
);
}

public HttpsDataSourceMetadata(Map<String, String> headers) {
HttpsDataSourceMetadata(Map<String, String> headers) {
contentType = headers.get(HttpHeaders.CONTENT_TYPE);
contentLength = parseLong(headers.get(HttpHeaders.CONTENT_LENGTH));
lastModified = parseDate(headers.get(HttpHeaders.LAST_MODIFIED));
}

public String contentType() {
String contentType() {
return contentType;
}

public long contentLength() {
long contentLength() {
return contentLength;
}

public long lastModified() {
long lastModified() {
return lastModified;
}

public boolean isZipContentType() {
boolean isZipContentType() {
return CONTENT_TYPE_APPLICATION_ZIP.equalsIgnoreCase(contentType());
}

public boolean isGzipContentType() {
boolean isGzipContentType() {
return CONTENT_TYPE_APPLICATION_GZIP.equalsIgnoreCase(contentType());
}

@@ -77,7 +78,7 @@ private static long parseLong(String header) {
try {
return Long.parseLong(header);
} catch (Exception e) {
return -1;
return DataSource.UNKNOWN;
}
}

Original file line number Diff line number Diff line change
@@ -47,8 +47,8 @@ public void testAccessorsForNoneExistingDirectory() throws IOException {
assertEquals(DIRNAME, subject.name());
assertEquals(expectedPath, subject.path());
assertEquals(REPORT, subject.type());
assertEquals(0L, subject.lastModified());
assertEquals(0L, subject.size());
assertEquals(DataSource.UNKNOWN, subject.lastModified());
assertEquals(DataSource.UNKNOWN, subject.size());
assertFalse(subject.exists());
assertTrue(subject.isWritable());

@@ -99,6 +99,6 @@ public void testIO() throws IOException {
}

private String toString(Collection<DataSource> sources) {
return sources.stream().map(DataSource::name).collect(Collectors.toList()).toString();
return sources.stream().map(DataSource::name).sorted().toList().toString();
}
}
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.opentripplanner.datastore.api.DataSource;

public class FileDataSourceTest {

@@ -44,8 +45,8 @@ public void testAccessorsForNoneExistingFile() {
assertEquals(FILENAME, subject.name());
assertEquals(expectedPath, subject.path());
assertEquals(GRAPH, subject.type());
assertEquals(0L, subject.lastModified());
assertEquals(0L, subject.size());
assertEquals(DataSource.UNKNOWN, subject.lastModified());
assertEquals(DataSource.UNKNOWN, subject.size());
assertFalse(subject.exists());
assertTrue(subject.isWritable());

0 comments on commit 3d31c0f

Please sign in to comment.