PageRenderTime 82ms CodeModel.GetById 40ms app.highlight 10ms RepoModel.GetById 31ms app.codeStats 0ms

/src/org/ooc/frontend/PathList.java

http://github.com/nddrylliog/ooc
Java | 164 lines | 84 code | 42 blank | 38 comment | 18 complexity | a1f02b9d7024a4f6af4a60823ff7f4b6 MD5 | raw file
  1package org.ooc.frontend;
  2
  3import java.io.File;
  4import java.util.ArrayList;
  5import java.util.Collection;
  6import java.util.HashMap;
  7import java.util.List;
  8
  9/**
 10 * Somehow like the 'classpath' in Java. E.g. holds where to find ooc
 11 * modules, and well, find them when asked kindly to.
 12 * 
 13 * @author Amos Wenger
 14 */
 15public class PathList {
 16	
 17	protected HashMap<String, File> paths;
 18	
 19	/**
 20	 * Default constructor
 21	 */
 22	public PathList() {
 23		
 24		paths = new HashMap<String, File> ();
 25		
 26	}
 27	
 28	/**
 29	 * Add an element to the classpath
 30	 * @param path
 31	 */
 32	public void add(String path) {
 33		
 34		File file = new File(path);
 35		if(!file.exists()) {
 36			System.err.println("Classpath element cannot be found: "+path);
 37			return;
 38		} else if(!file.isDirectory()) {
 39			System.err.println("Classpath element is not a directory: "+path);
 40			return;
 41		}
 42		
 43		String absolutePath = file.getAbsolutePath();
 44		if(!paths.containsKey(absolutePath)) {
 45			paths.put(absolutePath, file);
 46		}
 47		
 48	}
 49	
 50	/**
 51	 * Remove an element from the sourcepath
 52	 * @param path
 53	 */
 54	public void remove(String path) {
 55		
 56		File file = new File(path);
 57		if(!file.exists()) {
 58			System.err.println("Classpath element cannot be found: "+file.getPath());
 59			return;
 60		} else if(!file.isDirectory()) {
 61			System.err.println("Classpath element is not a directory: "+file.getPath());
 62			return;
 63		} else if(!paths.containsKey(file.getAbsolutePath())) {
 64			System.err.println("Trying to remove a previously unexisting element: "+file.getPath()+", ignoring.");
 65			return;
 66		}
 67		paths.remove(file.getAbsolutePath());
 68		
 69	}
 70	
 71	/**
 72	 * Remove all elements from the sourcepath
 73	 */
 74	public void clear() {
 75		
 76		paths.clear();
 77		
 78	}
 79	
 80	/**
 81	 * @return all files in the source path
 82	 */
 83	public Collection<File> getPaths() {
 84		
 85		return paths.values();
 86		
 87	}
 88	
 89	/**
 90	 * Return a list of all files found in a directory in the whole sourcepath
 91	 * @param path
 92	 * @return
 93	 */
 94	public Collection<String> getRelativePaths(String path) {
 95		
 96		List<String> files = new ArrayList<String>();
 97		
 98		for(File element: paths.values()) {
 99			File candidate = new File(element, path);
100			if(candidate.exists()) addChildren(path, files, candidate);
101		}
102		
103		return files;
104		
105	}
106
107	private void addChildren(String basePath, List<String> list, File parent) {
108		
109		File[] children = parent.listFiles();
110		for(File child: children) {
111			if(child.isFile()) {
112				list.add(basePath + '/' + child.getName());
113			} else if(child.isDirectory()) {
114				addChildren(basePath + '/' + child.getName(), list, child);
115			}
116		}
117		
118	}
119	
120	/**
121	 * Find the file in the source path and return a File object associated to it
122	 */
123	public File getFile(String path) {
124		
125		File element = getElement(path);
126		return element == null ? null : new File(element, path);
127
128	}
129	
130	/**
131	 * Find the file in the source path and return the element of the path list
132	 * it has been found in.
133	 */
134	public File getElement(String path) {
135		
136		for(File element: paths.values()) {
137			File candidate = new File(element, path);
138			if(candidate.exists()) {
139				return element;
140			}
141		}
142		
143		return null;
144
145	}
146
147	/**
148	 * @return true if the source path is empty
149	 */
150	public boolean isEmpty() {
151
152		return paths.isEmpty();
153		
154	}
155	
156	
157	@Override
158	public String toString() {
159		
160		return paths.toString();
161		
162	}
163
164}