-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathBrowserModel.java
executable file
·145 lines (131 loc) · 3.87 KB
/
BrowserModel.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* This represents the heart of the browser: the collections
* that organize all the URLs into useful structures.
*
* @author Robert C. Duvall
*/
public class BrowserModel {
// constants
public static final String PROTOCOL_PREFIX = "http://";
// state
private URL myHome;
private URL myCurrentURL;
private int myCurrentIndex;
private List<URL> myHistory;
private Map<String, URL> myFavorites;
/**
* Creates an empty model.
*/
public BrowserModel () {
myHome = null;
myCurrentURL = null;
myCurrentIndex = -1;
myHistory = new ArrayList<>();
myFavorites = new HashMap<>();
}
/**
* Returns the first page in next history, null if next history is empty.
*/
public URL next () {
if (hasNext()) {
myCurrentIndex++;
return myHistory.get(myCurrentIndex);
}
return null;
}
/**
* Returns the first page in back history, null if back history is empty.
*/
public URL back () {
if (hasPrevious()) {
myCurrentIndex--;
return myHistory.get(myCurrentIndex);
}
return null;
}
/**
* Changes current page to given URL, removing next history.
*/
public URL go (String url) throws BrowserException{
myCurrentURL = completeURL(url);
if (myCurrentURL != null) {
if (hasNext()) {
myHistory = myHistory.subList(0, myCurrentIndex + 1);
}
myHistory.add(myCurrentURL);
myCurrentIndex++;
}
return myCurrentURL;
}
/**
* Returns true if there is a next URL available
*/
public boolean hasNext () {
return myCurrentIndex < (myHistory.size() - 1);
}
/**
* Returns true if there is a previous URL available
*/
public boolean hasPrevious () {
return myCurrentIndex > 0;
}
/**
* Returns URL of the current home page or null if none is set.
*/
public URL getHome () {
return myHome;
}
/**
* Sets current home page to the current URL being viewed.
*/
public void setHome () {
// just in case, might be called before a page is visited
if (myCurrentURL != null) {
myHome = myCurrentURL;
}
}
/**
* Adds current URL being viewed to favorites collection with given name.
*/
public void addFavorite (String name) {
// just in case, might be called before a page is visited
if (name != null && !name.equals("") && myCurrentURL != null) {
myFavorites.put(name, myCurrentURL);
}
}
/**
* Returns URL from favorites associated with given name, null if none set.
*/
public URL getFavorite (String name) {
if (name != null && !name.equals("") && myFavorites.containsKey(name)) {
return myFavorites.get(name);
}
return null;
}
// deal with a potentially incomplete URL
private URL completeURL (String possible) throws BrowserException{
try {
// try it as is
return new URL(possible);
} catch (MalformedURLException e) {
try {
// try it as a relative link
// BUGBUG: need to generalize this :(
return new URL(myCurrentURL.toString() + "/" + possible);
} catch (MalformedURLException ee) {
try {
// e.g., let user leave off initial protocol
return new URL(PROTOCOL_PREFIX + possible);
} catch (MalformedURLException eee) {
throw new BrowserException("message");
}
}
}
}
}