0

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;
    }
            
Thomas Adrian
  • 3,543
  • 6
  • 32
  • 62

2 Answers2

2

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;
}

Alternatively you can change your method to return the universal id of the document (using checkEntry.getUniversalID()) and then use that to get the document and the needed fields.

Per Henrik Lausten
  • 21,331
  • 3
  • 29
  • 76
  • I want the last matched entry in the loop to be returned, I can maybe sort the collection descending and return the first entry. or use getLastEntry/getPrevEntry – Thomas Adrian Sep 22 '22 at 08:57
  • Ah missed that part. I guess the issue is that you recycle checkEntry before returning matchEntry - and matchEntry points to the same object as checkEntry. – Per Henrik Lausten Sep 22 '22 at 09:10
  • Perhaps you can use my answer if you reverse the sort order of the view 'v'. – Per Henrik Lausten Sep 22 '22 at 09:12
  • 1
    I used getLastItem/getPrevItem to solve my problem and exit after the first I found. sometimes it just helps to write the problem down. thx – Thomas Adrian Sep 23 '22 at 07:44
2

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

fantaghirocco
  • 4,761
  • 6
  • 38
  • 48