-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCollectionFacadeSet.java
More file actions
78 lines (63 loc) · 1.85 KB
/
CollectionFacadeSet.java
File metadata and controls
78 lines (63 loc) · 1.85 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
import java.util.Collection;
import java.util.TreeSet;
/**
* Wraps an underlying Collection and serves to both simplify its API and give it a common type with the
* implemented SimpleHashSets.
* @author avishavtal
*/
public class CollectionFacadeSet implements SimpleSet {
protected Collection<String> collection;
/**
* Creates a new facade wrapping the specified collection.
*
* @param collection The Collection to wrap.
*/
public CollectionFacadeSet(Collection<String> collection) {
this.collection = collection;
deleteDuplicates();
}
/* delete the duplicates from the collection.*/
private void deleteDuplicates() {
TreeSet<String> temp = new TreeSet<>();
temp.addAll(collection);
collection.clear();
collection.addAll(temp);
}
/**
* Add a specified element to the set if it's not already in it.
*
* @param newValue New value to add to the set
* @return False iff newValue already exists in the set
*/
@Override
public boolean add(String newValue) {
return !collection.contains(newValue) && collection.add(newValue);
}
/**
* Look for a specified value in the set.
*
* @param searchVal Value to search for
* @return True iff searchVal is found in the set
*/
@Override
public boolean contains(String searchVal) {
return collection.contains(searchVal);
}
/**
* Remove the input element from the set.
*
* @param toDelete Value to delete
* @return True iff toDelete is found and deleted
*/
@Override
public boolean delete(String toDelete) {
return collection.remove(toDelete);
}
/**
* @return The number of elements currently in the set
*/
@Override
public int size() {
return collection.size();
}
}