Skip to content

Commit 42092cc

Browse files
authored
Merge pull request #12 from javaquery/v1.2.1
V1.2.1
2 parents 06d4dff + 49c8dfa commit 42092cc

File tree

8 files changed

+114
-3
lines changed

8 files changed

+114
-3
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
44
zipStoreBase=GRADLE_USER_HOME
55
zipStorePath=wrapper/dists
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.javaquery.util.iface;
2+
3+
/**
4+
* @author vicky.thakor
5+
* @since 1.2.1
6+
*/
7+
public interface Processor<T, R> {
8+
9+
/**
10+
* Process T object and return the value.
11+
* @param t - input object
12+
* @return return value
13+
*/
14+
R process(T t);
15+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javaquery.util.iface;
2+
3+
import com.javaquery.util.io.JFile;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @author vicky.thakor
9+
* @since 1.2.1
10+
*/
11+
public interface Reader<T> {
12+
/**
13+
* @param file - file to read
14+
* @return return the result
15+
* @throws IOException exception
16+
*/
17+
T read(JFile file) throws IOException;
18+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.javaquery.util.iface;
2+
3+
import com.javaquery.util.io.JFile;
4+
5+
import java.io.IOException;
6+
7+
/**
8+
* @author vicky.thakor
9+
* @since 1.2.1
10+
*/
11+
public interface Writer<T> {
12+
/**
13+
* @param data - data to write
14+
* @param file - file to write
15+
* @throws IOException exception
16+
*/
17+
void write(T data, JFile file) throws IOException;
18+
}

src/main/java/com/javaquery/util/json/JSONObject.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.javaquery.util.time.DateTimeFormat;
66
import com.javaquery.util.time.Dates;
77
import org.json.JSONArray;
8+
import org.json.JSONException;
89

910
import java.math.BigDecimal;
1011
import java.math.BigInteger;
@@ -27,12 +28,37 @@ public JSONObject(String json) {
2728
this(new org.json.JSONObject(json));
2829
}
2930

31+
public JSONObject(){
32+
this(new org.json.JSONObject());
33+
}
34+
3035
/** @param jsonObject {@link org.json.JSONObject} to prepare {@link JSONObject} */
3136
public JSONObject(org.json.JSONObject jsonObject) {
3237
ROOT = jsonObject;
3338
CACHED_OBJECT = new HashMap<>();
3439
}
3540

41+
/**
42+
* Put a key/value pair in the JSONObject. If the value is <code>null</code>, then the
43+
* key will be removed from the JSONObject if it is present.
44+
*
45+
* @param key
46+
* A key string.
47+
* @param value
48+
* An object which is the value. It should be of one of these
49+
* types: Boolean, Double, Integer, JSONArray, JSONObject, Long,
50+
* String, or the JSONObject.NULL object.
51+
* @return this.
52+
* @throws JSONException
53+
* If the value is non-finite number.
54+
* @throws NullPointerException
55+
* If the key is <code>null</code>.
56+
*/
57+
public JSONObject put(String key, Object value){
58+
ROOT.put(key, value);
59+
return this;
60+
}
61+
3662
/**
3763
* Get an optional boolean associated with a key. It returns false if there is no such key, or if
3864
* the value is not Boolean.TRUE or the String "true".

src/main/java/com/javaquery/util/string/Strings.java

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,24 @@ public static void notEquals(String a, String b, ExecutableFunction executableFu
212212
}
213213
}
214214

215+
/**
216+
* Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
217+
* Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
218+
* <ul>
219+
* <li>The two characters are the same (as compared by the == operator)</li>
220+
* <li>Calling Character.toLowerCase(Character.toUpperCase(char)) on each character produces the same result</li>
221+
* </ul>
222+
*
223+
* @param a String
224+
* @param b String to be compared with {@code a} for equality (ignoring case considerations)
225+
* @return {@code true} if the provided Strings are equalsIgnoreCase otherwise
226+
* * {@code false}
227+
*/
228+
public static boolean equalsIgnoreCase(String a, String b){
229+
return (a == null && b == null)
230+
|| (a != null && a.equalsIgnoreCase(b));
231+
}
232+
215233
/**
216234
* Compares this String to another String, ignoring case considerations. Two strings are considered equal ignoring case if they are of the same length and corresponding characters in the two strings are equal ignoring case.
217235
* Two characters c1 and c2 are considered the same ignoring case if at least one of the following is true:
@@ -225,7 +243,7 @@ public static void notEquals(String a, String b, ExecutableFunction executableFu
225243
* @param executableFunction lambda function given executed if the provided Strings are equals (ignoring case considerations).
226244
*/
227245
public static void equalsIgnoreCase(String a, String b, ExecutableFunction executableFunction){
228-
if(a != null && a.equalsIgnoreCase(b)){
246+
if(equalsIgnoreCase(a, b)){
229247
executableFunction.execute();
230248
}
231249
}
@@ -238,7 +256,7 @@ public static void equalsIgnoreCase(String a, String b, ExecutableFunction execu
238256
* @param executableFunction lambda function given executed if the provided Strings are non equals (ignoring case considerations).
239257
*/
240258
public static void notEqualsIgnoreCase(String a, String b, ExecutableFunction executableFunction){
241-
if(a != null && !a.equalsIgnoreCase(b)){
259+
if(!equalsIgnoreCase(a, b)){
242260
executableFunction.execute();
243261
}
244262
}

src/test/java/com/javaquery/util/json/TestJSONObject.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,13 @@ public class TestJSONObject {
2121
"{\"author\":\"vicky\",\"created\":\"2021-01-20 10:00:10\",\"items\":{\"item\":[{\"ppu\":0.55,\"batters\":{\"batter\":[{\"available\":true,\"id\":\"1001\",\"type\":\"Regular\"},{\"available\":false,\"id\":\"1002\",\"type\":\"Chocolate\"},{\"id\":\"1003\",\"type\":\"Blueberry\"},{\"id\":\"1004\",\"type\":\"Devil's Food\"}]},\"name\":\"Cake\",\"id\":\"0001\",\"type\":\"donut\",\"topping\":[{\"id\":\"5001\",\"type\":\"None\"},{\"id\":\"5002\",\"type\":\"Glazed\"},{\"id\":\"5005\",\"type\":\"Sugar\",\"kg\":652398},{\"id\":\"5007\",\"type\":\"Powdered Sugar\",\"kg\":875},{\"id\":\"5006\",\"type\":\"Chocolate with Sprinkles\"},{\"id\":\"5003\",\"type\":\"Chocolate\"},{\"id\":\"5004\",\"type\":\"Maple\"}]}]}}";
2222
private static final JSONObject JSONOBJECT = new JSONObject(STRING_JSON_OBJECT);
2323

24+
@Test
25+
public void test_put(){
26+
JSONObject jsonObject = new JSONObject();
27+
jsonObject.put("a", 10);
28+
Assertions.assertEquals(10, jsonObject.optInt("a"));
29+
}
30+
2431
@Test
2532
public void test_optBoolean() {
2633
Assertions.assertTrue(JSONOBJECT.optBoolean("items.item[0].batters.batter[0].available"));

src/test/java/com/javaquery/util/string/TestStrings.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,15 @@ public void test_notEquals(){
116116

117117
@Test
118118
public void test_equalsIgnoreCase(){
119+
Assertions.assertTrue(Strings.equalsIgnoreCase("a", "A"));
120+
Assertions.assertTrue(Strings.equalsIgnoreCase("a", "a"));
121+
Assertions.assertTrue(Strings.equalsIgnoreCase(null, null));
122+
Assertions.assertFalse(Strings.equalsIgnoreCase("a", null));
123+
Assertions.assertFalse(Strings.equalsIgnoreCase(null, "b"));
124+
}
125+
126+
@Test
127+
public void test_equalsIgnoreCaseWithExecutableFunction(){
119128
Assertions.assertThrows(RuntimeException.class, () -> Strings.equalsIgnoreCase("A", "A", () -> {throw new RuntimeException("Function executed when two Strings are equalsIgnoreCase");}));
120129
Assertions.assertThrows(RuntimeException.class, () -> Strings.equalsIgnoreCase("A", "a", () -> {throw new RuntimeException("Function executed when two Strings are equalsIgnoreCase");}));
121130
Strings.equalsIgnoreCase("A", "b", () -> {throw new RuntimeException("Function won't execute when two Strings are not equalsIgnoreCase");});

0 commit comments

Comments
 (0)