Skip to content

Commit 12070b2

Browse files
committed
GCModel/FileInformation: Also store creation date of underlying resource
In cases where the creation date is unavailable (URL resource, unix filestystem) the lastModified time is used instead.
1 parent 3ac5d97 commit 12070b2

File tree

1 file changed

+67
-18
lines changed

1 file changed

+67
-18
lines changed

src/main/java/com/tagtraum/perf/gcviewer/model/GCModel.java

Lines changed: 67 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@
1313
import java.net.HttpURLConnection;
1414
import java.net.URL;
1515
import java.net.URLConnection;
16+
import java.nio.file.Files;
17+
import java.nio.file.attribute.BasicFileAttributes;
1618
import java.time.Duration;
1719
import java.time.ZonedDateTime;
1820
import java.time.temporal.ChronoUnit;
@@ -36,29 +38,56 @@ public class GCModel implements Serializable {
3638
*/
3739
private static class FileInformation implements Serializable {
3840
private static final long serialVersionUID = 1L;
41+
private static final Logger logger = Logger.getLogger(FileInformation.class.getName());
3942

43+
public long creationTime;
4044
public long lastModified;
4145
public long length;
4246

4347
public FileInformation() {
44-
this(-1, -1);
48+
creationTime = 0;
49+
creationTime = 0;
50+
length = 0;
4551
}
4652

47-
public FileInformation(long lastModified, long length) {
48-
super();
53+
public FileInformation(File file) {
54+
if (file == null)
55+
throw new IllegalArgumentException("File must not be null!");
4956

50-
this.lastModified = lastModified;
51-
this.length = length;
57+
Optional<BasicFileAttributes> fileAttributes = getFileAttributes(file);
58+
this.lastModified = file.lastModified();
59+
this.creationTime = determineCreationDate(file, fileAttributes);
60+
this.length = file.length();
61+
}
62+
63+
private Optional<BasicFileAttributes> getFileAttributes(File file) {
64+
try {
65+
return Optional.of(Files.readAttributes(file.toPath(), BasicFileAttributes.class));
66+
}
67+
catch (IOException ex) {
68+
logger.log(Level.WARNING, "Failed to read attributes of file " + file + ". Reason: " + ex.getMessage());
69+
logger.log(Level.FINER, "Details: ", ex);
70+
}
71+
return Optional.empty();
72+
}
73+
74+
private long determineCreationDate(File file, Optional<BasicFileAttributes> fileAttributes) {
75+
if (fileAttributes.isPresent()) {
76+
return fileAttributes.get().creationTime().toMillis();
77+
}
78+
else {
79+
// Creation date is unavailable on unix based oS
80+
return file.lastModified();
81+
}
5282
}
5383

5484
public void setFileInformation(FileInformation other) {
85+
this.creationTime = other.creationTime;
5586
this.lastModified = other.lastModified;
5687
this.length = other.length;
5788
}
5889

59-
/**
60-
* @see java.lang.Object#equals(java.lang.Object)
61-
*/
90+
@Override
6291
public boolean equals(Object other) {
6392
if (this == other) {
6493
return true;
@@ -72,8 +101,15 @@ public boolean equals(Object other) {
72101

73102
FileInformation fileInfo = (FileInformation)other;
74103

75-
return fileInfo.lastModified == lastModified
76-
&& fileInfo.length == length;
104+
return fileInfo.lastModified == lastModified && fileInfo.creationTime == creationTime && fileInfo.length == length;
105+
}
106+
107+
@Override
108+
public int hashCode() {
109+
int result = (int) (creationTime ^ (creationTime >>> 32));
110+
result = 31 * result + (int) (lastModified ^ (lastModified >>> 32));
111+
result = 31 * result + (int) (length ^ (length >>> 32));
112+
return result;
77113
}
78114

79115
@Override
@@ -189,6 +225,10 @@ public long getLastModified() {
189225
return fileInformation.lastModified;
190226
}
191227

228+
public long getCreationTime() {
229+
return fileInformation.creationTime;
230+
}
231+
192232
public URL getURL() {
193233
return url;
194234
}
@@ -232,19 +272,28 @@ public void printDetailedInformation() {
232272

233273
private FileInformation readFileInformation(URL url) {
234274
FileInformation fileInformation = new FileInformation();
235-
URLConnection urlConnection = null;
275+
URLConnection urlConnection;
236276
try {
237-
urlConnection = url.openConnection();
238277
if (url.getProtocol().startsWith("http")) {
239-
((HttpURLConnection)urlConnection).setRequestMethod("HEAD");
278+
urlConnection = url.openConnection();
279+
((HttpURLConnection) urlConnection).setRequestMethod("HEAD");
280+
try (InputStream inputStream = urlConnection.getInputStream()) {
281+
fileInformation.length = urlConnection.getContentLength();
282+
fileInformation.lastModified = urlConnection.getLastModified();
283+
}
240284
}
241-
try (InputStream inputStream = urlConnection.getInputStream()) {
242-
fileInformation.length = urlConnection.getContentLength();
243-
fileInformation.lastModified = urlConnection.getLastModified();
285+
else {
286+
if (url.getProtocol().startsWith("file")) {
287+
File file = new File(url.getFile());
288+
if (file.exists()) {
289+
fileInformation = new FileInformation(file);
290+
}
291+
}
244292
}
245293
}
246294
catch (IOException e) {
247-
if (LOG.isLoggable(Level.WARNING)) LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e);
295+
if (LOG.isLoggable(Level.WARNING))
296+
LOG.log(Level.WARNING, "Failed to obtain age and length of URL " + url, e);
248297
}
249298

250299
return fileInformation;
@@ -257,7 +306,7 @@ public void setURL(URL url) {
257306

258307
public boolean isDifferent(File otherFile) {
259308
// we just ignore the file name for now...
260-
FileInformation fileInformation = new FileInformation(otherFile.lastModified(), otherFile.length());
309+
FileInformation fileInformation = new FileInformation(otherFile);
261310

262311
return !this.fileInformation.equals(fileInformation);
263312
}

0 commit comments

Comments
 (0)