Skip to content

Instantly share code, notes, and snippets.

@lkallas
Created April 27, 2016 11:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lkallas/9ad025d06bfb76c96ef16d59d3800c73 to your computer and use it in GitHub Desktop.
Save lkallas/9ad025d06bfb76c96ef16d59d3800c73 to your computer and use it in GitHub Desktop.
Small JAVA class for comapring URL-s.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* Simple class for comparing URL-s with HTTP/HTTPS protocol.
*
* @author Lennar Kallas
*/
public class URLcompare {
/**
* Gets the base URL or domain of the URL. Everything that comes before the
* first occurrence of the '?' char, which indicates the start of the
* parameters.
*
* @param url URL as string.
* @return Base URL.
*/
private String getBaseURL(String url) {
if (url != null && !url.trim().isEmpty()) {
return url.substring(0, url.contains("?") ? url.indexOf('?') : url.length());
}
return null;
}
/**
* Gets the List of URL parameters.
*
* @param url URL as string.
* @return List with URL parameters.
*/
private List<String> getURLparams(String url) {
if (url != null && !url.trim().isEmpty()) {
return Arrays.asList(url.substring(url.indexOf('?') + 1).split("&"));
}
return null;
}
/**
* Print and get differences of the URL-s.
*
* @param list1 List with URL parameters.
* @param list2 List with URL parameters.
* @return List with differences.
*/
private List<String> getParamsDiff(List<String> list1, List<String> list2) {
List<String> missingItems = new ArrayList<>(1);
// Start comparing (take the bigger collection as master collection, if equal take the first param)
for (String item : list1.size() >= list2.size() ? list1 : list2) {
if (!(list1.size() >= list2.size() ? list2.contains(item) : list1.contains(item))) {
System.err.println("Diff: " + item);
missingItems.add(item);
}
}
return missingItems;
}
/**
* Builds URL with alphabetically sorted parameters.
*
* @param listOfParams List with URL parameters.
* @return URL with alphabetically ordered parameters.
*/
private String buildSortedParamURL(List<String> listOfParams) {
String baseUrl = null;
StringBuilder sb = new StringBuilder();
for (String param : listOfParams) {
if (param.contains("http://") || param.contains("https://")) {
baseUrl = param + (listOfParams.size() > 1 ? "?" : "");
} else {
if (sb.length() > 1) {
sb.append('&');
}
sb.append(param);
}
}
return baseUrl + sb.toString();
}
/**
* Takes care of the URL comparing.
*
* @param url1 URL to compare with another.
* @param url2 URL to compare with another.
* @return true if URL are identical, false otherwise.
*/
private boolean compareURL(String url1, String url2) {
// Create lists that will contain parameters, base url etc
List<String> items1 = new ArrayList<>(1);
List<String> items2 = new ArrayList<>(1);
// Get base URL-s
String baseURL1 = getBaseURL(url1);
String baseURL2 = getBaseURL(url2);
// Compare base URL-s
if (!baseURL1.equals(baseURL2)) {
System.err.println("Base URL-s (domains) don't match!");
return false;
}
// Add base URL
items1.add(getBaseURL(url1));
items2.add(getBaseURL(url2));
// Add parameters found from URL
items1.addAll(getURLparams(url1));
items2.addAll(getURLparams(url2));
// Sort lists
Collections.sort(items1);
Collections.sort(items2);
String sortedParamURL1 = buildSortedParamURL(items1);
String sortedParamURL2 = buildSortedParamURL(items2);
System.err.println("URL-s with sorted parameters: \n\n\tURL 1: "
+ sortedParamURL1
+ "\n\tURL 2: " + sortedParamURL2 + "\n");
// Compare the length of the lists
if (items1.size() != items2.size()) {
System.err.println("Length of the URL parameters don't match!");
getParamsDiff(items1, items2);
return false;
}
// Check the length of the URL-s
if (sortedParamURL1.length() != sortedParamURL2.length()) {
System.err.println("Length of the URL-s don't match!");
getParamsDiff(items1, items2);
return false;
}
// Check if the URL-s are equal
if (!sortedParamURL1.equals(sortedParamURL2)) {
System.err.println("URL-s don't match!");
getParamsDiff(items1, items2);
return false;
}
if (!items1.containsAll(items2)) {
System.err.println("URL parameters don't match!");
getParamsDiff(items1, items2);
return false;
}
System.err.println("The URL-s are identical.");
return true;
}
public static void main(String[] args) {
URLcompare compare = new URLcompare();
// Compare different URL-s
String diff1 = "http://www.mydomain.com/someservice?username=Jane&id=1&status=OK";
String diff2 = "http://www.mydomain.com/someservice?username=John&id=2&status=FAILED";
compare.compareURL(diff1, diff2);
// Compare identical URL-s
String actualURL = "http://www.mydomain.com/someservice?username=Jane&id=1&status=OK";
String builtURL = "http://www.mydomain.com/someservice?username=Jane&id=1&status=OK";
compare.compareURL(actualURL, builtURL);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment