I have some problem when I input value like this [] or [""] to insert data base.
It will infinity loop not stop.
I don't know why.
Example source code below.
#Entity("student")
class Student {
......
#Column(name = "something")
#Convert(converter = CustomConverter.class)
private List<String> something;
}
#Converter
public class CustomConverter implements AttributeConverter<List<String>, String> {
#Override
public String convertToDatabaseColumn(final List<String> param) {
if (param == null) {
return null;
} else if (param.isEmpty()) {
return null;
} else {
return String.join(",", param);
}
}
#Override
public List<String> convertToEntityAttribute(final String param) {
if (param == null) {
return null;
} else if (param.isEmpty()) {
return null;
} else {
return Arrays.asList(param.split(","));
}
}
But when I use value valid is working fine like this value ["vanda","nico"]
Question how to solve it and why this process loop forever ?
Thank you.
This is the request that been sent to service layer
public class UserRequest {
private String id;
private String link;
private String searchText;
}
Created class for "user response creator" -> to have different transformations
#Component
public class UserResponseCreator {
#Autowired
private UserInfoServiceClient userInfoServiceClient;
public List<UserResponse> getUserResponse() {
//transformation one
}
public List<UserResponse> getUserResponse(UserRequest request) {
//transformation two
}
}
The service class:
#Service
public class UserService {
#Autowired
private UserResponseCreator UserResponseCreator;
public List<UserResponse> sendEntries(UserRequest request) {
String node = request.getnode();
String link = request.getLinkedTo();
String searchText = request.getSearchText();
List<UserResponse> UserResponses = new ArrayList<>();
if ( node == null && link == null && searchText == null ) {
UserResponses = UserResponseCreator.getUserResponse();
} else if (node != null && link != null) {
UserResponses = UserResponseCreator.getUserResponse(request);
} else if (searchText!=null) {
UserResponses = UserResponseCreator.getUserResponse();
} else {
throw new InvalidRequestBodyException("Please check the request body");
}
return UserResponses;
}
}
Now I have following issues:
How to avoid the multiple if and repeated null checks
Have to use command pattern and create a method to pass parameter , check null values
For the null check i prepared a method:
public boolean checkFieldsIsNull(Object... varArgs) {
return Stream.of(varArgs)
.allMatch(Objects::isNull);
}
I'm reading the source code of spring boot. I found a problem when reading the SpringFactoriesLoader.loadFactoryNames. The URLClassPath.getResources return the implementation of Enumeration<Resource>, but the value has extra two fields name and check. So, when do the two fields be added into the return value?
public Enumeration<Resource> getResources(final String var1, final boolean var2) {
return new Enumeration<Resource>() {
private int index = 0;
private int[] cache = URLClassPath.this.getLookupCache(var1);
private Resource res = null;
private boolean next() {
if (this.res != null) {
return true;
} else {
do {
URLClassPath.Loader var1x;
if ((var1x = URLClassPath.this.getNextLoader(this.cache, this.index++)) == null) {
return false;
}
this.res = var1x.getResource(var1, var2);
} while(this.res == null);
return true;
}
}
public boolean hasMoreElements() {
return this.next();
}
public Resource nextElement() {
if (!this.next()) {
throw new NoSuchElementException();
} else {
Resource var1x = this.res;
this.res = null;
return var1x;
}
}
};
}
I'm using the Intellij to debug the program, the result is
Result instance is an anonymous class in this case and it captures the values of the getResources parameters (final String var1, final boolean var2).
When I call the below method, it returns null.
public String determineAction(String someAction){
if(someAction.length() > 4){
return someAction;
}else{
return super.determineAction(someAction)
}
}
When I break out the return statement into a String return value
String action = super.determineAction(someAction);
return action;
the value is returned.
Care to share some of the mechanics? I do not remember seeing this behavior with other objects or primitive data types.
Below is the super method
protected String determineAction(String someAction) {
if(someAction!= null) {
switch(someAction) {
case ACCEPT: return ACTION_ACCEPT;
case CANCEL: return ACTION_CANCEL;
case UPDATE: return ACTION_UPDATE;
case PASS: return ACTION_PASS;
default: return null;
}
}
return null;
}
Please refer to the following code snippets.
Parent class
public class Father {
protected String determineAction(String someAction) {
if(someAction!= null) {
switch(someAction) {
case "abc": return someAction+someAction+someAction;
case "ab": return someAction+someAction;
default: return null;
}
}
return null;
}
}
Child class
public class Son extends Father {
public String determineAction(String someAction){
if(someAction.length() > 4)
return someAction;
return super.determineAction(someAction);
}
public static void main(String[] args) {
Son mySon = new Son();
System.out.println(mySon.determineAction("abc")); // prints abcabcabc // father determineAction()
System.out.println(mySon.determineAction("a1234")); // prints a1234 // son determineAction()
}
}
Can someone tell me what the purpose of having inner classes? I can think of a few but may be they are not good reasons for using inner classes. My reasoning is that inner class is helpful when you want to use a class that no other classes can use. What else?
When I was learning Java we used inner classes for GUI event handling classes. It is sort of a "one time use" class that need not be available to other classes, and only is relevant to the class in which it resides.
Inner classes can be used to simulate closures: http://en.wikipedia.org/wiki/Closure_(computer_science)#Java
I use inner classes to define a structure that is best represented by the containing class, but doesn't necessarily make sense to use a separate external class to represent the structure.
To give an example I have a class that represents a particular type of network device, and the class has certain types of tests that can be run on that device. For each test there is also a potential set of errors that can be found. Each type of device may have a different structure for the errors.
With this you could do things like
List<Error> errors = RemoteDeviceA.getErrors();
With methods being available from the inner class, like
for ( Error error : errors ) {
System.out.println("MOnitor Type: " + error.getMonType());
...
}
Of course there are other ways to do this, this is just an inner class approach.
Simplified (aka incomplete) code for above:
public class RemoteDeviceA {
private String host;
private String user;
private String password;
private static List<Error> errors;
public RemoteDeviceA(String user, String host, String password) {
this.host = host;
this.user = user;
this.password = password;
login();
}
private void login() {
// Logs in
}
public void runTestA() {
List<Error> errorList = new ArrayList<Error>();
//loop through test results
if (!value.equals("0")) {
Error error = new Error(node, rackNum, shelfNum, slotNum, monType, value);
if (error.isError()) {
errorList.add(error);
}
}
setErrors(errorList);
}
private static void setErrors(List<Error> errors) {
RemoteDeviceA.errors = errors;
}
public List<Error> getErrors() {
return errors;
}
public class Error {
private String monType;
private String node;
private String rack;
private String shelf;
private String slot;
private String value;
private boolean error = false;
private boolean historyError = false;
private boolean critical = false;
private boolean criticalHistory = false;
Error(String node, String rack, String shelf, String slot,
String monType, String value) {
parseAlarm(node, rack, shelf, slot, monType, value);
}
private void parseAlarm(String node, String rack, String shelf,
String slot, String monType, String value) {
String modType = "";
if (monType.startsWith("ES_15") && !value.equals("0")) {
setMonType("ES_15");
setError(true);
} else if (monType.startsWith("SES_15") && !value.equals("0")) {
setMonType("SES_15");
setError(true);
} else if (monType.startsWith("BBE_15") && !value.equals("0")) {
setMonType("BBE_15");
setError(true);
} else if (monType.startsWith("UT_15") && !value.equals("0")) {
setMonType("UT_15");
setError(true);
setCritial(critical);
} else if (monType.startsWith("ES_24") && !value.equals("0")) {
setMonType("ES_24");
setHistoryError(true);
setError(true);
} else if (monType.startsWith("SES_24") && !value.equals("0")) {
setMonType("SES_24");
setHistoryError(true);
setError(true);
} else if (monType.startsWith("BBE_24") && !value.equals("0")) {
setMonType("BBE_24");
setHistoryError(true);
setError(true);
} else if (monType.startsWith("UT_24") && !value.equals("0")) {
setMonType("UT_24");
setHistoryError(true);
setError(true);
setCriticalHistory(true);
} else if (monType.startsWith("UT_15") && !value.equals("0")) {
setMonType("UT_15");
setError(true);
setCritial(true);
} else if (monType.startsWith("LASPWR")) {
float laserPwr = Float.valueOf(value);
if (node.startsWith("LEM_EM")) {
if ((laserPwr < 8.0) || (laserPwr > 12.0)) {
setMonType("LASERPWR");
setError(true);
}
} else if (node.startsWith("LEM10")) {
if ((laserPwr < 18.0) || (laserPwr > 22.0)) {
setMonType("LASERPWR");
setError(true);
}
}
}
if (isError()) {
setNode(node);
setRack(rack);
setShelf(shelf);
setSlot(slot);
setValue(value);
setError(true);
}
}
private void setMonType(String monType) {
this.monType = monType;
}
public String getMonType() {
return monType;
}
private void setNode(String node) {
this.node = node;
}
public String getNode() {
return node;
}
public void setRack(String rack) {
this.rack = rack;
}
public String getRack() {
return rack;
}
public void setShelf(String shelf) {
this.shelf = shelf;
}
public String getShelf() {
return shelf;
}
public void setSlot(String slot) {
this.slot = slot;
}
public String getSlot() {
return slot;
}
private void setValue(String value) {
this.value = value;
}
public String getValue() {
return value;
}
private void setError(boolean error) {
this.error = error;
}
public boolean isError() {
return error;
}
public void setCritial(boolean critical) {
this.critical = critical;
}
public boolean isCritical() {
return critical;
}
public void setCriticalHistory(boolean criticalHistory) {
this.criticalHistory = criticalHistory;
}
public boolean isCriticalHistory() {
return criticalHistory;
}
public void setHistoryError(boolean historyError) {
this.historyError = historyError;
}
public boolean isHistoryError() {
return historyError;
}
}
}
A list implementation that internally uses a linked list to store the elements could make good use of an inner class to represent the nodes within the list. I think you've hit the nail on the head by saying that you'd use such a class where you want to use it internally to a class but don't want it exposed - a 'one off' class that is only really useful 'here'.
I use inner classes (in C++) in situations where multiple classes, unrelated through inheritance, have conceptually similar implementation details, which form an implicit part of the public interface and ought to be named similarly.
class lib::Identifier { ... };
class lib::Person {
public:
class Identifier : public lib::Identifier { ... };
};
class lib::File {
public:
class Identifier : public lib::Identifier { ... };
};
This makes it convenient to refer to Identifier, Person::Identifier, and File::Identifier as simply Identifier, in the appropriate scopes.