Skip to content

Commit dafd9a9

Browse files
authored
Merge pull request #10 from javaquery/v1.0.7
release: 1.0.7
2 parents 94c7161 + 5ab7827 commit dafd9a9

File tree

8 files changed

+201
-7
lines changed

8 files changed

+201
-7
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.6.1</version>
49+
<version>1.0.7</version>
5050
</dependency>
5151
```
5252

5353
# Gradle
5454

5555
```
56-
implementation 'com.javaquery:util:1.0.6.1'
56+
implementation 'com.javaquery:util:1.0.7'
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.6.1'
9+
version '1.0.7'
1010

1111
repositories {
1212
mavenCentral()
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.javaquery.util.exception;
2+
3+
/**
4+
* @author vicky.thakor
5+
* @since 1.0.7
6+
*/
7+
public abstract class AbstractRuntimeException extends RuntimeException {
8+
static final long serialVersionUID = -1497632070387643027L;
9+
private final String errorCode;
10+
11+
public AbstractRuntimeException(String errorCode, String message) {
12+
super(message);
13+
this.errorCode = errorCode;
14+
}
15+
16+
public AbstractRuntimeException(String errorCode, String message, Throwable cause) {
17+
super(message, cause);
18+
this.errorCode = errorCode;
19+
}
20+
21+
public AbstractRuntimeException(String errorCode, Throwable cause) {
22+
super(cause);
23+
this.errorCode = errorCode;
24+
}
25+
26+
public String getErrorCode() {
27+
return errorCode;
28+
}
29+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.javaquery.util.exception;
2+
3+
import java.util.stream.Collectors;
4+
import java.util.stream.Stream;
5+
6+
/**
7+
* Use this exception to log parameters you sent to method for debugging.
8+
* Make sure you do not log any confidential details.
9+
* @author vicky.thakor
10+
* @since 1.0.7
11+
*/
12+
public class ParameterAwareException extends AbstractRuntimeException {
13+
static final long serialVersionUID = 4721842774212273469L;
14+
private static final String PARAMETERS = "Parameters: ";
15+
private final Object[] parameters;
16+
17+
/**
18+
* @param errorCode error code of exception to distinguish your project specific exception
19+
* @param message exception message
20+
* @param parameters parameters to log
21+
*/
22+
public ParameterAwareException(String errorCode, String message, Object... parameters) {
23+
super(errorCode, message + "\n" + PARAMETERS + Stream.of(parameters).map(String::valueOf).collect(Collectors.toList()));
24+
this.parameters = parameters;
25+
}
26+
27+
/**
28+
* @param errorCode error code of exception to distinguish your project specific exception
29+
* @param cause an exception
30+
* @param parameters parameters to log
31+
*/
32+
public ParameterAwareException(String errorCode, Throwable cause, Object... parameters){
33+
super(errorCode, PARAMETERS + Stream.of(parameters).map(String::valueOf).collect(Collectors.toList()), cause);
34+
this.parameters = parameters;
35+
}
36+
37+
/**
38+
* @param errorCode error code of exception to distinguish your project specific exception
39+
* @param message exception message
40+
* @param cause an exception
41+
* @param parameters parameters to log
42+
*/
43+
public ParameterAwareException(String errorCode, String message, Throwable cause, Object... parameters){
44+
super(errorCode, message + "\n" + PARAMETERS + Stream.of(parameters).map(String::valueOf).collect(Collectors.toList()), cause);
45+
this.parameters = parameters;
46+
}
47+
48+
/**
49+
* @return parameters provided in exception
50+
*/
51+
public Object[] getParameters() {
52+
return parameters;
53+
}
54+
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,61 @@ public static boolean hasDiacritics(String str) {
185185
String str2 = normalize(str);
186186
return str2.matches("(?s).*\\p{InCombiningDiacriticalMarks}.*");
187187
}
188+
189+
/**
190+
* Compares this string to the specified string. The result is true if and only if the argument is not null and
191+
* is a String object that represents the same sequence of characters as this object.
192+
* @param a String
193+
* @param b String to be compared with {@code a} for equality
194+
* @param executableFunction lambda function given executed if the provided Strings are equals.
195+
*/
196+
public static void equals(String a, String b, ExecutableFunction executableFunction){
197+
if(Objects.equals(a, b)){
198+
executableFunction.execute();
199+
}
200+
}
201+
202+
/**
203+
* Compares this string to the specified string. The result is true if and only if the one argument is null or
204+
* a String object that represents the different sequence of characters as this object.
205+
* @param a String
206+
* @param b String to be compared with {@code a} for non equality
207+
* @param executableFunction lambda function given executed if the provided Strings are not equals.
208+
*/
209+
public static void notEquals(String a, String b, ExecutableFunction executableFunction){
210+
if(!Objects.equals(a, b)){
211+
executableFunction.execute();
212+
}
213+
}
214+
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+
* @param executableFunction lambda function given executed if the provided Strings are equals (ignoring case considerations).
226+
*/
227+
public static void equalsIgnoreCase(String a, String b, ExecutableFunction executableFunction){
228+
if(a != null && a.equalsIgnoreCase(b)){
229+
executableFunction.execute();
230+
}
231+
}
232+
233+
/**
234+
* Compares this String to another String, ignoring case considerations. Two strings are considered not equal ignoring case if they are of the not same length and corresponding characters in the two strings are not equal ignoring case.
235+
*
236+
* @param a String
237+
* @param b String to be compared with {@code a} for non equality (ignoring case considerations)
238+
* @param executableFunction lambda function given executed if the provided Strings are non equals (ignoring case considerations).
239+
*/
240+
public static void notEqualsIgnoreCase(String a, String b, ExecutableFunction executableFunction){
241+
if(a != null && !a.equalsIgnoreCase(b)){
242+
executableFunction.execute();
243+
}
244+
}
188245
}

src/main/java/com/javaquery/util/time/Dates.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
public final class Dates {
1616

1717
public static final TimeZone SYSTEM_TIMEZONE = TimeZone.getDefault();
18-
private static final Calendar CALENDAR = Calendar.getInstance();
1918

2019
private Dates() {}
2120

@@ -205,8 +204,9 @@ public static long epochSeconds(Date date) {
205204
* @return Returns {@code Date} with given year, month, day, hour, minute, seconds
206205
*/
207206
public static Date getDate(int year, int month, int day, int hour, int minute, int seconds) {
208-
CALENDAR.set(year, --month, day, hour, minute, seconds);
209-
return CALENDAR.getTime();
207+
Calendar calendar = Calendar.getInstance();
208+
calendar.set(year, --month, day, hour, minute, seconds);
209+
return calendar.getTime();
210210
}
211211

212212
/**
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.javaquery.util.exception;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
/**
7+
* @author vicky.thakor
8+
* @since 1.0.7
9+
*/
10+
public class TestParameterAwareException {
11+
12+
@Test
13+
public void test_constructor(){
14+
ParameterAwareException parameterAwareException = new ParameterAwareException("ERR:1", new Exception(), "a", 1);
15+
Assertions.assertTrue(parameterAwareException.getMessage().contains("1"));
16+
Assertions.assertEquals("ERR:1", parameterAwareException.getErrorCode());
17+
18+
ParameterAwareException parameterAwareExceptionWithMessage = new ParameterAwareException("ERR:2", "Exception Message", new Exception(), "a", 1);
19+
Assertions.assertTrue(parameterAwareExceptionWithMessage.getMessage().contains("Exception Message"));
20+
Assertions.assertEquals("ERR:2", parameterAwareExceptionWithMessage.getErrorCode());
21+
Assertions.assertEquals(1, parameterAwareException.getParameters()[1]);
22+
23+
ParameterAwareException parameterAwareException2 = new ParameterAwareException("ERR:3", "Exception Message", 2);
24+
Assertions.assertTrue(parameterAwareException2.getMessage().contains("2"));
25+
Assertions.assertEquals("ERR:3", parameterAwareException2.getErrorCode());
26+
Assertions.assertEquals(2, parameterAwareException2.getParameters()[0]);
27+
}
28+
}

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.javaquery.util.string;
22

3-
import com.javaquery.util.io.Console;
43
import org.junit.jupiter.api.Assertions;
54
import org.junit.jupiter.api.Test;
65

@@ -101,4 +100,31 @@ public void test_hasDiacritics(){
101100
Assertions.assertTrue(Strings.hasDiacritics(str));
102101
Assertions.assertFalse(Strings.hasDiacritics(HELLO));
103102
}
103+
104+
@Test
105+
public void test_equals(){
106+
Assertions.assertThrows(RuntimeException.class, () -> Strings.equals("A", "A", () -> {throw new RuntimeException("Function executed when two Strings are equal");}));
107+
Strings.equals("A", "a", () -> {throw new RuntimeException("Function won't execute when two Strings are equal with ignore case");});
108+
Strings.equals("A", "B", () -> {throw new RuntimeException("Function won't execute when two Strings are not equal");});
109+
}
110+
111+
@Test
112+
public void test_notEquals(){
113+
Assertions.assertThrows(RuntimeException.class, () -> Strings.notEquals("A", "B", () -> {throw new RuntimeException("Function executed when two Strings are not equal");}));
114+
Strings.notEquals("A", "A", () -> {throw new RuntimeException("Function won't execute when two Strings are equal");});
115+
}
116+
117+
@Test
118+
public void test_equalsIgnoreCase(){
119+
Assertions.assertThrows(RuntimeException.class, () -> Strings.equalsIgnoreCase("A", "A", () -> {throw new RuntimeException("Function executed when two Strings are equalsIgnoreCase");}));
120+
Assertions.assertThrows(RuntimeException.class, () -> Strings.equalsIgnoreCase("A", "a", () -> {throw new RuntimeException("Function executed when two Strings are equalsIgnoreCase");}));
121+
Strings.equalsIgnoreCase("A", "b", () -> {throw new RuntimeException("Function won't execute when two Strings are not equalsIgnoreCase");});
122+
}
123+
124+
@Test
125+
public void test_notEqualsIgnoreCase(){
126+
Assertions.assertThrows(RuntimeException.class, () -> Strings.notEqualsIgnoreCase("A", "B", () -> {throw new RuntimeException("Function executed when two Strings are not equalsIgnoreCase");}));
127+
Assertions.assertThrows(RuntimeException.class, () -> Strings.notEqualsIgnoreCase("A", "b", () -> {throw new RuntimeException("Function executed when two Strings are not equalsIgnoreCase");}));
128+
Strings.notEqualsIgnoreCase("A", "a", () -> {throw new RuntimeException("Function won't execute when two Strings are equalsIgnoreCase");});
129+
}
104130
}

0 commit comments

Comments
 (0)