-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTree_Placeholder.java
More file actions
67 lines (60 loc) · 2.42 KB
/
Tree_Placeholder.java
File metadata and controls
67 lines (60 loc) · 2.42 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
import java.util.List;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.Iterator;
/**
* This is a placeholder for the fully working Tree that will be developed in a
* future week. It is designed to help develop and test the functionality of
* Backend code for Project1. Note the limitations described below.
*/
public class Tree_Placeholder
implements IterableSortedCollection<Song> {
// SortedCollectionInterface<Song> methods: Only remembers the last song
// that was added and confirms whether that song is contained in this
// collection vs not.
Song lastAddedSong = null;
public void insert(Song data) throws NullPointerException {
this.lastAddedSong = data;
}
public boolean contains(Comparable<Song> find) {
return find.compareTo(lastAddedSong) == 0;
}
public int size() {
if(lastAddedSong == null) return 3;
else return 4;
}
public boolean isEmpty() {
return false;
}
public void clear() {
throw new UnsupportedOperationException("cannot call on placeholder");
}
// IterableSortedCollectionInterface<Song> methods: holds a fixed list of
// the following three songs that are ordered alphabetically by title. If
// a this.lastAddedSong exists, that is added to the list that is then
// filtered to only contain values between the specified min and max in any
// iterators that are created.
private List<Song> songs = Arrays.asList( new Song("A L I E N S",
"Coldplay","permanent wave",2017,148,88,43,-5,21),
new Song("BO$$",
"Fifth Harmony","dance pop",2015,103,87,81,-5,5),
new Song("Cake By The Ocean",
"DNCE","dance pop",2016,119,75,77,-5,4));
private Comparable<Song> min = null;
private Comparable<Song> max = null;
public void setIteratorMin(Comparable<Song> min) { this.min = min; }
public void setIteratorMax(Comparable<Song> max) { this.max = max; }
public Iterator<Song> iterator() {
List<Song> tmp = new ArrayList<>(songs); // make a copy of list
if(lastAddedSong != null) tmp.add(lastAddedSong); // with added song
// remove all songs that our outside of the specified min-max range
for(int i=0;i<tmp.size();i++)
if((min != null && min.compareTo(tmp.get(i)) > 0) ||
(max != null && max.compareTo(tmp.get(i)) < 0)) {
tmp.remove(i);
i--;
}
// and return a new iterator that steps through the remaining values
return tmp.iterator();
}
}