Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save francoisneron/e3e4c56c70add7f09bed to your computer and use it in GitHub Desktop.
Save francoisneron/e3e4c56c70add7f09bed to your computer and use it in GitHub Desktop.
Project for interview with CLX networks
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<option name="DEFAULT_COMPILER" value="Javac" />
<resourceExtensions />
<wildcardResourcePatterns>
<entry name="!?*.java" />
<entry name="!?*.form" />
<entry name="!?*.class" />
<entry name="!?*.groovy" />
<entry name="!?*.scala" />
<entry name="!?*.flex" />
<entry name="!?*.kt" />
<entry name="!?*.clj" />
</wildcardResourcePatterns>
<annotationProcessing>
<profile default="true" name="Default" enabled="false">
<processorPath useClasspath="true" />
</profile>
</annotationProcessing>
</component>
</project>
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding" useUTFGuessing="true" native2AsciiForPropertiesFiles="false" />
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="EntryPointsManager">
<entry_points version="2.0" />
</component>
<component name="ProjectKey">
<option name="state" value="project://63537948-39a4-48a0-9c97-34259a0fa913" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_7" assert-keyword="true" jdk-15="true" project-jdk-name="IntelliJ IDEA IU-141.2735.5" project-jdk-type="IDEA JDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/InterviewUrlShortener.iml" filepath="$PROJECT_DIR$/InterviewUrlShortener.iml" />
</modules>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Palette2">
<group name="Swing">
<item class="com.intellij.uiDesigner.HSpacer" tooltip-text="Horizontal Spacer" icon="/com/intellij/uiDesigner/icons/hspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="1" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="com.intellij.uiDesigner.VSpacer" tooltip-text="Vertical Spacer" icon="/com/intellij/uiDesigner/icons/vspacer.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="1" anchor="0" fill="2" />
</item>
<item class="javax.swing.JPanel" icon="/com/intellij/uiDesigner/icons/panel.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3" />
</item>
<item class="javax.swing.JScrollPane" icon="/com/intellij/uiDesigner/icons/scrollPane.png" removable="false" auto-create-binding="false" can-attach-label="true">
<default-constraints vsize-policy="7" hsize-policy="7" anchor="0" fill="3" />
</item>
<item class="javax.swing.JButton" icon="/com/intellij/uiDesigner/icons/button.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="0" fill="1" />
<initial-values>
<property name="text" value="Button" />
</initial-values>
</item>
<item class="javax.swing.JRadioButton" icon="/com/intellij/uiDesigner/icons/radioButton.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="RadioButton" />
</initial-values>
</item>
<item class="javax.swing.JCheckBox" icon="/com/intellij/uiDesigner/icons/checkBox.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="3" anchor="8" fill="0" />
<initial-values>
<property name="text" value="CheckBox" />
</initial-values>
</item>
<item class="javax.swing.JLabel" icon="/com/intellij/uiDesigner/icons/label.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="8" fill="0" />
<initial-values>
<property name="text" value="Label" />
</initial-values>
</item>
<item class="javax.swing.JTextField" icon="/com/intellij/uiDesigner/icons/textField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JPasswordField" icon="/com/intellij/uiDesigner/icons/passwordField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JFormattedTextField" icon="/com/intellij/uiDesigner/icons/formattedTextField.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1">
<preferred-size width="150" height="-1" />
</default-constraints>
</item>
<item class="javax.swing.JTextArea" icon="/com/intellij/uiDesigner/icons/textArea.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTextPane" icon="/com/intellij/uiDesigner/icons/textPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JEditorPane" icon="/com/intellij/uiDesigner/icons/editorPane.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JComboBox" icon="/com/intellij/uiDesigner/icons/comboBox.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="2" anchor="8" fill="1" />
</item>
<item class="javax.swing.JTable" icon="/com/intellij/uiDesigner/icons/table.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JList" icon="/com/intellij/uiDesigner/icons/list.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="2" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTree" icon="/com/intellij/uiDesigner/icons/tree.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3">
<preferred-size width="150" height="50" />
</default-constraints>
</item>
<item class="javax.swing.JTabbedPane" icon="/com/intellij/uiDesigner/icons/tabbedPane.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSplitPane" icon="/com/intellij/uiDesigner/icons/splitPane.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="3" hsize-policy="3" anchor="0" fill="3">
<preferred-size width="200" height="200" />
</default-constraints>
</item>
<item class="javax.swing.JSpinner" icon="/com/intellij/uiDesigner/icons/spinner.png" removable="false" auto-create-binding="true" can-attach-label="true">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSlider" icon="/com/intellij/uiDesigner/icons/slider.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="8" fill="1" />
</item>
<item class="javax.swing.JSeparator" icon="/com/intellij/uiDesigner/icons/separator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="6" anchor="0" fill="3" />
</item>
<item class="javax.swing.JProgressBar" icon="/com/intellij/uiDesigner/icons/progressbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1" />
</item>
<item class="javax.swing.JToolBar" icon="/com/intellij/uiDesigner/icons/toolbar.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="6" anchor="0" fill="1">
<preferred-size width="-1" height="20" />
</default-constraints>
</item>
<item class="javax.swing.JToolBar$Separator" icon="/com/intellij/uiDesigner/icons/toolbarSeparator.png" removable="false" auto-create-binding="false" can-attach-label="false">
<default-constraints vsize-policy="0" hsize-policy="0" anchor="0" fill="1" />
</item>
<item class="javax.swing.JScrollBar" icon="/com/intellij/uiDesigner/icons/scrollbar.png" removable="false" auto-create-binding="true" can-attach-label="false">
<default-constraints vsize-policy="6" hsize-policy="0" anchor="0" fill="2" />
</item>
</group>
</component>
</project>
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="" />
</component>
</project>
#Fri Oct 16 18:37:00 EDT 2015
LAZteT=http\://google.com/
GJ46E5=http\://www.bob.com/
WSFWHY=http\://www.google.com/
w51t9Y=http\://stackoverflow.com/
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>
package main.shorty.Application;
import main.shorty.business.URLShortyFile;
import main.shorty.domain.URLShorty;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.HashMap;
/*
* TODO: Add database interactions.
* TODO: Probably a good thing to do webservice. (Add new url return shorten url)
* TODO: Finish tests.
* TODO: Better exceptions handling.
*/
public class ShortyApplication {
public static void main(String[] args) throws MalformedURLException, IOException {
URLShortyFile file = new URLShortyFile();
HashMap<String,String> mostRecentKeyToValueMap = file.loadAll();
URLShorty u = new URLShorty("http://cl.ip/", mostRecentKeyToValueMap);
String addUrl = "http://www.bob.com/";
System.out.println("URL:" + addUrl + "\tShortened: " + u.getShortURL(addUrl) + "\tExpanded: " + u.getLongURL(u.getShortURL(addUrl)));
u.printAllCurrentKeyToUrlMap();
file.saveAll(u.getCurrentKeyToUrlMap());
}
}
package main.shorty.business;
import java.io.IOException;
import java.util.HashMap;
/**
* TODO: Class for db interactions.
*/
public class URLShortyDao implements URLShortyService{
@Override
public void save(String key, String longURL) {
}
@Override
public void delete(String key) {
}
@Override
public void getKey(String longURL) {
}
@Override
public void getValue(String key) {
}
@Override
public void saveAll(HashMap<String, String> keyToLongURL) throws IOException {
}
@Override
public HashMap<String, String> loadAll() throws IOException {
return null;
}
}
package main.shorty.business;
import java.io.*;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
* Interview purpose.
*/
public class URLShortyFile implements URLShortyService {
private HashMap<String, String> keyToLongURL;
Properties prop;
OutputStream output = null;
InputStream input = null;
public URLShortyFile() throws FileNotFoundException {
keyToLongURL = new HashMap<String, String>();
prop = new Properties();
}
@Override
public void save(String key, String longURL) {
}
@Override
public void delete(String key) {
}
@Override
public void getKey(String longURL) {
}
@Override
public void getValue(String key) {
}
@Override
public void saveAll(HashMap<String, String> keyToLongURL) throws IOException {
System.out.println("Saving all new shorten urls...");
output = new FileOutputStream("config.properties");
for (Map.Entry<String, String> e : keyToLongURL.entrySet()) {
String key = e.getKey();
String value = e.getValue();
if(prop.getProperty(key,value) != null)
prop.setProperty(key, value);
}
prop.store(output, null);
output.close();
}
@Override
public HashMap<String, String> loadAll() throws IOException {
System.out.println("Loading all previous shorten urls...");
HashMap<String, String> keyToValueMap = new HashMap<String, String>();
input = new FileInputStream("config.properties");
prop.load(input);
Enumeration<?> e = prop.propertyNames();
while (e.hasMoreElements()) {
String key = (String) e.nextElement();
String value = prop.getProperty(key);
keyToValueMap.put(key, value);
}
input.close();
return keyToValueMap;
}
}
package main.shorty.business;
import java.io.IOException;
import java.util.HashMap;
/**
* Created by NERF on 16/10/2015.
*/
public interface URLShortyService {
/* if we want to use a db. Some refactors will be needed in the
* URLShorty class
* */
public void save(String key, String longURL);
public void delete(String key);
public void getKey(String longURL);
public void getValue(String key);
/* Mostly to use with file */
public void saveAll(HashMap<String, String> keyToLongURL) throws IOException;
public HashMap<String, String> loadAll() throws IOException;
}
package main.shorty.domain;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.Random;
/**
* Created by NERF on 16/10/2015.
*/
public class URLShorty {
/*
Container for key/value shortURL/LongUrl
*/
private HashMap<String, String> keyToUrlMap;
// TODO: Add copy of the hashMap but inverted.
/*
Set current domain.
*/
private String domain;
/*
Set of characters to be used to generate short url.
*/
private static final char characters[] = new char[]{'0','1','2','3','4','5','6','7','8','9',
'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
/*
Instantiate random to generate unique key.
*/
private Random random;
/*
Max key length.
*/
private static final int MAXIMUM_KEY_LENGTH = 5;
/**
* Constructor.
*/
public URLShorty() {
random = new Random();
}
/**
* Constructor.
* @param domainURL
*/
public URLShorty(String domainURL, HashMap<String, String> currentPairs) throws MalformedURLException {
this();
this.keyToUrlMap = currentPairs;
if(!domainURL.isEmpty()) {
this.domain = getDomainFromUrl(domainURL);
}
}
/**
* Return the short url (aka Key) with domain in it from hashMap.
*
* @param longURL
* @return short url
*/
public String getShortURL(String longURL) {
String shortURL = "";
// TODO: Add a url validation
if(keyToUrlMap.containsValue(longURL)) {
shortURL = domain + "/" + getKeyFromValue(longURL);
}
else {
shortURL = domain + "/" + getKey(longURL);
}
return shortURL;
}
/**
* Return the long url (aka Value) from hashMap.
*
* @param shortURL
* @return long url
*/
public String getLongURL(String shortURL) {
String key = shortURL.substring(domain.length() +1);
String exception = "There is no value saved under this key.";
if(keyToUrlMap.get(key) == null)
return exception;
else
return keyToUrlMap.get(key);
}
/**
* Generate a 5 digit unique key from the array of characters.
*
* @return key
*/
private String generateShortURL(){
boolean flag = true;
String key = "";
while(flag) {
for(int i = 0; i <= MAXIMUM_KEY_LENGTH; i++) {
key += characters[random.nextInt(characters.length)];
}
if(!keyToUrlMap.containsKey(key)) {
flag = false;
}
}
return key;
}
/**
* Retrieve the key associated with the value (longURL). This is not
* great... what is the point of using hashMap? Might be faster to
* have a second hashMap and invert key/value.
*
* @param longURL
* @return key
*/
private String getKeyFromValue(String longURL) {
for (String entry: keyToUrlMap.keySet()){
String key =entry.toString();
String value = keyToUrlMap.get(entry).toString();
if(longURL.equals(value)) {
return key;
}
}
return null;
}
/**
* Return the key generated. Also, add the newly created unique key
* paired with his value (long url).
*
* @param longURL
* @return key
*/
private String getKey(String longURL) {
String key = generateShortURL();
keyToUrlMap.put(key, longURL);
return key;
}
/**
* Return only the domain from the complete url (clean).
*
* @param domainURL
* @return domain
*/
private String getDomainFromUrl(String domainURL) throws MalformedURLException {
URL url = new URL(domainURL);
return url.getHost();
}
/**
* Getter. (to be able to save in file, batch job like)
*/
public HashMap<String, String> getCurrentKeyToUrlMap() {
return keyToUrlMap;
}
/**
* Print current hash map. (debug purpose)
*/
public void printAllCurrentKeyToUrlMap() {
for (String entry: keyToUrlMap.keySet()){
String key =entry.toString();
String value = keyToUrlMap.get(entry).toString();
System.out.println("Key: " + key + "\tValue: " + value + "\tShortened: " + getShortURL(value) + "\tExpanded: " + getLongURL(getShortURL(value)) );
}
}
}
package test;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* TODO: Test URLShortyFile class.
*/
public class URLShortyFileTest {
@Test
public void testSaveAll() throws Exception {
}
@Test
public void testLoadAll() throws Exception {
}
}
package test;
import org.junit.Test;
import static org.junit.Assert.*;
/**
* TODO: Test URLShorty class.
*/
public class URLShortyTest {
@Test
public void testGetShortURL() throws Exception {
}
@Test
public void testGetLongURL() throws Exception {
}
@Test
public void testGetCurrentKeyToUrlMap() throws Exception {
}
@Test
public void testPrintAllCurrentKeyToUrlMap() throws Exception {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment