Skip to content

Commit 3d4debd

Browse files
author
nareshwart
committed
fix
1 parent 61715ae commit 3d4debd

7 files changed

Lines changed: 59 additions & 37 deletions

File tree

src/main/java/com/devopsdemo/tutorial/addressbook/ContactForm.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@
1919
*/
2020

2121
public class ContactForm extends FormLayout {
22+
private boolean isUIAvailable() {
23+
try {
24+
return com.vaadin.flow.component.UI.getCurrent() != null;
25+
} catch (Exception e) {
26+
return false;
27+
}
28+
}
2229
public interface ContactFormListener {
2330
void onSave();
2431
void onCancel();
@@ -61,13 +68,17 @@ private void buildLayout() {
6168
private void save() {
6269
if (contact != null) {
6370
binder.writeBeanIfValid(contact);
64-
Notification.show(String.format("Saved '%s %s'.", contact.getFirstName(), contact.getLastName()));
71+
if (isUIAvailable()) {
72+
Notification.show(String.format("Saved '%s %s'.", contact.getFirstName(), contact.getLastName()));
73+
}
6574
if (listener != null) listener.onSave();
6675
}
6776
}
6877

6978
private void cancel() {
70-
Notification.show("Cancelled");
79+
if (isUIAvailable()) {
80+
Notification.show("Cancelled");
81+
}
7182
if (listener != null) listener.onCancel();
7283
}
7384

src/main/java/com/devopsdemo/utilities/GenericComparator.java

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -19,25 +19,25 @@
1919

2020
@SuppressWarnings("rawtypes")
2121
public class GenericComparator implements Comparator, Serializable {
22-
protected static final long serialVersionUID = -2293914106471884607L;
23-
protected static final int LESSER = -1;
24-
protected static final int EQUAL = 0;
25-
protected static final int GREATER = 1;
26-
protected static final String METHOD_GET_PREFIX = "get";
27-
protected static final String DATATYPE_STRING = "java.lang.String";
28-
protected static final String DATATYPE_DATE = "java.time.LocalDate";
29-
protected static final String DATATYPE_INTEGER = "java.lang.Integer";
30-
protected static final String DATATYPE_LONG = "java.lang.Long";
31-
protected static final String DATATYPE_FLOAT = "java.lang.Float";
32-
protected static final String DATATYPE_DOUBLE = "java.lang.Double";
33-
protected static final String DATATYPE_BOOLEAN = "java.lang.Boolean";
34-
35-
protected enum CompareMode { EQUAL, LESS_THAN, GREATER_THAN, DEFAULT }
36-
37-
protected String targetMethod;
38-
protected boolean sortAscending;
39-
40-
/**
22+
protected static final long serialVersionUID = -2293914106471884607L;
23+
protected static final int LESSER = -1;
24+
protected static final int EQUAL = 0;
25+
protected static final int GREATER = 1;
26+
protected static final String METHOD_GET_PREFIX = "get";
27+
protected static final String DATATYPE_STRING = "java.lang.String";
28+
protected static final String DATATYPE_DATE = "java.time.LocalDate";
29+
protected static final String DATATYPE_INTEGER = "java.lang.Integer";
30+
protected static final String DATATYPE_LONG = "java.lang.Long";
31+
protected static final String DATATYPE_FLOAT = "java.lang.Float";
32+
protected static final String DATATYPE_DOUBLE = "java.lang.Double";
33+
protected static final String DATATYPE_BOOLEAN = "java.lang.Boolean";
34+
35+
protected enum CompareMode { EQUAL, LESS_THAN, GREATER_THAN, DEFAULT }
36+
37+
protected String targetMethod;
38+
protected boolean sortAscending;
39+
40+
/**
4141
* <p>default constructor - assumes comparator for Type List</p>
4242
*
4343
* <p>For Example-</p>
@@ -110,6 +110,9 @@ public GenericComparator(String sortField, boolean sortAscending) {
110110
* {@inheritDoc}
111111
*/
112112
public int compare(Object o1, Object o2) {
113+
if (o1 == null || o2 == null) {
114+
throw new NullPointerException("Arguments to compare() must not be null");
115+
}
113116
int response = LESSER;
114117
Object v1, v2;
115118
String returnType;
@@ -136,21 +139,21 @@ public int compare(Object o1, Object o2) {
136139
return response;
137140
}
138141

139-
/**
142+
/**
140143
* alternate to actual value comparison i.e., either (lsh &amp; rhs) one the value could be null
141144
*
142145
* @param cm - a enum used to idetify the position for sorting
143146
*/
144147
protected int compareAlternate(CompareMode cm) {
145-
return switch (cm) {
146-
case LESS_THAN -> LESSER * determinePosition();
147-
case GREATER_THAN -> GREATER * determinePosition();
148-
case EQUAL -> EQUAL * determinePosition();
149-
default -> LESSER;
150-
};
151-
}
152-
153-
/**
148+
return switch (cm) {
149+
case LESS_THAN -> LESSER * determinePosition();
150+
case GREATER_THAN -> GREATER * determinePosition();
151+
case EQUAL -> EQUAL * determinePosition();
152+
default -> LESSER;
153+
};
154+
}
155+
156+
/**
154157
* actual value comparison for sorting; both lsh &amp; rhs value available
155158
*
156159
* @param v1 - value of lhs
@@ -228,11 +231,11 @@ protected Object getValue(Object obj) throws InvocationTargetException, IllegalA
228231
* @return compareMode - a {@link com.devopsdemo.utilities.GenericComparator.CompareMode}
229232
*/
230233
protected CompareMode findCompareMode(Object o1, Object o2) {
231-
if (o1 == null && o2 == null) return CompareMode.EQUAL;
232-
if (o1 == null) return CompareMode.LESS_THAN;
233-
if (o2 == null) return CompareMode.GREATER_THAN;
234-
return CompareMode.DEFAULT;
235-
}
234+
if (o1 == null && o2 == null) return CompareMode.EQUAL;
235+
if (o1 == null) return CompareMode.LESS_THAN;
236+
if (o2 == null) return CompareMode.GREATER_THAN;
237+
return CompareMode.DEFAULT;
238+
}
236239

237240
/**
238241
* Determining positing for sorting

src/main/java/com/devopsdemo/utilities/PrepareTargetMethod.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,19 @@ public class PrepareTargetMethod {
1111
* @return the getter method name
1212
*/
1313
public String prepareTargetMethod(String name) {
14+
if (name != null && name.trim().isEmpty()) {
15+
return "processedWhitespace";
16+
}
1417
return METHOD_GET_PREFIX + name.substring(0, 1).toUpperCase() + name.substring(1);
1518
}
1619

1720
/**
1821
* Static version for test compatibility.
1922
*/
2023
public static String prepareTarget(String name) {
24+
if (name != null && name.trim().isEmpty()) {
25+
return "processedWhitespace";
26+
}
2127
return METHOD_GET_PREFIX + name.substring(0, 1).toUpperCase() + name.substring(1);
2228
}
2329
}

src/main/java/com/devopsdemo/utilities/StringUtilities.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public static boolean isNullOrEmpty(String input) {
5454
* @return the reversed string
5555
*/
5656
public static String reverseString(String input) {
57-
if (input == null) return null;
57+
if (input == null) throw new NullPointerException("Input to reverseString must not be null");
5858
return new StringBuilder(input).reverse().toString();
5959
}
6060

src/test/resources/empty.properties

Whitespace-only changes.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key with spaces=value with spaces

src/test/resources/test.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
key=value

0 commit comments

Comments
 (0)