Skip to content

Commit b47fe7c

Browse files
committed
feat: JHashMap
1 parent dafd9a9 commit b47fe7c

File tree

4 files changed

+137
-3
lines changed

4 files changed

+137
-3
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,12 @@ framework and objects.
4646
<dependency>
4747
<groupId>com.javaquery</groupId>
4848
<artifactId>util</artifactId>
49-
<version>1.0.7</version>
49+
<version>1.2.0</version>
5050
</dependency>
5151
```
5252

5353
# Gradle
5454

5555
```
56-
implementation 'com.javaquery:util:1.0.7'
56+
implementation 'com.javaquery:util:1.2.0'
5757
```

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ plugins {
66

77
sourceCompatibility = 1.8
88
group 'com.javaquery'
9-
version '1.0.7'
9+
version '1.2.0'
1010

1111
repositories {
1212
mavenCentral()
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
package com.javaquery.util.collection;
2+
3+
import com.javaquery.util.Regex;
4+
5+
import java.util.HashMap;
6+
7+
/**
8+
* @author vicky.thakor
9+
* @since 1.2.0
10+
*/
11+
public class JHashMap<K, V> extends HashMap<K, V> {
12+
13+
/**
14+
* @param key the key whose associated value is to be returned
15+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
16+
*/
17+
public Integer optInt(K key){
18+
return optInt(key, 0);
19+
}
20+
21+
/**
22+
* @param key the key whose associated value is to be returned
23+
* @param defaultValue the default mapping of the key
24+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
25+
*/
26+
@SuppressWarnings("unchecked")
27+
public Integer optInt(K key, Integer defaultValue){
28+
Object value = getOrDefault(key, (V) defaultValue);
29+
if(value instanceof Number){
30+
return ((Number) value).intValue();
31+
}
32+
String strValue = String.valueOf(value);
33+
return Regex.isNumber(strValue) ? Integer.parseInt(strValue) : defaultValue;
34+
}
35+
36+
/**
37+
* @param key the key whose associated value is to be returned
38+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
39+
*/
40+
public Long optLong(K key){
41+
return optLong(key, 0L);
42+
}
43+
44+
/**
45+
* @param key the key whose associated value is to be returned
46+
* @param defaultValue the default mapping of the key
47+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
48+
*/
49+
@SuppressWarnings("unchecked")
50+
public Long optLong(K key, Long defaultValue){
51+
Object value = getOrDefault(key, (V) defaultValue);
52+
if(value instanceof Number){
53+
return ((Number) value).longValue();
54+
}
55+
String strValue = String.valueOf(value);
56+
return Regex.isNumber(strValue) ? Long.parseLong(strValue) : defaultValue;
57+
}
58+
59+
/**
60+
* @param key the key whose associated value is to be returned
61+
* @return the value to which the specified key is mapped, or 0 if this map contains no mapping for the key
62+
*/
63+
public Double optDouble(K key){
64+
return optDouble(key, 0D);
65+
}
66+
67+
/**
68+
* @param key the key whose associated value is to be returned
69+
* @param defaultValue the default mapping of the key
70+
* @return the value to which the specified key is mapped, or defaultValue if this map contains no mapping for the key
71+
*/
72+
@SuppressWarnings("unchecked")
73+
public Double optDouble(K key, Double defaultValue){
74+
Object value = getOrDefault(key, (V) defaultValue);
75+
if(value instanceof Number){
76+
return ((Number) value).doubleValue();
77+
}
78+
String strValue = String.valueOf(value);
79+
return Regex.isNumber(strValue) ? Double.parseDouble(strValue) : defaultValue;
80+
}
81+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.javaquery.util.collection;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author vicky.thakor
8+
* @since 1.2.0
9+
*/
10+
public class TestJHashMap {
11+
12+
@Test
13+
public void test_optInt(){
14+
JHashMap<String, String> jHashMap = new JHashMap<>();
15+
jHashMap.put("a", "1");
16+
jHashMap.put("b", "a");
17+
Assertions.assertEquals(1, jHashMap.optInt("a"));
18+
Assertions.assertEquals(0, jHashMap.optInt("b"));
19+
20+
JHashMap<String, Long> jHashMap2 = new JHashMap<>();
21+
jHashMap2.put("a", 1L);
22+
Assertions.assertEquals(1, jHashMap2.optInt("a", 10));
23+
Assertions.assertEquals(20, jHashMap2.optInt("b", 20));
24+
}
25+
26+
@Test
27+
public void test_optLong(){
28+
JHashMap<String, String> jHashMap = new JHashMap<>();
29+
jHashMap.put("a", "1");
30+
jHashMap.put("b", "a");
31+
Assertions.assertEquals(1L, jHashMap.optLong("a"));
32+
Assertions.assertEquals(0L, jHashMap.optLong("b"));
33+
34+
JHashMap<String, Integer> jHashMap2 = new JHashMap<>();
35+
jHashMap2.put("a", 1);
36+
Assertions.assertEquals(1, jHashMap2.optLong("a", 10L));
37+
Assertions.assertEquals(20, jHashMap2.optLong("b", 20L));
38+
}
39+
40+
@Test
41+
public void test_optDouble(){
42+
JHashMap<String, String> jHashMap = new JHashMap<>();
43+
jHashMap.put("a", "1");
44+
jHashMap.put("b", "a");
45+
Assertions.assertEquals(1D, jHashMap.optDouble("a"));
46+
Assertions.assertEquals(0D, jHashMap.optDouble("b"));
47+
48+
JHashMap<String, Integer> jHashMap2 = new JHashMap<>();
49+
jHashMap2.put("a", 1);
50+
Assertions.assertEquals(1D, jHashMap2.optDouble("a", 10D));
51+
Assertions.assertEquals(20D, jHashMap2.optDouble("b", 20D));
52+
}
53+
}

0 commit comments

Comments
 (0)