Why traverse listener lists backwards?

Discussion in 'Java' started by J Krugman, Aug 4, 2004.

  1. J Krugman

    J Krugman Guest

    Looking at Swing code I'm surprised that, invariable, when a list
    of event listeners needs to be traversed, it is done from last to
    first. Why is this?

    Thanks,

    jill

    --
    To s&e^n]d me m~a}i]l r%e*m?o\v[e bit from my a|d)d:r{e:s]s.
    J Krugman, Aug 4, 2004
    #1
    1. Advertising

  2. J Krugman

    xarax Guest

    "J Krugman" <> wrote in message
    news:cequg7$r90$...
    >
    >
    >
    > Looking at Swing code I'm surprised that, invariable, when a list
    > of event listeners needs to be traversed, it is done from last to
    > first. Why is this?


    Unless the JavaDoc specifies the order in which listeners
    are called, you cannot rely on the order (LIFO versus FIFO)
    of calling listeners. Different vendors make provide
    different implementations, and thus call the listeners in
    a different order. So, do not design your listeners to
    presume a certain order of calling, unless the JavaDoc
    of the Swing API specifies a certain order.
    xarax, Aug 5, 2004
    #2
    1. Advertising

  3. J Krugman

    Jacob Guest

    J Krugman wrote:

    > Looking at Swing code I'm surprised that, invariable, when a list
    > of event listeners needs to be traversed, it is done from last to
    > first. Why is this?


    I don't know the specific case, but in general (as a pattern)
    it is useful to traverse this kind of lists backwards in case
    the accessed element is deleted along the way. A delete during
    a traverse normally affects the remainder of the list, and a
    forward iteration might easily be messed up.

    As this makes assumptions about the implementation of the
    underlaying structure, it is in general better to iterate over
    a *copy* of the list.
    Jacob, Aug 5, 2004
    #3
  4. J Krugman <> writes:

    > Looking at Swing code I'm surprised that, invariable, when a list
    > of event listeners needs to be traversed, it is done from last to
    > first. Why is this?


    This allows later added listeners (that is, the developer's own
    listeners) to e.g. consume the event, so that the default behaviour
    can be superseded by the developer's code. If it was the other way
    around, then the "standard" listeners would trigger first.
    Tor Iver Wilhelmsen, Aug 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    493
  2. =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==

    List of lists of lists of lists...

    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==, May 8, 2006, in forum: Python
    Replies:
    5
    Views:
    390
    =?UTF-8?B?w4FuZ2VsIEd1dGnDqXJyZXogUm9kcsOtZ3Vleg==
    May 15, 2006
  3. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,763
    Smokey Grindel
    Dec 2, 2006
  5. Protoman
    Replies:
    17
    Views:
    594
    Andre Kostur
    Dec 1, 2005
Loading...

Share This Page