Home > Software design >  How can I take a list from a method inside a class X, and using it inside another method in class Y
How can I take a list from a method inside a class X, and using it inside another method in class Y

Time:12-06

I want to make a navigation through some events that are store in:

Page pageOfEventsDto = this.eventService.findAllEvents(code, filtre, page);

I need to take this object in order to iterate and open the events in the list.

 public class X {
    
    @GetMapping(value = "/evenement/lister")
    public String findAllCarEvents(Authentication user, Model model, @ModelAttribute(name = "filtre") FiltreRechercheAvanceeTramwayDto filtre,
                HttpServletResponse response) throws IOException, ServletException {
    
    
   // ...
     PageRequest page = this.getPagingAttribute(filtre);
     Page<EventForListDto> pageOfEventsDto = this.eventService.findAllEvents(code, filtre, page);
   // ...
    }

    public class Y {

     @GetMapping(value = "/modification/navigateur")
     @CanCreateTramwayEvent
     public String navigerEvenementCar(Model model, @RequestParam(name = "id", required = true) long id, Optional<EventForSaveDto> optEvent) {
    
   // I want to use here "pageOfEventsDto" drom Class X, findAllCarEvents() method
    }

I want to use the pageOfEventsDto in method navigerEvenementCar(). I want to use same instance of the list.

Maybe can I use a 3rd method?

CodePudding user response:

What you could do is add your list as a private global variable, and instantiate if for use within class X as well as provide a public getter method to retrieve the list so that it can be used not only in class Y but other classes too (if need be). From there you could access the list as required.

Hope this helps :)

       public class X {
            private Page<EventForListDto> pageOfEventsDto;
            
            @GetMapping(value = "/evenement/lister")
            public String findAllCarEvents(Authentication user, Model model, @ModelAttribute(name = "filtre") FiltreRechercheAvanceeTramwayDto filtre,
                        HttpServletResponse response) throws IOException, ServletException {
            
            
           // ...
             PageRequest page = this.getPagingAttribute(filtre);
             pageOfEventsDto = this.eventService.findAllEvents(code, filtre, page);
           // ...
            }

            public Page<EventForListDto> getPageOfEventsDto() {
                return this.pageOfEventsDto;
            }
       }

    
    
        
       public class Y {
        
           @GetMapping(value = "/modification/navigateur")
           @CanCreateTramwayEvent
           public String navigerEvenementCar(Model model, @RequestParam(name = "id", required = true) long id, Optional<EventForSaveDto> optEvent) {
            
              Page<EventForListDto> pageOfEventsDto = new X().getPageOfEventsDto();

           }

       }

This will create and new reference of the pageOfEventsDto Object but will refer to the same instance in memory.

CodePudding user response:

To use a list from a method inside one class in another method in a different class, you can pass the list as a parameter to the second method.

For example, you could change the navigerEvenementCar method to accept a List of EventForSaveDto as a parameter, and pass the pageOfEventsDto list from the findAllCarEvents method as an argument when calling the navigerEvenementCar method.

Here is how that might look:

public class X {
  @GetMapping(value = "/evenement/lister")
  public String findAllCarEvents(Authentication user, Model model, @ModelAttribute(name = "filtre") FiltreRechercheAvanceeTramwayDto filtre,
              HttpServletResponse response) throws IOException, ServletException {
    // ...
    PageRequest page = this.getPagingAttribute(filtre);
    Page<EventForSaveDto> pageOfEventsDto = this.eventService.findAllEvents(siren, filtre, page);

    // Pass the list of EventForSaveDto to the navigerEvenementCar method
    Y.navigerEvenementCar(pageOfEventsDto);
  }
}

public class Y {
  @GetMapping(value = "/modification/navigateur")
  @CanCreateTramwayEvent
  public static String navigerEvenementCar(List<EventForSaveDto> eventsDto, Model model, @RequestParam(name = "id", required = true) long id, Optional<EventForSaveDto> optEvent) {
    // Use the eventsDto list here
  }
}

Note that in this example, the navigerEvenementCar method has been changed to be a static method, so that it can be called from the findAllCarEvents method without needing to create an instance of the Y class. This is just one way to accomplish this, and there are other ways to do it as well.

  • Related