1 18 19 package org.apache.tools.ant.taskdefs.optional.ssh; 20 21 import java.util.ArrayList ; 22 import java.util.Iterator ; 23 import java.util.StringTokenizer ; 24 import java.io.File ; 25 26 29 public class Directory { 30 31 private File directory; 32 private ArrayList childDirectories; 33 private ArrayList files; 34 private Directory parent; 35 36 40 public Directory(File directory) { 41 this(directory, null); 42 } 43 44 49 public Directory(File directory , Directory parent) { 50 this.parent = parent; 51 this.childDirectories = new ArrayList (); 52 this.files = new ArrayList (); 53 this.directory = directory; 54 } 55 56 60 public void addDirectory(Directory directory) { 61 if (!childDirectories.contains(directory)) { 62 childDirectories.add(directory); 63 } 64 } 65 66 70 public void addFile(File file) { 71 files.add(file); 72 } 73 74 78 public Iterator directoryIterator() { 79 return childDirectories.iterator(); 80 } 81 82 86 public Iterator filesIterator() { 87 return files.iterator(); 88 } 89 90 94 public Directory getParent() { 95 return parent; 96 } 97 98 102 public boolean isRoot() { 103 return parent == null; 104 } 105 106 110 public File getDirectory() { 111 return directory; 112 } 113 114 119 public Directory getChild(File dir) { 120 for (int i = 0; i < childDirectories.size(); i++) { 121 Directory current = (Directory) childDirectories.get(i); 122 if (current.getDirectory().equals(dir)) { 123 return current; 124 } 125 } 126 127 return null; 128 } 129 130 136 public boolean equals(Object obj) { 137 if (obj == this) { 138 return true; 139 } 140 141 if (!(obj instanceof Directory)) { 142 return false; 143 } 144 145 Directory d = (Directory) obj; 146 147 return this.directory.equals(d.directory); 148 } 149 150 154 public int hashCode() { 155 return directory.hashCode(); 156 } 157 158 162 public String [] getPath() { 163 return getPath(directory.getAbsolutePath()); 164 } 165 166 172 public static String [] getPath(String thePath) { 173 StringTokenizer tokenizer = new StringTokenizer (thePath, 174 File.separator); 175 String [] path = new String [ tokenizer.countTokens() ]; 176 177 int i = 0; 178 while (tokenizer.hasMoreTokens()) { 179 path[i] = tokenizer.nextToken(); 180 i++; 181 } 182 183 return path; 184 } 185 186 190 public int fileSize() { 191 return files.size(); 192 } 193 } 194 | Popular Tags |