This discussion is archived
1 2 Previous Next 19 Replies Latest reply: Dec 13, 2012 1:18 AM by TPD-Opitz-Consulting-com Go to original post RSS
  • 15. Re: how to optimize this code + is design proper
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Vicky wrote:
    Thanks TPD
    But a full fleged code might have been better to understand.
    Shortly I was blamed for posting code, but that was in another thread...
    So here is what I mean:
    public class Links{
         public static final Filter<Links> UNFILTERED= new Filter<Links>() {
             public boolean accept(Links aLink) { return true;}
         };
         private Filter<Links> currentFilter = UNFILTERED;
         private String nodeName;
         private List<Links> children;
    
         public void setFilter( Filter<Links> newFilter){
                    assert(null!=newFilter);
              this.currentFilter=newFilter;
         }
    
         public List<Links> getChildren(){
                    List<Links> filteredLinks = new ArrayList<>();
              for (Links currentLink : children) {
                 if(this.currentFilter.accept(currentLink) {
                            currentLink.setFilter(this.currentFilter);
                            filteredLinks.add(currentLink);
                 }
              }
              return filteredLinks;
         }
    // more methods ...
    }
    I for myself also like my setter methods to return this
     public Links setFilter( Filter<Links> newFilter){
                    assert(null!=newFilter);
              this.currentFilter=newFilter;
                    return this;
         }
    so I can write
    filteredLinks.add(currentLink.setFilter(this.currentFilter)); 
    You may be concerned about creating to many instances of <tt>ArrayList</tt> but they live for a verry short time and the GC is optimized for handling of short living objects. Also generally you should avoid returning a reference to a mutable member by any chance because this turnes the member into a global variable, which is not a good idea.

    bye
    TPD
  • 16. Re: how to optimize this code + is design proper
    814889 Newbie
    Currently Being Moderated
    Thanks TPD


    May be i am asking to much but i wont be able to understand until i am able to run the code , the code was insufficient to run so if you dont mind can you post full code which i can run i will fill data myself.
  • 17. Re: how to optimize this code + is design proper
    814889 Newbie
    Currently Being Moderated
    still some doubts left
  • 18. Re: how to optimize this code + is design proper
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    hello Vicky,

    I'm willing to explain concepts even by posting code, but you should be able to use basic computer handlig concepts like copy and paste and basic programming concepts like how to call a method and how to pass parameters already.
    I will not do that part for you.

    But if you post what you made from the input given here I may take time to comment that...

    bye
    TPD
  • 19. Re: how to optimize this code + is design proper
    TPD-Opitz-Consulting-com Expert
    Currently Being Moderated
    Vicky wrote:
    still some doubts left
    which are?

    bye
    TPD
1 2 Previous Next

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points