Skip to content

Commit

Permalink
Use hashmap instead of having wrapping class. Remove misc browser unu…
Browse files Browse the repository at this point in the history
…sed code.
  • Loading branch information
yl-coder committed Jun 10, 2016
1 parent 5d6acc5 commit 6ccd89a
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 48 deletions.
25 changes: 0 additions & 25 deletions src/main/java/address/image/CachedImage.java

This file was deleted.

17 changes: 8 additions & 9 deletions src/main/java/address/image/ImageManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,7 @@

import javafx.scene.image.Image;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.HashMap;

/**
* An Image Manager that manages the remote images.
Expand All @@ -16,10 +14,11 @@
public class ImageManager {

private static ImageManager instance;
List<CachedImage> imageList;

HashMap<String, Image> imageHashMap;

private ImageManager(){
imageList = new ArrayList<>();
imageHashMap = new HashMap<>();
}

public static ImageManager getInstance(){
Expand All @@ -35,14 +34,14 @@ public static ImageManager getInstance(){
* @return The graphical view of the image.
*/
public synchronized Image getImage(String url){
Optional<CachedImage> cachedImage = imageList.stream().filter(img -> img.getUrl().equals(url)).findAny();
Image cachedImage = imageHashMap.get(url);

if (cachedImage.isPresent()) {
return cachedImage.get().getImage();
if (cachedImage != null) {
return cachedImage;
}

Image newImage = new Image(url);
imageList.add(new CachedImage(newImage, url));
imageHashMap.put(url, newImage);
return newImage;
}
}
17 changes: 3 additions & 14 deletions src/main/java/address/util/UrlUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,21 +10,10 @@
*/
public class UrlUtil {

public static String getBaseUrl(String url) {
URL parsedUrl = null;
String parsedString = null;
try {
parsedUrl = new URL(url);
parsedString = parsedUrl.toURI().normalize().toASCIIString();
} catch (MalformedURLException e) {
return "";
} catch (URISyntaxException e) {
return "";
}
return parsedString;
}

public static boolean compareBaseUrls(URL url1, URL url2) {
if (url1 == null || url2 == null) {
return false;
}
return url1.getHost().equals(url2.getHost()) && url1.getPath().equals(url2.getPath());
}

Expand Down

0 comments on commit 6ccd89a

Please sign in to comment.