-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEntryMap.java
98 lines (77 loc) · 2.57 KB
/
EntryMap.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
package st;
import java.util.ArrayList;
import java.util.HashSet;
public class EntryMap {
private ArrayList<Entry> entries;
private HashSet<Entry> uniqueEntries;
public EntryMap(){
entries = new ArrayList<>();
uniqueEntries = new HashSet<>();
}
public void store(String pattern, String value, Boolean caseSensitive) throws RuntimeException{
if (caseSensitive == null){
caseSensitive = Boolean.FALSE;
}
Entry entry = new Entry(pattern, value, caseSensitive);
if (!isEntryValid(entry)){
throw new RuntimeException();
}
if (isEntryUnique(entry)){
addEntry(entry);
}
}
private Boolean isEntryValid(Entry entry){
if (entry.getPattern()== null)
return Boolean.FALSE;
if (entry.getPattern().isEmpty())
return Boolean.FALSE;
if (entry.getValue() == null)
return Boolean.FALSE;
return Boolean.TRUE;
}
private Boolean isEntryUnique(Entry entry){
return !uniqueEntries.contains(entry);
}
private void addEntry(Entry entry){
entries.add(entry);
uniqueEntries.add(entry);
}
public ArrayList<Entry> getEntries() {
return entries;
}
class Entry {
String pattern;
String value;
Boolean caseSensitive;
public Entry(String pattern, String value, Boolean caseSensitive) {
this.pattern = pattern;
this.value = value;
this.caseSensitive = caseSensitive;
}
public String getPattern() {
return pattern;
}
public String getValue() {
return value;
}
public Boolean getCaseSensitive() {
return caseSensitive;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Entry entry = (Entry) o;
if (!getPattern().equals(entry.getPattern())) return false;
if (!getValue().equals(entry.getValue())) return false;
return getCaseSensitive() != null ? getCaseSensitive().equals(entry.getCaseSensitive()) : entry.getCaseSensitive() == null;
}
@Override
public int hashCode() {
int result = getPattern().hashCode();
result = 31 * result + getValue().hashCode();
result = 31 * result + (getCaseSensitive() != null ? getCaseSensitive().hashCode() : 0);
return result;
}
}
}