MatthijsFontys

Empty iterator example

Jan 21st, 2021
566
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class MenuItem{
  2.  
  3.     private string _name;
  4.  
  5.     public MenuItem(string name){
  6.         _name = name;
  7.     }
  8.    
  9.    
  10.     public IEnumerable CreateSingleIterator(){
  11.         return new SingleIterator(this);
  12.     }
  13.  
  14. }
  15.  
  16.  
  17.  
  18. public class SingleIterator : IEnumerable {
  19.    
  20.     private MenuItem _menuItem;
  21.     private bool _isCalled;
  22.    
  23.     public SingleIterator(MenuItem menuItem) {
  24.         _menuItem = menuItem;
  25.         _isCalled = false;
  26.     }
  27.    
  28.     public bool HasNext(){
  29.         return _isCalled;
  30.     }
  31.    
  32.     public MenuItem Next(){
  33.         if(_isCalled){
  34.             _isCalled = true;
  35.             return _menuItem;
  36.         }
  37.         else{
  38.             throw new IllegalStateException("Im out of elements");
  39.         }
  40.     }
  41.  
  42. }
RAW Paste Data