~kosova/+junk/tuxfamily-twiki

« back to all changes in this revision

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