-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimpleLocalCacheUsingTrie.java
More file actions
160 lines (140 loc) · 3.42 KB
/
SimpleLocalCacheUsingTrie.java
File metadata and controls
160 lines (140 loc) · 3.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
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
import java.util.regex.Pattern;
/**
* @author yeddulamanjeersrujan
*
* May 31, 2019
*
* 1) This class is not thread safe
*
* 2) Works only for code, country.
*
* 3) We can add more fields in AirPort class and return Airport(It
* doesn't have public setters) object instead of String to handle more
* fields
*
* 4) Code should be as length 3 and only alphabets
*
* 5) Didn't add delete method
*
* 6) Validations are simple. If we need more validations and the code
* should be more abstracted
*
*
*
*/
public class SimpleLocalCacheUsingTrie {
private Trie cache = new Trie(null);
private Pattern airportCodePattern = Pattern.compile("[a-zA-Z]");
private static SimpleLocalCacheUsingTrie instance = null;
/**
*
*/
private SimpleLocalCacheUsingTrie() {
super();
}
/**
* @author yeddulamanjeersrujan
*
* May 31, 2019
*
*/
private class Airport {
public Airport(String code, String country) {
super();
this.code = code;
this.country = country;
}
public String getCode() {
return code;
}
public String getCountry() {
return country;
}
private String code;
private String country;
}
/**
* @author yeddulamanjeersrujan
*
* May 31, 2019
*
* The trie node
*
*/
private class Trie {
public Trie(Airport ap) {
super();
this.ch = new Trie[26];
this.ap = ap;
}
Trie[] ch = null;
Airport ap;
}
/**
* @return
*
* Abstract method to get singleton object
*/
public static SimpleLocalCacheUsingTrie getInstance() {
if (instance == null) {
synchronized (SimpleLocalCacheUsingTrie.class) {
if (instance == null) {
instance = new SimpleLocalCacheUsingTrie();
}
}
}
return instance;
}
/**
* @param code
* @param country
*/
protected void insertAirport(String code, String country) {
if (code == null || code.length() != 3 || !airportCodePattern.matcher(code).find()) {
throw new IllegalArgumentException("Code should be non-null, of length 3 and only alphabets");
}
int i = 0;
Trie cur = cache;
code = code.toUpperCase();
while (i < code.length()) {
if (cur.ch[code.charAt(i) - 'A'] == null) {
cur.ch[code.charAt(i) - 'A'] = new Trie(null);
}
cur = cur.ch[code.charAt(i) - 'A'];
i++;
}
cur.ap = new Airport(code, country);
}
/**
* @param code
* @return
*/
public String getCountry(String code) {
if (code == null || code.length() != 3 || !airportCodePattern.matcher(code).find()) {
throw new IllegalArgumentException("Code should be non-null, of length 3 and only alphabets");
}
int i = 0;
Trie cur = cache;
code = code.toUpperCase();
while (i < code.length()) {
if (cur.ch[code.charAt(i) - 'A'] == null) {
return null;
}
cur = cur.ch[code.charAt(i) - 'A'];
i++;
}
return (cur.ap != null && cur.ap.getCode() != null && cur.ap.getCode().equals(code)) ? cur.ap.getCountry()
: null;
}
public static void main(String[] args) {
SimpleLocalCacheUsingTrie sc = SimpleLocalCacheUsingTrie.getInstance();
sc.insertAirport("abc", "abcCountry");
sc.insertAirport("bcd", "bcdCountry");
sc.insertAirport("xyz", "xyzCountry");
System.out.println(sc.getCountry("abc"));
System.out.println(sc.getCountry("bcd"));
System.out.println(sc.getCountry("xyz"));
System.out.println(sc.getCountry("xyc"));
System.out.println(sc.getCountry("pqr"));
}
}