Revision: 2408 http://archive-access.svn.sourceforge.net/archive-access/?rev=2408&view=rev Author: bradtofel Date: 2008-07-07 14:26:40 -0700 (Mon, 07 Jul 2008) Log Message: ----------- FEATURE: added awareness of Anchor Date and Anchor Window to getClosest method, which can optionally throw an exception if no dates are within the range specified. Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java 2008-07-07 21:25:23 UTC (rev 2407) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/CaptureSearchResults.java 2008-07-07 21:26:40 UTC (rev 2408) @@ -29,6 +29,7 @@ import java.util.Iterator; import org.archive.wayback.WaybackConstants; +import org.archive.wayback.exception.AnchorWindowTooSmallException; /** * @@ -78,17 +79,46 @@ return new Timestamp(lastResultTimestamp).getDate(); } + public void markClosest(WaybackRequest wbRequest) { + CaptureSearchResult closest = getClosest(wbRequest); + if(closest != null) { + closest.setClosest(true); + } + } /** * @param wbRequest * @return The closest CaptureSearchResult to the request. */ public CaptureSearchResult getClosest(WaybackRequest wbRequest) { + try { + return getClosest(wbRequest,false); + } catch (AnchorWindowTooSmallException e) { + // cannot happen with 2nd arg false... + e.printStackTrace(); + } + return null; + } + /** + * @param wbRequest + * @return The closest CaptureSearchResult to the request. + */ + public CaptureSearchResult getClosest(WaybackRequest wbRequest, boolean err) + throws AnchorWindowTooSmallException { CaptureSearchResult closest = null; long closestDistance = 0; CaptureSearchResult cur = null; + String anchorDate = wbRequest.get(WaybackRequest.REQUEST_ANCHOR_DATE); + long maxWindow = -1; long wantTime = Timestamp.parseBefore(wbRequest .get(WaybackConstants.REQUEST_EXACT_DATE)).getDate().getTime(); + if(anchorDate != null) { + wantTime = Timestamp.parseBefore(anchorDate).getDate().getTime(); + String anchorWindow = wbRequest.get(WaybackRequest.REQUEST_ANCHOR_WINDOW); + if(anchorWindow != null) { + maxWindow = Long.parseLong(anchorWindow); + } + } Iterator<CaptureSearchResult> itr = results.iterator(); while (itr.hasNext()) { @@ -101,6 +131,13 @@ closestDistance = curDistance; } } + if(err && (maxWindow != -1)) { + if(closestDistance > maxWindow) { + throw new AnchorWindowTooSmallException("Closest is " + + closestDistance + " seconds away, Window is " + + maxWindow); + } + } return closest; } /** This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |