Repository navigation
Expand file tree
/
Copy pathDirectory.java
More file actions
322 lines (295 loc) · 11.6 KB
/
Copy pathDirectory.java
File metadata and controls
322 lines (295 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
package file_system;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import constants.Constants;
import constants.Exceptions;
/**
* This class represents a virtual directory that can hold other FileSystem
* objects such as file or directory
*/
public class Directory extends FileSystemObject
implements Iterable<FileSystemObject> {
// this is a list that holds this directory's children
private final ArrayList<FileSystemObject> children;
/**
* This is an Directory Iterator
*/
public class DirectoryIterator implements Iterator<FileSystemObject> {
// this is array of objects to loop through
private final ArrayList<FileSystemObject> childrenArr;
// this is a counter
int counter;
/**
* Create directory iterator with an array of objects to loop through
* @param children array of children that can be a file or a directory
*/
public DirectoryIterator(ArrayList<FileSystemObject> children) {
this.childrenArr = children;
counter = 0;
}
@Override
public boolean hasNext() {
return counter < childrenArr.size() && childrenArr.get(counter) != null;
}
@Override
public FileSystemObject next() {
FileSystemObject fsObject = children.get(counter);
counter ++;
return fsObject;
}
}
/** Constructs a directory without a parent (root directory) */
public Directory() {
// set name and create empty list of children
super();
children = new ArrayList<>();
}
/**
* Constructs a Directory that has a name, and a parent directory
* @param name is a name of the directory
* @param parent is the parent directory of the directory
* @throws Exception if name has invalid characters in it
*/
public Directory(String name, Directory parent) throws Exception {
// assign name, and parent directory, create list of children
super(name, parent);
children = new ArrayList<>();
// add this directory as a child of parent directory
if (parent != null) {
parent.addChild(this);
}
}
/** Return children of this directory */
public ArrayList<FileSystemObject> getChildren() {
return children;
}
/** Add a child to this directory
* @throws Exception if child with that type and name already exists
*/
public void addChild(FileSystemObject child) throws Exception {
if (this.hasChildWithName(child.name)) {
throw new Exception(Exceptions.FILE_EXISTS);
}
children.add(child);
}
@Override
public String getContent() {
StringBuilder content = new StringBuilder();
for (FileSystemObject child : this) {
content.append(child.getName()).append("\n");
}
if (!content.toString().equals("")) {
content = new StringBuilder(content.substring(0, content.length() - 1));
}
return content.toString();
}
/**
* Checks if this directory has a child with given name and type
* @param childClass is the type of the child
* @param name is the given name of the child
* @return boolean that indicates if directory has child with that name
*/
public boolean hasChildWithTypeAndName(Class<?> childClass, String name) {
boolean result = false;
for (FileSystemObject currentChild : this.children) {
if (currentChild.isEqualByClassAndName(childClass, name)) {
result = true;
}
}
return result;
}
/**
* Checks if this directory has a child with given name
* @param name is the given name of the child
* @return boolean that indicates if directory has child with that name
*/
public boolean hasChildWithName(String name) {
boolean result = false;
for (FileSystemObject currentChild : this.children) {
if (name.equals(currentChild.getName())) {
result = true;
break;
}
}
return result;
}
/**
* Check if given directory has a child with given type and name
* @param currentDir is given directory
* @param classType is the given child type
* @param name is the given child name
* @return child with matching name and type
* @throws Exception if currentDir directory has no such child
*/
public static FileSystemObject findChild
(Directory currentDir, Class<?> classType, String name) throws Exception {
List<FileSystemObject> curDirChildren = currentDir.getChildren();
/*
* loop through children to find and return child directory that
* matches by name
*/
for (FileSystemObject currentChild : curDirChildren) {
if (currentChild.isEqualByClassAndName(classType, name)) {
return currentChild;
}
}
// throw exception because if method hasn't returned yet, no child matched
throw new Exception(Exceptions.WRONG_PATH_INPUT_MSG);
}
/**
* Check if given directory has a child with given name; if name is '.' refer
* to current directory, if name is '..' refer to parent directory
* @param currentDir is given directory
* @param childName is the given fileSystemObject name
* @return child with matching name
* @throws Exception if this directory has no such child
*/
public static FileSystemObject findChild
(Directory currentDir, String childName) throws Exception {
List<FileSystemObject> curDirChildren = currentDir.getChildren();
/*
* if name is '.' refer
* to current directory, if name is '..' refer to parent directory
*/
if (childName.equals(Constants.PARENT_OF_SYSTEM_FILE_INDICATOR)) {
return (currentDir.getParent() != null)
? currentDir.getParent() : currentDir;
} else if (childName.equals(Constants.CURRENT_SYSTEM_FILE_INDICATOR)) {
return currentDir;
}
/*
* else loop through children to find and return child directory
* that matches by name
*/
for (FileSystemObject currentChild : curDirChildren) {
if (currentChild.getName().equals(childName)) {
return currentChild;
}
}
// throw exception because if method hasn't returned yet, no child matched
throw new Exception(Exceptions.WRONG_PATH_INPUT_MSG);
}
/**
* Removes directory's child with current name(if present)
* @param fsObject reference child file object
*/
public void removeChild(FileSystemObject fsObject) {
children.remove(fsObject);
}
/**
* Removes directory's child with current name(if present)
* @param fsObjectName given child name
*/
public void removeChildByName(String fsObjectName) {
try {
removeChild(findChild(this, fsObjectName));
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
/**
* Check if given directory has a directory child with given name
* @param currentDir is given directory
* @param dirName is the given directory name
* @return child with matching name
* @throws Exception currentDir directory has no such child
*/
public static Directory findChildDirectory
(Directory currentDir, String dirName) throws Exception {
return (Directory) findChild(currentDir, Directory.class, dirName);
}
/**
* Check if given directory has a file child with given name
* @param currentDir is given directory
* @param fileName is the given directory name
* @return file child with matching name
* @throws Exception currentDir directory has no such child
*/
public static File findChildFile
(Directory currentDir, String fileName) throws Exception {
return (File) findChild(currentDir, File.class, fileName);
}
/**
* Find next directory by moving either backwards (to parent) or forward
* (some child directory) from given directory according to the given string
* @param currentDir is the given directory
* @param nextDirectory is string that indicates which directory to move to
* @return directory that matches nextDirectory
* @throws Exception if cannot move from this directory to directory that
* matches nextDirectory
*/
public static Directory findDirectory
(Directory currentDir, String nextDirectory) throws Exception{
Directory wantedDirectory = currentDir;
if (nextDirectory.equals(Constants.PARENT_OF_SYSTEM_FILE_INDICATOR)) {
/*
* if nextDirectory is the 'go to parent directory' indicator,
* move to current directory parent; remain in the current directory if
* it doesn't have a parent or if nextDirectory is an empty String
*/
if (currentDir.getParent() != null) {
wantedDirectory = (Directory) currentDir.getParent();
}
} else if (!nextDirectory.equals(Constants.CURRENT_SYSTEM_FILE_INDICATOR)
&& !nextDirectory.equals("")) {
/*
* else if nextDirectory isn't the 'current directory' indicator (do
* nothing if it is), check to see any child directory of current one
* matches the next directory name in the list by name
*/
wantedDirectory = findChildDirectory(currentDir, nextDirectory);
}
return wantedDirectory;
}
/**
* Find the directory starting from given directory, moving either backwards
* (to its parent) or forward (to one of its children) according to the
* given list of directory names
* @param currentDir is the starting directory
* @param listOfDirNames is list of directory names to move through
* @return directory that is there after moved through the entire list
* @throws Exception if at some point, next item on the list cannot be moved
* to from current directory
*/
public static Directory findDirectory
(Directory currentDir, List<String> listOfDirNames) throws Exception {
// move in order through each directory in the list
for (String nextDirectory: listOfDirNames) {
currentDir = findDirectory(currentDir, nextDirectory);
}
return currentDir;
}
/**
* Moves given file system object into the current directory, overwriting
* existing file in current directory (if they have the same name)
* @param fsObject the file object to be moved in this directory
*/
public void moveFsObjectIn(FileSystemObject fsObject) {
removeChildByName(fsObject.getName());
try {
fsObject.setParent(this);
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Override
public Iterator<FileSystemObject> iterator() {
return new DirectoryIterator(children);
}
/**
* Constructor for cloning purposes
*/
@Override
public FileSystemObject cloneObject(){
Directory clonedDirectory = new Directory();
try {
clonedDirectory = new Directory(name, null);
for (FileSystemObject child : this) {
FileSystemObject childClone = child.cloneObject();
childClone.setParent(clonedDirectory);
}
} catch (Exception e) {System.out.println(e.getMessage());}
return clonedDirectory;
}
}