In the following code the returned matchEntry is recycled, how can I fix that so that the returned matchEntry is not recycled.
I want the last matched entry in the loop to be returned
The object.clone() method does not seem to be available, and object.getClass().newInstance() doesn't seem to work.
public ViewEntry getStdTime(DateTime input,String key){
ViewEntryCollection checkColl = v.getAllEntriesByKey(key, true);
ViewEntry checkEntry;
ViewEntry checkTmpEntry = null;
ViewEntry matchEntry = null;
checkEntry = checkColl.getFirstEntry();
while (checkEntry != null) {
String xStartDate = checkEntry.getColumnValues().get(2);
DateTime dtx = session.createDateTime(xStartDate);
if (dtx.toJavaDate().compareTo(input.toJavaDate()) <= 0) {
matchEntry = checkEntry;
}
checkTmpEntry = checkColl.getNextEntry(checkEntry);
checkEntry.recycle();
checkEntry = checkTmpEntry;
}
return matchEntry;
}
CodePudding user response:
Return the entry as soon as you find it:
public ViewEntry getStdTime(DateTime input,String key){
ViewEntryCollection checkColl = v.getAllEntriesByKey(key, true);
ViewEntry checkEntry;
ViewEntry checkTmpEntry = null;
checkEntry = checkColl.getFirstEntry();
while (checkEntry != null) {
String xStartDate = checkEntry.getColumnValues().get(2);
DateTime dtx = session.createDateTime(xStartDate);
if (dtx.toJavaDate().compareTo(input.toJavaDate()) <= 0) {
return checkEntry;
}
checkTmpEntry = checkColl.getNextEntry(checkEntry);
checkEntry.recycle();
checkEntry = checkTmpEntry;
}
return null;
}
CodePudding user response:
Maybe there's a more elegant solution but this should do the job if I got it correctly:
public ViewEntry getStdTime(DateTime input,String key){
ViewEntryCollection checkColl = v.getAllEntriesByKey(key, true);
ViewEntry checkEntry;
ViewEntry checkTmpEntry = null;
ViewEntry matchEntry = null;
checkEntry = checkColl.getFirstEntry();
while (checkEntry != null) {
String xStartDate = checkEntry.getColumnValues().get(2);
DateTime dtx = session.createDateTime(xStartDate);
if (dtx.toJavaDate().compareTo(input.toJavaDate()) <= 0) {
if (matchEntry != null)
matchEntry.recycle();
matchEntry = checkEntry;
}
checkTmpEntry = checkColl.getNextEntry(checkEntry);
if (!checkEntry.equals(matchEntry))
checkEntry.recycle();
checkEntry = checkTmpEntry;
}
return matchEntry;
}
The only non-recycled entry shoud be the last matched one