java.lang.Objectedu.ubc.cpsc.googlex.SimpleGoogleSearch
public class SimpleGoogleSearch
A simplified version of the GoogleSearch class.
Please use SimpleGoogleSearch for this assignment rather than using GoogleSearch directly. However, feel free to read through the code for SimpleGoogleSearch to understand GoogleSearch better. We do NOT recommend reading through the code for LoggedGoogleSearch and LoggingGoogleSearch, as they are designed for simplicity of use rather than readability. (We would have designed them differently if we wanted them to be generally useful and readable.)
SimpleGoogleSearch simplifies access to GoogleSearch in three ways. First, it hides GoogleSearch's use of arrays (which we'll learn about shortly!). Second, it hides GoogleSearch's use of exceptions (which we won't discuss in 111, although 211 does discuss them). Third, it provides access to Logged and Logging versions of GoogleSearch to ease debugging and marking. Note: we will mark this assignment using the logged mode of SimpleGoogleSearch and the log we provided you! You should ensure that your code works in that mode!
How do "logging" and "logged" modes work? Well, if both are disabled, SimpleGoogleSearch actually goes to Google and searches for the queries you pose to it. In "logged" mode, however, SimpleGoogleSearch does not connect to the internet at all (which means you can use logged mode even without a network connection). Instead, it uses a file called test.log that we've distributed to you. That file contains a collection of stored searches. "Logged" mode simply accesses those stored searches, printing an error message if a stored search matching the search you requested cannot be found. You don't really need to know about "logging" mode, but in case you want to use it.. Logging mode works just like normal mode except any searches performed in logging mode are appended to the file test.log for future reference in logged mode. (We created test.log by running the sample solution in logging mode and performing some searches!)
Constructor Summary | |
---|---|
SimpleGoogleSearch(java.lang.String key)
Create a SimpleGoogleSearch that uses the provided Google API key. | |
SimpleGoogleSearch(java.lang.String key,
boolean logging, boolean logged)
Create a SimpleGoogleSearch that uses the provided Google API key. |
Method Summary | |
---|---|
boolean |
doSearch(java.lang.String queryString)
Search for the given query, that is, a string you might type into Google when performing a search. |
int |
getHitCount()
Get the number of hits the latest search produced. |
int |
getNumResults()
Gets the number of search results (i.e., snippets) from the latest search. |
java.lang.String |
getSnippetAt(int index)
The snippet Google returned for the search result with the given index. |
static boolean |
isDefaultLogged()
Gets the default "logged" status. |
static boolean |
isDefaultLogging()
Gets the default "logging" status. |
static void |
setIsDefaultLogged(boolean isDefaultLogged)
Sets the default "logged" status. |
static void |
setIsDefaultLogging(boolean isDefaultLogging)
Sets the default "logging" status. |
Methods inherited from class java.lang.Object |
---|
equals, getClass, hashCode, notify, notifyAll, toString, wait,
wait, wait |
Constructor Detail |
---|
public SimpleGoogleSearch(java.lang.String key)
key
- a valid Google API key (non-null)public SimpleGoogleSearch(java.lang.String key, boolean logging, boolean logged)
key
- a valid Google API key (non-null)
logging
- whether to create a logging searcher
logged
- whether to create a searcher that works from a
logMethod Detail |
---|
public boolean doSearch(java.lang.String queryString)
queryString
- a query (non-null)
public int getHitCount()
public int getNumResults()
public java.lang.String getSnippetAt(int index)
index
- a number between 0 and getNumResults - 1. NOTE THAT
THIS STARTS AT ZERO!
public static boolean isDefaultLogged()
public static boolean isDefaultLogging()
public static void setIsDefaultLogged(boolean isDefaultLogged)
isDefaultLogged
- The value to set the default logged
status to.public static void setIsDefaultLogging(boolean isDefaultLogging)
isDefaultLogging
- The value to set the default logging
status to.