From: <bra...@us...> - 2008-07-01 23:16:14
|
Revision: 2350 http://archive-access.svn.sourceforge.net/archive-access/?rev=2350&view=rev Author: bradtofel Date: 2008-07-01 16:16:20 -0700 (Tue, 01 Jul 2008) Log Message: ----------- REFACTOR: these are now base classes with common method for (Url|Capture)SearchResult(s) classes. Modified Paths: -------------- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResults.java Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java 2008-07-01 23:13:28 UTC (rev 2349) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResult.java 2008-07-01 23:16:20 UTC (rev 2350) @@ -24,10 +24,10 @@ */ package org.archive.wayback.core; -import java.util.Properties; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; -import org.archive.wayback.WaybackConstants; - /** * * @@ -35,6 +35,9 @@ * @version $Date$, $Revision$ */ public class SearchResult { + + public static final String RESULT_TRUE_VALUE = "true"; + /** * Expandable Data bag for String to String tuples -- who knows what data * we'll want to put in an Index. Perhaps this should BE a Properties, @@ -42,71 +45,39 @@ * 'type' field that would allow discrimination/hinting at what kind * of data might be found in the Properties... */ - private Properties data = null; - - /** - * Constructor - */ + protected HashMap<String,String> data = null; + public SearchResult() { - super(); - data = new Properties(); + data = new HashMap<String,String>(); } - - /** - * @param key - * @return boolean true if 'key' is a key in 'data' - */ - public boolean containsKey(String key) { - return data.containsKey(key); - } - - /** - * @param key - * @return String value for key 'key' -- null if 'key' does not exist - */ public String get(String key) { - return (String) data.get(key); + return data.get(key); } - - /** - * @param key - * @param value - * @return String previous value of 'key' - */ - public String put(String key, String value) { - return (String) data.put(key, value); + public void put(String key, String value) { + data.put(key,value); } - - /** - * @return Returns the data. - */ - public Properties getData() { - return data; + public boolean getBoolean(String key) { + String value = get(key); + return (value != null && value.equals(RESULT_TRUE_VALUE)); } - - /** - * @return the (probably) 14-digit timestamp indicating when this capture - * was made. - */ - public String getCaptureDate() { - return get(WaybackConstants.RESULT_CAPTURE_DATE); + public void putBoolean(String key, boolean value) { + if(value) { + put(key,RESULT_TRUE_VALUE); + } else { + data.remove(key); + } } - - /** - * @return the url that created this request, without the leading http:// - */ - public String getUrl() { - return get(WaybackConstants.RESULT_URL); + protected String dateToTS(Date date) { + return new Timestamp(date).getDateStr(); } - - /** - * @return the url that created this request, including the leading http:// - */ - public String getAbsoluteUrl() { - String url = get(WaybackConstants.RESULT_URL); - if(url.startsWith(WaybackConstants.HTTP_URL_PREFIX)) { - return url; - } - return WaybackConstants.HTTP_URL_PREFIX + url; + protected Date tsToDate(String timestamp) { + return Timestamp.parseBefore(timestamp).getDate(); } + public Map<String, String> toCanonicalStringMap() { + return data; + } + public void fromCanonicalStringMap(Map<String, String> canonical) { + data = new HashMap<String, String>(); + data.putAll(canonical); + } } Modified: trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResults.java =================================================================== --- trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResults.java 2008-07-01 23:13:28 UTC (rev 2349) +++ trunk/archive-access/projects/wayback/wayback-core/src/main/java/org/archive/wayback/core/SearchResults.java 2008-07-01 23:16:20 UTC (rev 2350) @@ -24,9 +24,8 @@ */ package org.archive.wayback.core; -import java.util.ArrayList; -import java.util.Iterator; -import java.util.Properties; +import java.util.HashMap; +import java.util.Map; /** * @@ -36,100 +35,42 @@ */ public abstract class SearchResults { /** - * List of SearchResult objects for index records matching a query + * Results: int total number of records matching, not all necc. returned. */ - protected ArrayList<SearchResult> results = null; + public static final String RESULTS_NUM_RESULTS = "numresults"; + /** - * 14-digit timestamp of first capture date contained in the SearchResults + * Results: int first record of all matching returned, 1-based */ - protected String firstResultDate; + public static final String RESULTS_FIRST_RETURNED = "firstreturned"; + /** - * 14-digit timestamp of last capture date contained in the SearchResults + * Results: int total number of records *returned* in results */ - protected String lastResultDate; + public static final String RESULTS_NUM_RETURNED = "numreturned"; + /** + * Results: int number of results requested + */ + public static final String RESULTS_REQUESTED = "resultsrequested"; + /** * Expandable data bag for tuples associated with the search results, * likely examples might be "total matching documents", "index of first * document returned", etc. */ - private Properties filters = new Properties(); - + private HashMap<String,String> filters = null; /** * Constructor */ public SearchResults() { - super(); - results = new ArrayList<SearchResult>(); + filters = new HashMap<String,String>(); } - /** - * @return true if no SearchResult objects, false otherwise. - */ - public boolean isEmpty() { - return results.isEmpty(); - } + private long returnedCount = -1; + private long firstReturned = -1; + private long matchingCount = -1; + private long numRequested = -1; /** - * @param result - * @param append - */ - public void addSearchResultRaw(final SearchResult result, - final boolean append) { - - if(append) { - results.add(result); - } else { - results.add(0,result); - } - } - - /** - * @return one of "Url" or "Capture" depending on the type of results - * contained in this object - */ - public abstract String getResultsType(); - - /** - * append a result - * @param result - */ - public abstract void addSearchResult(final SearchResult result); - /** - * add a result to this results, at either the begginning or at the end, - * depending on the append argument - * @param result - * SearchResult to add to this set - * @param append - */ - public abstract void addSearchResult(final SearchResult result, - final boolean append); - - /** - * @return number of SearchResult objects contained in these SearchResults - */ - public int getResultCount() { - return results.size(); - } - - /** - * @return an Iterator that contains the SearchResult objects - */ - public Iterator<SearchResult> iterator() { - return results.iterator(); - } - /** - * @return Returns the firstResultDate. - */ - public String getFirstResultDate() { - return firstResultDate; - } - /** - * @return Returns the lastResultDate. - */ - public String getLastResultDate() { - return lastResultDate; - } - - /** * @param key * @return boolean, true if key 'key' exists in filters */ @@ -142,7 +83,7 @@ * @return value of key 'key' in filters */ public String getFilter(String key) { - return filters.getProperty(key); + return filters.get(key); } /** @@ -151,12 +92,66 @@ * @return previous String value of key 'key' or null if there was none */ public String putFilter(String key, String value) { - return (String) filters.setProperty(key, value); + return (String) filters.put(key, value); } /** * @return Returns the filters. */ - public Properties getFilters() { + public Map<String,String> getFilters() { return filters; } + private long getLongFilter(String key) { + String tmp = getFilter(key); + if(tmp == null) { + return 0; + } + return Long.parseLong(tmp); + } + + public long getReturnedCount() { + if(returnedCount == -1) { + returnedCount = getLongFilter(RESULTS_NUM_RETURNED); + } + return returnedCount; + } + public void setReturnedCount(long returnedCount) { + this.returnedCount = returnedCount; + putFilter(RESULTS_NUM_RETURNED, String.valueOf(returnedCount)); + } + + public long getFirstReturned() { + if(firstReturned == -1) { + firstReturned = getLongFilter(RESULTS_FIRST_RETURNED); + } + return firstReturned; + } + + public void setFirstReturned(long firstReturned) { + this.firstReturned = firstReturned; + putFilter(RESULTS_FIRST_RETURNED, String.valueOf(firstReturned)); + } + + public long getMatchingCount() { + if(matchingCount == -1) { + matchingCount = getLongFilter(RESULTS_NUM_RESULTS); + } + return matchingCount; + } + + public void setMatchingCount(long matchingCount) { + this.matchingCount = matchingCount; + putFilter(RESULTS_NUM_RESULTS, String.valueOf(matchingCount)); + } + + public long getNumRequested() { + if(numRequested == -1) { + numRequested = getLongFilter(RESULTS_REQUESTED); + } + return numRequested; + } + + public void setNumRequested(long numRequested) { + this.numRequested = numRequested; + putFilter(RESULTS_REQUESTED, String.valueOf(numRequested)); + } } This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |