forked from RLewis627/cs_4990_PageRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SubRedditNode.java
68 lines (60 loc) · 2.44 KB
/
SubRedditNode.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
import java.util.ArrayList;
import java.util.List;
public class SubRedditNode {
private List<String> linksToSubreddits = new ArrayList<>();
private List<String> linksFromSubreddits = new ArrayList<>();
private String subredditName;
private int numOfOutlinks;
private int numOfInLinks;
SubRedditNode(String name, List<String> links){
subredditName = name;
LinksToOtherSubreddits(links);
}
public void setlinksFromSubreddits(List<String> internalList){
linksFromSubreddits = internalList;
numOfInLinks = internalList.size();
}
public void setnumOfInLinks(int i) {numOfInLinks = i;}
public String getsubredditName() {return subredditName;}
public List<String> getlinksToSubreddits(){return linksToSubreddits;}
public int getnumOfInLinks() {return numOfInLinks;}
public void addInLinkSubreddit(String s) {
linksFromSubreddits.add(s);
}
private void LinksToOtherSubreddits(List<String> links) {
String nextUrl = null;
boolean isValidSubreddit;
while(!links.isEmpty()) {
do {
isValidSubreddit = true;
if(!links.isEmpty()){nextUrl = links.remove(0);}else {break;}
if(nextUrl.length() < 25) {isValidSubreddit = false;}
else if(!nextUrl.substring(0, 25).equals("https://www.reddit.com/r/")){isValidSubreddit = false;}
else if(nextUrl.indexOf("/",25) == -1){isValidSubreddit = false;}
else if(subredditName.equals(nextUrl.substring(25,nextUrl.indexOf("/",25)))){isValidSubreddit = false;}
else {
String Name = nextUrl.substring(25,nextUrl.indexOf("/",25));
for(String subreddit : linksToSubreddits) {
if(Name.equals(subreddit)) {isValidSubreddit = false;}
}
}
} while (isValidSubreddit == false);
if(!links.isEmpty()) {
linksToSubreddits.add(nextUrl.substring(25, nextUrl.indexOf("/",25)));
numOfOutlinks++;
}
}
}
public void printLinksToSubreddits() {
System.out.println("There are "+numOfOutlinks+" subreddits that "+subredditName+" links to");
if(numOfOutlinks != 0) {
for(String subreddit : linksToSubreddits) {System.out.printf(subreddit+" ");}
}
}
public void printLinksFromSubreddits() {
System.out.println("There are "+numOfInLinks+" subreddits that link to "+subredditName);
if(numOfInLinks != 0) {
for(String subreddit : linksFromSubreddits) {System.out.printf(subreddit+" ");}
}
}
}