Skip to content

Commit

Permalink
Refactoring and Test for ClassPathTree
Browse files Browse the repository at this point in the history
  • Loading branch information
Raquel Pau committed Apr 13, 2018
1 parent 67ea64a commit ce6f96c
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@ public IndexedURLClassLoader(URL[] urls, ClassLoader parent) {
}

public List<String> getPackageClasses(String packageName) {
return ucp.listPackageContents(packageName,false);
return ucp.listPackageContents(packageName);
}

public List<String> getSDKContents(String packageName) {
return ucp.listSDKContents(packageName,false);
return ucp.listSDKContents(packageName);
}

private static URL[] getClassPathURLs() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public URL findResource(final String name, boolean check) {
}


public List<String> listPackageContents(final String packageName, boolean check) {
public List<String> listPackageContents(final String packageName) {

String packageFile = packageName.replaceAll("\\.", File.separator);
while (lastIndexed < urls.length) {
Expand All @@ -83,7 +83,7 @@ public List<String> listPackageContents(final String packageName, boolean check)
return index.list(packageFile);
}

public List<String> listSDKContents(final String packageName, boolean check) {
public List<String> listSDKContents(final String packageName) {
String packageFile = packageName.replaceAll("\\.", File.separator);
indexURLs(RT_JAR);
return index.list(packageFile);
Expand Down Expand Up @@ -155,5 +155,5 @@ private void maybeIndexResource(String relPath, URLClassPath delegate) {


// Map from resource name to URLClassPath to delegate loading that resource to.
private final ClassPathTree index = new ClassPathTree("/", "/", new HashMap<String, ClassPathTree>(), null);
private final PathTree<URLClassPath> index = new PathTree<URLClassPath>();
}
Original file line number Diff line number Diff line change
@@ -1,46 +1,47 @@
package org.walkmod.javalang.compiler.types;


import sun.misc.URLClassPath;

import java.io.File;
import java.util.*;

public class ClassPathTree {
private String key;
private Map<String, ClassPathTree> children;
private TreeValue value;
public class PathTree<T> {

public ClassPathTree(String key, String path, Map<String, ClassPathTree> children, URLClassPath value) {
this.key = key;
this.children = children;
this.value = new TreeValue(path, value);
private final Map<String, PathTree<T>> children;
private final TreeValue<T> value;

public PathTree() {
this(new HashMap<String, PathTree<T>>(), null);
}

public PathTree(Map<String, PathTree<T>> children, TreeValue<T> value) {
this.children = children;
this.value = value;
}

public void put(String key, URLClassPath value) {
insertTree(key, Arrays.asList(key.split(File.separator)), value);
public void put(String key, T value) {
insertTree(key, Arrays.asList(key.split(File.separator)), new TreeValue(key, value));
}

private void insertTree(String path, List<String> keys, URLClassPath value) {
private void insertTree(String path, List<String> keys, TreeValue<T> value) {
if (keys.isEmpty()) {
return;
}

String currentKey = keys.get(0);
ClassPathTree tree = children.get(currentKey);
PathTree tree = children.get(currentKey);

if (tree != null) {
tree.insertTree(path, keys.subList(1, keys.size()), value);
} else {
if (children.isEmpty()) {
String originalKey = this.value.path.substring(this.value.path.lastIndexOf("/") + 1);

children.put(originalKey, new ClassPathTree(
originalKey,
this.value.path,
new HashMap<String, ClassPathTree>(),
this.value.delegate));
this.value = null;
}
children.put(currentKey, new ClassPathTree(currentKey, path, new HashMap<String, ClassPathTree>(), value));
TreeValue<T> valueToInsert = null;
if (keys.size() == 1) {
valueToInsert = value;
}

PathTree<T> aux = new PathTree(new HashMap<String, PathTree>(), valueToInsert);
children.put(currentKey, aux);
aux.insertTree(path, keys.subList(1, keys.size()), value);
}
}

Expand All @@ -57,51 +58,51 @@ private List<String> findAll(List<String> keys) {
if (value != null) {
list.add(value.path);
}
Iterator<ClassPathTree> it = children.values().iterator();
Iterator<PathTree<T>> it = children.values().iterator();
while (it.hasNext()) {
ClassPathTree tree = it.next();
PathTree tree = it.next();
if (tree.value != null) {
list.add(tree.value.path);
}
}
} else {
String next = keys.get(0);
ClassPathTree tree = children.get(next);
PathTree tree = children.get(next);
if (tree != null) {
return tree.findAll(keys.subList(1, keys.size()));
}
}
return list;
}

public URLClassPath get(String key) {
public T get(String key) {
return findOne(Arrays.asList(key.split(File.separator)));
}

public boolean containsKey(String key) {
return get(key) != null;
}

private URLClassPath findOne(List<String> keys) {
private T findOne(List<String> keys) {
if (keys.isEmpty()) {
if (value != null) {
return value.delegate;
}
return null;
}
String next = keys.get(0);
ClassPathTree tree = children.get(next);
PathTree<T> tree = children.get(next);
if (tree != null) {
return tree.findOne(keys.subList(1, keys.size()));
}
return null;
}

class TreeValue {
String path;
URLClassPath delegate;
class TreeValue<T> {
final String path;
final T delegate;

public TreeValue(String path, URLClassPath delegate) {
public TreeValue(String path, T delegate) {
this.path = path;
this.delegate = delegate;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package org.walkmod.javalang.compiler.types;

import org.junit.Assert;
import org.junit.Test;

import java.io.File;
import java.util.HashMap;
import java.util.List;

public class PathTreeTest {

private static final String ROOT_KEY = "foo2";
private static final String PACKAGE = ROOT_KEY + File.separator + "bar";
private static final String HelloClassKey = PACKAGE + File.separator + "Hello.class";
private static final String ByeClassKey = PACKAGE + File.separator + "Bye.class";

@Test
public void testInsertion() {

PathTree<String> tree = createTreeWithData();

Assert.assertEquals(HelloClassKey, tree.get(HelloClassKey));
Assert.assertEquals(ByeClassKey, tree.get(ByeClassKey));

}

private PathTree<String> createTreeWithData() {
PathTree<String> tree = new PathTree<String>();

tree.put(HelloClassKey, HelloClassKey);
tree.put(ByeClassKey, ByeClassKey);
return tree;
}

@Test
public void testList() {
PathTree<String> tree = createTreeWithData();
List<String> result = tree.list(ROOT_KEY);
Assert.assertTrue(result.isEmpty());

result = tree.list(PACKAGE);
Assert.assertTrue(result.contains(HelloClassKey));
Assert.assertTrue(result.contains(ByeClassKey));

}

}

0 comments on commit ce6f96c

Please sign in to comment.