File Coverage

blib/lib/Array/Iterator/BiDirectional.pm
Criterion Covered Total %
statement 39 39 100.0
branch 20 20 100.0
condition n/a
subroutine 10 10 100.0
pod 4 7 57.1
total 73 76 96.0


line stmt bran cond sub pod time code
1              
2             package Array::Iterator::BiDirectional;
3              
4 1     1   70905 use strict;
  1         12  
  1         27  
5 1     1   5 use warnings;
  1         2  
  1         24  
6              
7 1     1   454 use Array::Iterator;
  1         3  
  1         442  
8              
9             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10             our $DATE = '2021-09-26'; # DATE
11             our $DIST = 'Array-Iterator'; # DIST
12             our $VERSION = '0.131'; # VERSION
13              
14             our @ISA = qw(Array::Iterator);
15              
16             sub has_previous {
17 17     17 1 116 my ($self, $n) = @_;
18              
19 17 100       51 if(not defined $n) { $n = 1 }
  10 100       16  
    100          
20 1         9 elsif(not $n) { die "has_previous(0) doesn't make sense, did you mean current()?" }
21 1         10 elsif($n < 0) { die "has_previous() with negative argument doesn't make sense, did you mean has_next()?" }
22              
23 15         36 my $idx = $self->_current_index - $n;
24              
25 15 100       56 return ($idx > 0) ? 1 : 0;
26             }
27              
28 10     10 0 1749 sub hasPrevious { my $self = shift; $self->has_previous(@_) }
  10         24  
29              
30             sub previous {
31 5     5 1 1650 my ($self) = @_;
32 5 100       15 (($self->_current_index - 1) > 0)
33             || die "Out Of Bounds : no more elements";
34 4         12 $self->_iterated = 1;
35 4         10 return $self->_getItem($self->_iteratee, --$self->_current_index);
36             }
37              
38             sub get_previous {
39 5     5 1 8 my ($self) = @_;
40 5 100       14 return undef unless (($self->_current_index - 1) > 0); ## no critic: Subroutines::ProhibitExplicitReturnUndef
41 4         10 $self->_iterated = 1;
42 4         10 return $self->_getItem($self->_iteratee, --$self->_current_index);
43             }
44              
45 5     5 0 1747 sub getPrevious { my $self = shift; $self->get_previous(@_) }
  5         15  
46              
47             sub look_back {
48 14     14 1 101 my ($self, $n) = @_;
49              
50 14 100       46 if(not defined $n) { $n = 1 }
  5 100       9  
    100          
51 1         10 elsif(not $n) { die "look_back(0) doesn't make sense, did you mean get_previous()?" }
52 1         10 elsif($n < 0) { die "look_back() with negative argument doesn't make sense, did you mean get_next()?" }
53              
54 12         32 my $idx = $self->_current_index - ($n + 1);
55              
56 12 100       41 return undef unless ($idx > 0); ## no critic: Subroutines::ProhibitExplicitReturnUndef
57 7         20 $self->_iterated = 1;
58 7         18 return $self->_getItem($self->_iteratee, $idx);
59             }
60              
61 4     4 0 8 sub lookBack { my $self = shift; $self->look_back(@_) }
  4         10  
62              
63             1;
64             # ABSTRACT: A subclass of Array::Iterator to allow forwards and backwards iteration
65              
66             __END__