edu.ubc.cpsc.googlex
Class SimpleGoogleSearch

java.lang.Object
  extended by edu.ubc.cpsc.googlex.SimpleGoogleSearch

public class SimpleGoogleSearch
extends java.lang.Object

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

SimpleGoogleSearch

public SimpleGoogleSearch(java.lang.String key)
Create a SimpleGoogleSearch that uses the provided Google API key. The resulting search will use the static default logging settings to determine whether it is a true Google searcher or whether it logs ("logging") or runs from a log ("logged").

Parameters:
key - a valid Google API key (non-null)

SimpleGoogleSearch

public SimpleGoogleSearch(java.lang.String key,
                          boolean logging,
                          boolean logged)
Create a SimpleGoogleSearch that uses the provided Google API key. If the logging parameter is true, the resulting searcher will access the internet directly and log any results for later use by a logged searcher. If the logged parameter is true, the resulting searcher will NOT access the internet directly, working instead from logs. If BOTH are true, the logged parameter overrides the logging parameter. If neither is true, the resulting searcher accesses the internet directly but neither logs nor works from a log.

Parameters:
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 log
Method Detail

doSearch

public boolean doSearch(java.lang.String queryString)
Search for the given query, that is, a string you might type into Google when performing a search.

Parameters:
queryString - a query (non-null)
Returns:
true if the search succeeds (i.e., encounters no errors, even if it doesn't return any results) and false otherwise

getHitCount

public int getHitCount()
Get the number of hits the latest search produced.

Returns:
the number of hits or -1 if there has not yet been a search or if the last search failed

getNumResults

public int getNumResults()
Gets the number of search results (i.e., snippets) from the latest search.

Returns:
the number of search results or 0 if there has not yet been a search or if the last search failed

getSnippetAt

public java.lang.String getSnippetAt(int index)
The snippet Google returned for the search result with the given index.

Parameters:
index - a number between 0 and getNumResults - 1. NOTE THAT THIS STARTS AT ZERO!
Returns:
the snippet at the given index

isDefaultLogged

public static boolean isDefaultLogged()
Gets the default "logged" status.

Returns:
Returns the default "logged" status.

isDefaultLogging

public static boolean isDefaultLogging()
Gets the default "logging" status.

Returns:
Returns the default "logging" status.

setIsDefaultLogged

public static void setIsDefaultLogged(boolean isDefaultLogged)
Sets the default "logged" status.

Parameters:
isDefaultLogged - The value to set the default logged status to.

setIsDefaultLogging

public static void setIsDefaultLogging(boolean isDefaultLogging)
Sets the default "logging" status.

Parameters:
isDefaultLogging - The value to set the default logging status to.