This is a popular question in machine coding rounds. The requirements are to build a URL shortener. There are certain registered users who have the capability to define a URL and an optional time to live. The system generates a shortened URL. The unregistered users can also generate a shortened URL with a predefined TTL. This is the URLshortener Class.
package urlshortener;
import hashalgorithm.IHashStrategy;
import java.util.*;
public class UrlShortener {
private IHashStrategy iHashStrategy;
private Map<String, List<Url>> userToUrl;
private List<String> registeredUser;
static final int DEFAULT_TIME_TO_LIVE_IN_MILLIS = 500000;
public UrlShortener(IHashStrategy instanceOfConcreteHashStrategy) {
iHashStrategy = instanceOfConcreteHashStrategy;
userToUrl = new HashMap<>();
registeredUser = new ArrayList<>();
registeredUser.add("abc");
registeredUser.add("bad");
registeredUser.add("bac");
registeredUser.add("lom");
registeredUser.add("DEFAULT");
}
public List<Url> getListOfShortenedUrlForUser(String userId) {
List<Url> urlList = getUrls(userId);
userToUrl.put(userId,urlList);
return urlList;
}
public void addNewEntryForUser(String url,String userId) {
addNewEntryForUserWithTTL(url,userId,DEFAULT_TIME_TO_LIVE_IN_MILLIS);
}
private boolean checkExistenceForShortenedUrl(String shortenedUrl) {
for(Map.Entry<String,List<Url>> entry : userToUrl.entrySet()) {
List<Url> urlList = entry.getValue();
for(int i = 0 ; i < urlList.size() ; i++) {
if(urlList.get(i).getShortenedUrl().equalsIgnoreCase(shortenedUrl))
return true;
}
}
return false;
}
public void addNewEntryForUserWithTTL(String url,String userId,int timeToLive) {
if(!registeredUser.contains(userId)) {
System.out.println("userId is not registered. Treated as default!");
userId = "DEFAULT";
}
List<Url> urlList = userToUrl.get(userId);
String shortenedUrl = iHashStrategy.getHashFromUrl(url);
while(checkExistenceForShortenedUrl(shortenedUrl)) {
shortenedUrl = iHashStrategy.getHashFromUrl(url);
}
System.out.println(shortenedUrl);
Url urlToInsert = new Url(url,shortenedUrl,timeToLive,new Date());
if(urlList == null)
urlList = new ArrayList<>();
urlList.add(urlToInsert);
userToUrl.put(userId,urlList);
}
public String getShortenedUrlForUser(String url,String userId) {
if(!registeredUser.contains(userId)) {
System.out.println("userId is not registered. Treated as default!");
userId = "DEFAULT";
}
List<Url> urlList = getUrls(userId);
for(int i = 0 ; i < urlList.size(); i++) {
if(urlList.get(i).getOriginalUrl().equalsIgnoreCase(url))
return urlList.get(i).getShortenedUrl();
}
return null;
}
private List<Url> getUrls(String userId) {
List<Url> urlList = userToUrl.get(userId);
for (int i = 0; i < urlList.size(); i++) {
Url currentUrl = urlList.get(i);
if (!currentUrl.isValid()) {
urlList.remove(i);
}
}
return urlList;
}
public String getLongUrlForUser(String shortUrl,String userId) {
List<Url> urlList = getUrls(userId);
for(int i = 0 ; i < urlList.size(); i++) {
if(urlList.get(i).getShortenedUrl().equalsIgnoreCase(shortUrl));
return urlList.get(i).getOriginalUrl();
}
return null;
}
}
And this is URL.java
package urlshortener;
import java.util.Date;
public class Url {
private String originalUrl;
private String shortenedUrl;
private int timeToLive;
private Date date;
public Url(String originalUrl, String shortenedUrl, int timeToLive, Date date) {
this.setShortenedUrl(shortenedUrl);
this.setTimeToLive(timeToLive);
this.setOriginalUrl(originalUrl);
this.setDate(date);
}
public boolean isValid() {
Date timeNow = new Date();
Date expireDate = new Date(this.getDate().getTime() + getTimeToLive());
return timeNow.before(expireDate);
}
public String getOriginalUrl() {
return originalUrl;
}
public void setOriginalUrl(String originalUrl) {
this.originalUrl = originalUrl;
}
public String getShortenedUrl() {
return shortenedUrl;
}
public void setShortenedUrl(String shortenedUrl) {
this.shortenedUrl = shortenedUrl;
}
public int getTimeToLive() {
return timeToLive;
}
public void setTimeToLive(int timeToLive) {
this.timeToLive = timeToLive;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
For the rest of the code please refer to https://github.com/suryasis-hub/urlshortener/tree/master/src
Date
class , some reason to not use java time API ? \$\endgroup\$