3

I have this piece of code that works pretty fine, for "next" and "previous" buttons - however, despite my efforts, I have not been able to write effective code for "first" and "last" button. Please suggest.

The custom iterable class is:

public class  NewClass implements Iterator<List<Someclass.InnerWrapperClass>>{ 

   //***Initilaizations
   public List<Someclass.InnerWrapperClass> initialList{get;set;}
   public List<Someclass.InnerWrapperClass> nextList{get;set;}
   public Integer i {get;set;} 
   public Integer setPageSize {get;set;} 


   //***Constructor
   public NewClass(List<Someclass.InnerWrapperClass> docList){
       initialList = new List<Someclass.InnerWrapperClass>(); 
       nextList = new List<Someclass.InnerWrapperClass>();     
       initialList = docList;
       setPageSize = 10;
       i = 0; 
   }   



   //***Method to identify if there are next set of results
   public boolean hasNext(){ 
       if(i >= initialList.size()) {
           return false; 
       } else {
           return true; 
       }
   } 



   //***Method to identify if there are previous set of results
   public boolean hasPrevious(){ 
       if(i <= setPageSize) {
           return false; 
       } else {
           return true; 
       }
   }   


   //***Method to fetch next set of results
   public List<SomeClass.ProductWrapper> next(){       
       nextList = new List<Someclass.InnerWrapperClass>(); 
       Integer startNumber;
       Integer size = initialList.size();
       if(hasNext()){  
           if(size <= (i + setPageSize)){
               startNumber = i;
               i = size;
           }
       else{
               i = (i + setPageSize);
               startNumber = (i - setPageSize);
           }

       for(integer start = startNumber; start < i; start++){
               nextList.add(initialList[start]);
          }
       } 
       return nextList;
   } 



   //***Method to fetch previous set of results
   public List<Someclass.InnerWrapperClass> previous(){      
       nextList = new List<Someclass.InnerWrapperClass>(); 
       integer size = initialList.size(); 
       if(i == size){
         if(Math.mod(size, setPageSize) > 0){    
               i = size - Math.mod(size, setPageSize);
         }
         else{
               i = (size - setPageSize);
             } 
       }
       else{
           i = (i - setPageSize);
           }

       for(Integer start = (i - setPageSize); start < i; ++start){
           nextList.add(initialList[start]);
       } 
       return nextList;
   }   
}

This custom iterable class is called by an another class where we pass the complete wrapper list in the "initialList" variable stated above.

for(Product__c s : prods){
          initialproductsList.add(new InnerWrapperClass(s,false));
      }

      obj = new NewClass(initialproductsList);
      obj.setPageSize  = PAGE_SIZE;
      next();
  • you might want to post what you've tried for first() and last() – cropredy Apr 21 '16 at 14:53
0

Your custom logic structures are a bit hard to follow, but it seems like first() and last() methods would function identically to your next() method, only you'd need to set the startNumber to 0 or initialList.size()-setPageSize-1, respectively

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.