-
Notifications
You must be signed in to change notification settings - Fork 3
/
Directory.java
100 lines (81 loc) · 2.9 KB
/
Directory.java
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
/*
Copyright 2022 Massimo Santini
This file is part of "Programmazione 2 @ UniMI" teaching material.
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This material is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this file. If not, see <https://www.gnu.org/licenses/>.
*/
import java.nio.file.FileAlreadyExistsException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Objects;
/** Classe mutabile che rappresenta una <em>directory</em>. */
public class Directory extends Entry implements Iterable<Entry> {
/** Il contenuto della directory. */
private final List<Entry> entries = new ArrayList<>();
// RI: entries non è null e non contiene null, non contiene due entry con lo stesso nome
/**
* Costruisce una <em>directory</em> vuota dato il suo <em>nome</em>
*
* @param name il nome.
* @throws IllegalArgumentException se il nome è {@code null} o vuoto.
*/
public Directory(final String name) {
super(name);
}
/**
* Restituisce l'<em>entry</em> dato il suo <em>nome</em> se presente (altrimenti restituisce
* {@code null}).
*
* @param name il nome dell'entry.
* @return l'entry di dato nome (o {@code null} se nessuna entry ha il nome dato).
* @throws NullPointerException se il {@code name} è {@code null}.
*/
public Entry find(final String name) {
Objects.requireNonNull(name, "Il nome non può essere null.");
for (Entry e : entries) if (e.name.equals(name)) return e;
return null;
}
/**
* Aggiunge una <em>entry</em>.
*
* @param entry l'entry da aggiungere.
* @throws NullPointerException se l'entry è {@code null}
* @thrwos {@link FileAlreadyExistsException} se la directory contiene una entry con lo stesso
* nome di quella da aggiungere.
*/
void add(final Entry entry) throws FileAlreadyExistsException {
Objects.requireNonNull(entry, "L'entry non può essere null.");
if (find(entry.name) != null)
throw new FileAlreadyExistsException(
"La directory contiene già una entry con lo stesso nome.");
entries.add(entry);
}
@Override
public boolean isDir() {
return true;
}
@Override
public int size() {
int sum = 0;
for (Entry e : entries) sum += e.size();
return sum;
}
@Override
public String toString() {
return String.format("%s*", name);
}
@Override
public Iterator<Entry> iterator() {
return Collections.unmodifiableList(entries).iterator();
}
}