refactor test for resource lastModified comparisons

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
This commit is contained in:
Lachlan Roberts 2022-09-27 14:15:12 +10:00
parent b503d93360
commit c58f769117
3 changed files with 17 additions and 6 deletions

View File

@ -84,7 +84,7 @@ public abstract class Resource implements Iterable<Resource>
{
if (resource == null)
return "null exists=false directory=false lm=-1";
return "%s exists=%b directory=%b lm=%d"
return "%s exists=%b directory=%b lm=%s"
.formatted(resource.toString(), resource.exists(), resource.isDirectory(), resource.lastModified());
}

View File

@ -231,7 +231,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory
{
compressedContent = null;
Resource compressedResource = _factory.newResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified().compareTo(resource.lastModified()) >= 0 &&
if (compressedResource.exists() && ResourceContentFactory.newerThanOrEqual(compressedResource, resource) &&
compressedResource.length() < resource.length())
{
compressedContent = new CachedHttpContent(compressedPathInContext, compressedResource, null);
@ -272,12 +272,12 @@ public class CachedContentFactory implements HttpContent.ContentFactory
{
String compressedPathInContext = pathInContext + format.getExtension();
CachedHttpContent compressedContent = _cache.get(compressedPathInContext);
if (compressedContent != null && compressedContent.isValid() && compressedContent.getResource().lastModified().compareTo(resource.lastModified()) >= 0)
if (compressedContent != null && compressedContent.isValid() && ResourceContentFactory.newerThanOrEqual(compressedContent.getResource(), resource))
compressedContents.put(format, compressedContent);
// Is there a precompressed resource?
Resource compressedResource = _factory.newResource(compressedPathInContext);
if (compressedResource.exists() && compressedResource.lastModified().compareTo(resource.lastModified()) >= 0 &&
if (compressedResource.exists() && ResourceContentFactory.newerThanOrEqual(compressedResource, resource) &&
compressedResource.length() < resource.length())
compressedContents.put(format,
new ResourceHttpContent(compressedResource, _mimeTypes.getMimeByExtension(compressedPathInContext)));
@ -613,7 +613,7 @@ public class CachedContentFactory implements HttpContent.ContentFactory
public boolean isValid()
{
return _precompressedContent.isValid() && _content.isValid() &&
_content.getResource().lastModified().compareTo(_precompressedContent.getResource().lastModified()) >= 0;
ResourceContentFactory.newerThanOrEqual(_precompressedContent.getResource(), _content.getResource());
}
@Override

View File

@ -87,7 +87,7 @@ public class ResourceContentFactory implements ContentFactory
{
String compressedPathInContext = pathInContext + format.getExtension();
Resource compressedResource = _factory.newResource(compressedPathInContext);
if (compressedResource != null && compressedResource.exists() && compressedResource.lastModified().compareTo(resource.lastModified()) >= 0 &&
if (compressedResource != null && compressedResource.exists() && ResourceContentFactory.newerThanOrEqual(compressedResource, resource) &&
compressedResource.length() < resource.length())
compressedContents.put(format,
new ResourceHttpContent(compressedResource, _mimeTypes.getMimeByExtension(compressedPathInContext)));
@ -98,6 +98,17 @@ public class ResourceContentFactory implements ContentFactory
return new ResourceHttpContent(resource, mt);
}
/**
* <p>Utility to compare {@link Resource#lastModified()} of two resources.</p>
* @param resource1 the first resource to test.
* @param resource2 the second resource to test.
* @return true if modified time of resource1 is newer or equal to that of resource2.
*/
static boolean newerThanOrEqual(Resource resource1, Resource resource2)
{
return !resource2.lastModified().isAfter(resource1.lastModified());
}
@Override
public String toString()
{