Reputation: 149
I have a method that takes in an ArrayList of urls. The url in each element is checked to see if it connects. If the status code is not 200, the url is added to a count as a broken link. As you can see I also print the url and the stauts code with message too.
What I would like to do is makes counts for each different status code that is not 200.
So I would have my total number of broken links and also a count for each instance of a status code that is not 200. e.g. if there are 5 http statuses of 404 they will be counted separately, as will any other status code that is not 200.
I do not want to have to hard code in every status code into my code as a count variable, I was wondering is there a way the code could create a new count variable for each different status?
Here is my current method:
public static void LinkSorter(List x){
System.out.println("processing...");
int brokenLinkCount = 0;
for(int i=0; i<x.size();i++){
try{
URL url = new URL((String) x.get(i));
URLConnection conn = url.openConnection();
conn.connect();
HttpURLConnection httpCon = (HttpURLConnection)conn;
int statusCode = httpCon.getResponseCode();
String statusMessage = httpCon.getResponseMessage();
if(statusCode != 200){
System.out.println((String) x.get(i) + statusCode + statusMessage);
brokenLinkCount++;
}
catch(MalformedURLException e) {
System.out.println("MalformedURL Exception");
System.out.println(e.getMessage());
brokenLinkCount++;
} catch(IOException e){
System.out.println("IOException");
System.out.println(e.getMessage() + "IOException");
brokenLinkCount++;
}
}
if(brokenLinkCount == 0){
System.out.println("There are no broken links.");
}else if(brokenLinkCount == 1){
System.out.println("There is 1 broken link.");
}else{
System.out.println("There are " + brokenLinkCount + " broken links.");
}
}
}
Upvotes: 0
Views: 322
Reputation: 80603
You could use a MultiMap, with the key being the status code and the values being the broken links. That pretty much automatically gets you both:
map.get(code).size()
)Pseudo-ish code:
// declare a multimap, somewhere before you need to first access it
Multimap<Integer, String> brokenLinkMap = ArrayListMultiMap.create();
// use the map, in your current if block
if(statusCode != 200){
System.out.println((String) x.get(i) + statusCode + statusMessage);
brokenLinkMap.put(statusCode, url.toExternalForm()); // <-- adding to the map!
}
// after all links have been processed, you can dump the map contents
for(final Integer key : brokenLinkMap.keys()) {
System.out.printf("Broken link count for status %s: %s\n", key, brokenLinkMap.get(key).size());
}
Alternatively, you could use all standard JDK classes:
// declare a map of lists, somewhere before you first need to access it
Map<Integer, List<String>> brokenLinkMap = new HashMap<Integer, List<String>>(4);
// use the map, in your current if block
if(statusCode != 200){
System.out.println((String) x.get(i) + statusCode + statusMessage);
if(! brokenLinkMap.containsKey(statusCode)) {
List<String> brokenLinkList = new ArrayList<String>(8);
brokenLinkMap.put(statusCode, brokenLinkList);
}
brokenLinkMap.get(statusCode).add(url.toExternalForm()); // <-- adding to the map!
}
// after all links have been processed, you can dump the map contents
for(final Integer key : brokenLinkMap.keys()) {
System.out.printf("Broken link count for status %s: %s\n", key, brokenLinkMap.get(key).size());
}
Upvotes: 1
Reputation: 158
I guess you could use java.util.Map, using the Status Code as key and the value as your counter.
Upvotes: 1