Skip to content

Commit aff7ef8

Browse files
committed
Fix last errors in checkstyle.
1 parent c150e5f commit aff7ef8

File tree

10 files changed

+249
-65
lines changed

10 files changed

+249
-65
lines changed

caching/src/main/java/com/iluwatar/caching/CachingPolicy.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,25 @@
3232
@AllArgsConstructor
3333
@Getter
3434
public enum CachingPolicy {
35+
/**
36+
* Through.
37+
*/
3538
THROUGH("through"),
39+
/**
40+
* AROUND.
41+
*/
3642
AROUND("around"),
43+
/**
44+
* BEHIND.
45+
*/
3746
BEHIND("behind"),
47+
/**
48+
* ASIDE.
49+
*/
3850
ASIDE("aside");
3951

52+
/**
53+
* Policy value.
54+
*/
4055
private final String policy;
4156
}

caching/src/main/java/com/iluwatar/caching/LruCache.java

Lines changed: 93 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,78 @@
3030
import lombok.extern.slf4j.Slf4j;
3131

3232
/**
33-
* Data structure/implementation of the application's cache. The data structure consists of a hash
34-
* table attached with a doubly linked-list. The linked-list helps in capturing and maintaining the
35-
* LRU data in the cache. When a data is queried (from the cache), added (to the cache), or updated,
36-
* the data is moved to the front of the list to depict itself as the most-recently-used data. The
37-
* LRU data is always at the end of the list.
33+
* Data structure/implementation of the application's cache. The data structure
34+
* consists of a hash table attached with a doubly linked-list. The linked-list
35+
* helps in capturing and maintaining the LRU data in the cache. When a data is
36+
* queried (from the cache), added (to the cache), or updated, the data is
37+
* moved to the front of the list to depict itself as the most-recently-used
38+
* data. The LRU data is always at the end of the list.
3839
*/
3940
@Slf4j
4041
public class LruCache {
41-
42+
/**
43+
* Static class Node.
44+
*/
4245
static class Node {
43-
String userId;
44-
UserAccount userAccount;
45-
Node previous;
46-
Node next;
47-
48-
public Node(String userId, UserAccount userAccount) {
49-
this.userId = userId;
50-
this.userAccount = userAccount;
46+
/**
47+
* user id.
48+
*/
49+
private final String userId;
50+
/**
51+
* User Account.
52+
*/
53+
private UserAccount userAccount;
54+
/**
55+
* previous.
56+
*/
57+
private Node previous;
58+
/**
59+
* next.
60+
*/
61+
private Node next;
62+
63+
/**
64+
* Node definition.
65+
* @param id String
66+
* @param account {@link UserAccount}
67+
*/
68+
Node(final String id, final UserAccount account) {
69+
this.userId = id;
70+
this.userAccount = account;
5171
}
5272
}
5373

54-
int capacity;
55-
Map<String, Node> cache = new HashMap<>();
56-
Node head;
57-
Node end;
74+
/**
75+
* Capacity of Cache.
76+
*/
77+
private int capacity;
78+
/**
79+
* Cache {@link HashMap}.
80+
*/
81+
private Map<String, Node> cache = new HashMap<>();
82+
/**
83+
* Head.
84+
*/
85+
private Node head;
86+
/**
87+
* End.
88+
*/
89+
private Node end;
5890

59-
public LruCache(int capacity) {
60-
this.capacity = capacity;
91+
/**
92+
* Constructor.
93+
* @param cap Integer.
94+
*/
95+
public LruCache(final int cap) {
96+
this.capacity = cap;
6197
}
6298

6399
/**
64100
* Get user account.
101+
* @param userId String
102+
* @return {@link UserAccount}
65103
*/
66-
public UserAccount get(String userId) {
104+
public UserAccount get(final String userId) {
67105
if (cache.containsKey(userId)) {
68106
var node = cache.get(userId);
69107
remove(node);
@@ -75,8 +113,9 @@ public UserAccount get(String userId) {
75113

76114
/**
77115
* Remove node from linked list.
116+
* @param node {@link Node}
78117
*/
79-
public void remove(Node node) {
118+
public void remove(final Node node) {
80119
if (node.previous != null) {
81120
node.previous.next = node.next;
82121
} else {
@@ -91,8 +130,9 @@ public void remove(Node node) {
91130

92131
/**
93132
* Move node to the front of the list.
133+
* @param node {@link Node}
94134
*/
95-
public void setHead(Node node) {
135+
public void setHead(final Node node) {
96136
node.next = head;
97137
node.previous = null;
98138
if (head != null) {
@@ -106,8 +146,10 @@ public void setHead(Node node) {
106146

107147
/**
108148
* Set user account.
149+
* @param userAccount {@link UserAccount}
150+
* @param userId {@link String}
109151
*/
110-
public void set(String userId, UserAccount userAccount) {
152+
public void set(final String userId, final UserAccount userAccount) {
111153
if (cache.containsKey(userId)) {
112154
var old = cache.get(userId);
113155
old.userAccount = userAccount;
@@ -127,25 +169,40 @@ public void set(String userId, UserAccount userAccount) {
127169
}
128170
}
129171

130-
public boolean contains(String userId) {
172+
/**
173+
* Che if Cache cintains the userId.
174+
* @param userId {@link String}
175+
* @return boolean
176+
*/
177+
public boolean contains(final String userId) {
131178
return cache.containsKey(userId);
132179
}
133180

134-
/**
135-
* Invalidate cache for user.
136-
*/
137-
public void invalidate(String userId) {
181+
/**
182+
* Invalidate cache for user.
183+
* @param userId {@link String}
184+
*/
185+
public void invalidate(final String userId) {
138186
var toBeRemoved = cache.remove(userId);
139187
if (toBeRemoved != null) {
140-
LOGGER.info("# {} has been updated! Removing older version from cache...", userId);
188+
LOGGER.info("# {} has been updated! "
189+
+ "Removing older version from cache...", userId);
141190
remove(toBeRemoved);
142191
}
143192
}
144193

194+
/**
195+
* Is cache full?
196+
* @return boolean
197+
*/
145198
public boolean isFull() {
146199
return cache.size() >= capacity;
147200
}
148201

202+
/**
203+
* Get LRU data.
204+
* @return {@link UserAccount}
205+
*/
149206
public UserAccount getLruData() {
150207
return end.userAccount;
151208
}
@@ -161,6 +218,7 @@ public void clear() {
161218

162219
/**
163220
* Returns cache data in list form.
221+
* @return {@link List}
164222
*/
165223
public List<UserAccount> getCacheDataInListForm() {
166224
var listOfCacheData = new ArrayList<UserAccount>();
@@ -174,10 +232,13 @@ public List<UserAccount> getCacheDataInListForm() {
174232

175233
/**
176234
* Set cache capacity.
235+
* @param newCapacity int
177236
*/
178-
public void setCapacity(int newCapacity) {
237+
public void setCapacity(final int newCapacity) {
179238
if (capacity > newCapacity) {
180-
clear(); // Behavior can be modified to accommodate for decrease in cache size. For now, we'll
239+
// Behavior can be modified to accommodate
240+
// for decrease in cache size. For now, we'll
241+
clear();
181242
// just clear the cache.
182243
} else {
183244
this.capacity = newCapacity;

caching/src/main/java/com/iluwatar/caching/UserAccount.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,16 @@
3636
@AllArgsConstructor
3737
@ToString
3838
public class UserAccount {
39+
/**
40+
* User Id.
41+
*/
3942
private String userId;
43+
/**
44+
* User Name.
45+
*/
4046
private String userName;
47+
/**
48+
* Additional Info.
49+
*/
4150
private String additionalInfo;
4251
}

caching/src/main/java/com/iluwatar/caching/constants/CachingConstants.java

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,27 @@
2626
/**
2727
* Constant class for defining constants.
2828
*/
29-
public class CachingConstants {
30-
29+
public final class CachingConstants {
30+
/**
31+
* User Account.
32+
*/
3133
public static final String USER_ACCOUNT = "user_accounts";
34+
/**
35+
* User ID.
36+
*/
3237
public static final String USER_ID = "userID";
38+
/**
39+
* User Name.
40+
*/
3341
public static final String USER_NAME = "userName";
42+
/**
43+
* Additional Info.
44+
*/
3445
public static final String ADD_INFO = "additionalInfo";
3546

47+
/**
48+
* Constructor.
49+
*/
50+
private CachingConstants() {
51+
}
3652
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
/**
2+
* Constants.
3+
*/
4+
package com.iluwatar.caching.constants;

caching/src/main/java/com/iluwatar/caching/database/DbManagerFactory.java

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
11
package com.iluwatar.caching.database;
22

3-
public class DbManagerFactory {
4-
public static DbManager initDb(boolean isMongo){
5-
if(isMongo){
3+
/**
4+
* Creates the database connection accroding the input parameter.
5+
*/
6+
public final class DbManagerFactory {
7+
/**
8+
* Private constructor.
9+
*/
10+
private DbManagerFactory() {
11+
}
12+
13+
/**
14+
* Init database.
15+
*
16+
* @param isMongo boolean
17+
* @return {@link DbManager}
18+
*/
19+
public static DbManager initDb(final boolean isMongo) {
20+
if (isMongo) {
621
return new MongoDb();
722
}
823
return new VirtualDb();

0 commit comments

Comments
 (0)