~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

Viewing changes to foswiki/lib/Foswiki/ListIterator.pm

  • Committer: James Michael DuPont
  • Date: 2009-07-18 19:58:49 UTC
  • Revision ID: jamesmikedupont@gmail.com-20090718195849-vgbmaht2ys791uo2
added foswiki

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# See bottom of file for license and copyright information
 
2
 
 
3
=begin TML
 
4
 
 
5
---+ package Foswiki::ListIterator
 
6
*implements* Foswiki::Iterator
 
7
 
 
8
Iterator over a perl list
 
9
 
 
10
=cut
 
11
 
 
12
package Foswiki::ListIterator;
 
13
use base 'Foswiki::Iterator';
 
14
 
 
15
use strict;
 
16
 
 
17
=begin TML
 
18
 
 
19
---++ new(\@list)
 
20
 
 
21
Create a new iterator over the given list. Designed primarily for operations
 
22
over fully defined lists of object references. The list is not damaged in
 
23
any way.
 
24
 
 
25
=cut
 
26
 
 
27
sub new {
 
28
    my ( $class, $list ) = @_;
 
29
    my $this = bless(
 
30
        {
 
31
            list    => $list,
 
32
            index   => 0,
 
33
            process => undef,
 
34
            filter  => undef,
 
35
            next    => undef,
 
36
        },
 
37
        $class
 
38
    );
 
39
    return $this;
 
40
}
 
41
 
 
42
=begin TML
 
43
 
 
44
---++ hasNext() -> $boolean
 
45
 
 
46
Returns false when the iterator is exhausted.
 
47
 
 
48
<verbatim>
 
49
my $it = new Foswiki::ListIterator(\@list);
 
50
while ($it->hasNext()) {
 
51
   ...
 
52
</verbatim>
 
53
 
 
54
=cut
 
55
 
 
56
sub hasNext {
 
57
    my ($this) = @_;
 
58
    return 1 if $this->{next};
 
59
    my $n;
 
60
    do {
 
61
        if ( $this->{list} && $this->{index} < scalar( @{ $this->{list} } ) ) {
 
62
            $n = $this->{list}->[ $this->{index}++ ];
 
63
        }
 
64
        else {
 
65
            return 0;
 
66
        }
 
67
    } while ( $this->{filter} && !&{ $this->{filter} }($n) );
 
68
    $this->{next} = $n;
 
69
    return 1;
 
70
}
 
71
 
 
72
=begin TML
 
73
 
 
74
---++ next() -> $data
 
75
 
 
76
Return the next entry in the list.
 
77
 
 
78
The iterator object can be customised to pre- and post-process entries from
 
79
the list before returning them. This is done by setting two fields in the
 
80
iterator object:
 
81
 
 
82
   * ={filter}= can be defined to be a sub that filters each entry. The entry
 
83
     will be ignored (next() will not return it) if the filter returns false.
 
84
   * ={process}= can be defined to be a sub to process each entry before it
 
85
     is returned by next. The value returned from next is the value returned
 
86
     by the process function.
 
87
 
 
88
For example,
 
89
<verbatim>
 
90
my @list = ( 1, 2, 3 );
 
91
 
 
92
my $it = new Foswiki::ListIterator(\@list);
 
93
$it->{filter} = sub { return $_[0] != 2 };
 
94
$it->{process} = sub { return $_[0] + 1 };
 
95
while ($it->hasNext()) {
 
96
    my $x = $it->next();
 
97
    print "$x, ";
 
98
}
 
99
</verbatim>
 
100
will print
 
101
<verbatim>
 
102
2, 4
 
103
</verbatim>
 
104
 
 
105
=cut
 
106
 
 
107
sub next {
 
108
    my $this = shift;
 
109
    $this->hasNext();
 
110
    my $n = $this->{next};
 
111
    $this->{next} = undef;
 
112
    $n = &{ $this->{process} }($n) if $this->{process};
 
113
    return $n;
 
114
}
 
115
 
 
116
1;
 
117
__DATA__
 
118
# Module of Foswiki - The Free and Open Source Wiki, http://foswiki.org/
 
119
#
 
120
# Copyright (C) 2008-2009 Foswiki Contributors. All Rights Reserved.
 
121
# Foswiki Contributors are listed in the AUTHORS file in the root
 
122
# of this distribution. NOTE: Please extend that file, not this notice.
 
123
#
 
124
# Additional copyrights apply to some or all of the code in this
 
125
# file as follows:
 
126
#
 
127
# Copyright (C) 2000-2007 TWiki Contributors. All Rights Reserved.
 
128
# TWiki Contributors are listed in the AUTHORS file in the root
 
129
# of this distribution. NOTE: Please extend that file, not this notice.
 
130
#
 
131
# This program is free software; you can redistribute it and/or
 
132
# modify it under the terms of the GNU General Public License
 
133
# as published by the Free Software Foundation; either version 2
 
134
# of the License, or (at your option) any later version. For
 
135
# more details read LICENSE in the root of this distribution.
 
136
#
 
137
# This program is distributed in the hope that it will be useful,
 
138
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
139
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 
140
#
 
141
# As per the GPL, removal of this notice is prohibited.