package com.simstock.fetcher;

import java.util.Calendar;

/**
 * YahooDataFetcher
 * 
 * Data Fetcher using "Builder" Design Pattern that is introduced in Effective
 * Java
 * 
 * @author rekinyz
 */
public class YahooDataFetcher implements DataFetcher {

	private static final String basicURL = "http://ichart.yahoo.com/table.csv?";
	private StringBuilder requestURL = new StringBuilder(basicURL);

	private String stockName;
	private Calendar fromDate;
	private Calendar toDate;

	public YahooDataFetcher(String stockName) {
		this.stockName = stockName;
		append(this.requestURL, "s=", stockName);
	}

	public YahooDataFetcher(String stockName, Calendar fromDate) {
		this(stockName);
		this.fromDate = fromDate;
		append(this.requestURL, "&b=", fromDate.get(Calendar.DATE));
		append(this.requestURL, "&a=", fromDate.get(Calendar.MONTH) - 1);
		append(this.requestURL, "&c=", fromDate.get(Calendar.YEAR));
	}

	public YahooDataFetcher(String stockName, Calendar fromDate, Calendar toDate) {
		this(stockName, fromDate);
		this.toDate = toDate;
		append(this.requestURL, "&e=", toDate.get(Calendar.DATE));
		append(this.requestURL, "&d=", toDate.get(Calendar.MONTH) - 1);
		append(this.requestURL, "&f=", toDate.get(Calendar.YEAR));
	}

	public String getStockName() {
		return this.stockName;
	}

	public void setStockSymbol(String stockName) {
		this.stockName = stockName;
		changeURL(stockName);
	}

	public Calendar getFromDate() {
		return this.fromDate;
	}

	public void setFromDate(Calendar fromDate) {
		this.fromDate = fromDate;
		changeURL(stockName);
	}

	public Calendar getToDate() {
		return this.toDate;
	}

	public void setToDate(Calendar toDate) {
		this.toDate = toDate;
		changeURL(stockName);
	}

	private void changeURL(String stockName) {
		this.requestURL = new StringBuilder(basicURL);
		append(this.requestURL, "s=", stockName);
		if (getFromDate() != null) {
			append(this.requestURL, "&b=", getFromDate().get(Calendar.DATE));
			append(this.requestURL, "&a=",
					getFromDate().get(Calendar.MONTH) - 1);
			append(this.requestURL, "&c=", getFromDate().get(Calendar.YEAR));
		}
		if (getToDate() != null) {
			append(this.requestURL, "&e=", getToDate().get(Calendar.DATE));
			append(this.requestURL, "&d=", getToDate().get(Calendar.MONTH) - 1);
			append(this.requestURL, "&f=", getToDate().get(Calendar.YEAR));
		}
	}

	private StringBuilder append(StringBuilder sb, String param, Object value) {
		return sb.append(param).append(value);
	}

	public static String getBasicURL() {
		return basicURL;
	}

	public String getURL() {
		return this.requestURL.toString();
	}

}

@@ -552,37 +552,32 @@
 kName;%0A%09%09append(
-this.
 requestURL, %22s=%22
@@ -706,37 +706,32 @@
 mDate;%0A%09%09append(
-this.
 requestURL, %22&b=
@@ -764,37 +764,32 @@
 ATE));%0A%09%09append(
-this.
 requestURL, %22&a=
@@ -827,37 +827,32 @@
  - 1);%0A%09%09append(
-this.
 requestURL, %22&c=
@@ -1023,37 +1023,32 @@
 oDate;%0A%09%09append(
-this.
 requestURL, %22&e=
@@ -1079,37 +1079,32 @@
 ATE));%0A%09%09append(
-this.
 requestURL, %22&d=
@@ -1140,37 +1140,32 @@
  - 1);%0A%09%09append(
-this.
 requestURL, %22&f=
@@ -1240,21 +1240,16 @@
 %09return 
-this.
 stockNam
@@ -1403,21 +1403,16 @@
 %09return 
-this.
 fromDate
@@ -1559,21 +1559,16 @@
 %09return 
-this.
 toDate;%0A
@@ -1711,21 +1711,16 @@
 me) %7B%0A%09%09
-this.
 requestU
@@ -1754,37 +1754,32 @@
 cURL);%0A%09%09append(
-this.
 requestURL, %22s=%22
@@ -1825,37 +1825,32 @@
 ll) %7B%0A%09%09%09append(
-this.
 requestURL, %22&b=
@@ -1889,37 +1889,32 @@
 TE));%0A%09%09%09append(
-this.
 requestURL, %22&a=
@@ -1963,37 +1963,32 @@
 - 1);%0A%09%09%09append(
-this.
 requestURL, %22&c=
@@ -2060,37 +2060,32 @@
 ll) %7B%0A%09%09%09append(
-this.
 requestURL, %22&e=
@@ -2122,37 +2122,32 @@
 TE));%0A%09%09%09append(
-this.
 requestURL, %22&d=
@@ -2197,21 +2197,16 @@
 %09append(
-this.
 requestU
@@ -2476,21 +2476,16 @@
 %09return 
-this.
 requestU

remove unnecessary "this."