ArrayList.pm

filter: Statementstmt 100% 216 / 216 Branchbran 100% 66 / 66 Conditioncond 90.4% 19 / 21 MC/DCmcdc 83.3% 10 / 12 Subroutinesub 100% 44 / 44 Podpod 94.7% 18 / 19 totaltotal 98.6% 373 / 378 CC 44 SCAR 9.6
CC
44
Cov
99%
subList
5.0
_checkSubListRange
4.1
__ANON__
4.0
CRAP 2.6
↑ Summary ArrayList/ListIterator.pm »
1 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::ArrayList;
4 4 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 $Data::ArrayList::VERSION = '0.01';
6 }
7 # ABSTRACT: java.util.ArrayList for perl
8  
9 4 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 ▶use Data::Clone ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 4 ▶use Data::ArrayList::ListIterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14  
15 has '_data' => (
16 is => 'ro',
17 isa => 'ArrayRef[Any]',
18 traits => [qw( Array )],
19 21 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
20 4 handles => {
21 '_addAt' => 'splice',
22 '_set' => 'set',
23 '_get' => 'get',
24 '_clear' => 'clear',
25 '_delete' => 'delete',
26 },
27 );
28  
29 4 has '_size' => (
30 is => 'rw',
31 isa => 'Int',
32 traits => [qw( Counter )],
33 default => 0,
34 handles => {
35 '_inc_size' => 'inc',
36 '_dec_size' => 'dec',
37 '_reset_size' => 'reset',
38 },
39 );
40  
41 4 has '_mod_count' => (
42 is => 'rw',
43 isa => 'Int',
44 traits => [qw( Counter )],
45 default => 0,
46 handles => {
47 '_inc_modcount' => 'inc',
48 },
49 );
50  
51 5 ▶around BUILDARGS => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
52 5 my $next = shift;
53 5 my $class = shift;
54  
55 5 ▶ return $class->$next({ _initialCapacity => @_ || 10 });
Condition100%2/2
@_ || 10
l!l
14
MC/DC100%1/1
@_ || 10
@_
Truth table100%2/2
A
A= @_
Aresult
11
00
56 4 };
57  
58 21 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
59 21 my ( $self, $params ) = @_;
60  
61 # ensureCapacity only on parent objects
62 21 ▶ if ( ref $self eq __PACKAGE__ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self eq __PACKAGE__) 5 16 21
63 5 $self->ensureCapacity( $params->{_initialCapacity} );
64 };
65  
66 21 return $self;
67 };
68  
69  
70 16 ▶sub add {
Subroutine add: called CC=2 SCAR=6.9
Pod documented
71 16 my $self = shift;
72  
73 16 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 15 16
74 unless scalar @_;
75  
76 15 $self->_addAt( $self->_size, 0, @_ );
77  
78 15 $self->_inc_size( scalar @_ );
79 15 $self->_inc_modcount;
80  
81 15 return 1;
82 }
83  
84  
85 17 ▶sub addAt {
Subroutine addAt: called CC=3 SCAR=11.0
Pod documented
86 17 my $self = shift;
87 17 my $index = shift;
88  
89 17 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndexForAdd($index) 2 15 17
90 unless $self->_checkIndexForAdd( $index );
91  
92 15 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 2 13 15
93 unless scalar @_;
94  
95 13 $self->_addAt( $index, 0, @_ );
96  
97 13 $self->_inc_size( scalar @_ );
98 13 $self->_inc_modcount;
99  
100 13 return 1;
101 }
102  
103  
104 191 ▶sub get {
Subroutine get: called CC=2 SCAR=6.9
Pod documented
105 191 my $self = shift;
106 191 my $index = shift;
107  
108 191 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 3 188 191
109 unless $self->_checkIndex( $index );
110  
111 188 return $self->_get($index);
112 }
113  
114  
115 7 ▶sub addAll {
Subroutine addAll: called CC=1 SCAR=0.0
Pod documented
116 7 shift->add( @_ );
117 }
118  
119  
120 1 ▶sub addAllAt {
Subroutine addAllAt: called CC=1 SCAR=0.0
Pod documented
121 1 shift->addAt( @_ );
122 }
123  
124  
125 5 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
126 5 my $self = shift;
127  
128 5 $self->_clear;
129 5 $self->_reset_size;
130 5 $self->_inc_modcount;
131 }
132  
133  
134  
135 1 ▶sub isEmpty {
Subroutine isEmpty: called CC=1 SCAR=0.0
Pod documented
136 1 return $_[0]->size == 0;
137 }
138  
139  
140 5 ▶sub indexOf {
Subroutine indexOf: called CC=3 SCAR=11.0
Pod documented
141 5 my $self = shift;
142 5 my $comparator = shift;
143  
144 5 for my $i ( 0 .. ($self->size - 1) ) {
145 22 local *_ = \( $self->get($i) );
146 22 ▶ return $i if $comparator->();
Branch: 1 branch
BranchTrueFalseTotal
if &$comparator() 3 19 22
147 }
148 2 return -1;
149 }
150  
151  
152 2 ▶sub lastIndexOf {
Subroutine lastIndexOf: called CC=3 SCAR=11.0
Pod documented
153 2 my $self = shift;
154 2 my $comparator = shift;
155  
156 2 for my $i ( reverse 0 .. ($self->size - 1) ) {
157 8 local *_ = \( $self->get($i) );
158 8 ▶ return $i if $comparator->();
Branch: 1 branch
BranchTrueFalseTotal
if &$comparator() 1 7 8
159 }
160 1 return -1;
161 }
162  
163  
164 2 ▶sub contains {
Subroutine contains: called CC=1 SCAR=0.0
Pod documented
165 2 shift->indexOf(@_) >= 0;
166 }
167  
168  
169 582 ▶sub size {
Subroutine size: called CC=1 SCAR=0.0
Pod documented
170 582 return $_[0]->_size;
171 }
172  
173  
174 1 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
175 1 my $self = shift;
176  
177 1 return bless { %$self }, ref $self;
178 }
179  
180  
181 3 ▶sub toArray {
Subroutine toArray: called CC=1 SCAR=0.0
Pod documented
182 3 return @{ Data::Clone::clone($_[0]->_data) };
183 }
184  
185  
186 16 ▶sub set {
Subroutine set: called CC=3 SCAR=11.0
Pod documented
187 16 my $self = shift;
188 16 my $index = shift;
189  
190 16 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 15 16
191 unless scalar @_;
192  
193 15 my $value = shift;
194  
195 15 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 2 13 15
196 unless $self->_checkIndex( $index );
197  
198 13 my $old = $self->get($index);
199 13 $self->_set( $index, $value );
200  
201 13 return $old;
202 }
203  
204  
205 8 ▶sub ensureCapacity {
Subroutine ensureCapacity: called CC=3 SCAR=11.0
Pod documented
206 8 my $self = shift;
207 8 my $capacity = shift;
208  
209 8 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless $capacity 1 7 8
210 unless $capacity;
211  
212 7 my $size = $self->size;
213 7 ▶ if ( $capacity > $size ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($capacity > $size) 6 1 7
214 6 my $d = $self->_data;
215 6 $d->[$capacity] = undef;
216 6 delete $d->[$capacity];
217 6 return 1;
218 };
219 }
220  
221  
222 7 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
223 7 my $self = shift;
224  
225 7 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 6 7
226 unless scalar @_;
227 6 my $index = shift;
228  
229 6 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 2 4 6
230 unless $self->_checkIndex( $index );
231  
232 4 my $old = $self->get($index);
233 4 $self->_delete( $index );
234  
235 4 $self->_dec_size;
236 4 $self->_inc_modcount;
237  
238 4 return $old;
239 }
240  
241 2 ▶sub _removeRange {
Subroutine _removeRange: called CC=1 SCAR=0.0
242 2 my $self = shift;
243 2 my $rangeFrom = shift;
244 2 my $rangeTo = shift;
245  
246  
247 2 $self->_addAt( $rangeFrom, $rangeTo - $rangeFrom );
248  
249 2 $self->_dec_size( $rangeTo - $rangeFrom );
250 2 $self->_inc_modcount;
251  
252 2 return 1;
253 }
254  
255  
256 16 ▶sub listIterator {
Subroutine listIterator: called CC=3 SCAR=11.0
Pod documented
257 16 my $self = shift;
258  
259 16 ▶ my $initialPosition = shift || 0;
Condition100%2/2
shift() || 0
l!l
214
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
260  
261 16 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($initialPosition) 1 15 16
262 unless $self->_checkIndex( $initialPosition );
263  
264 15 my $iter = Data::ArrayList::ListIterator->new(
265 _mod_count => $self->_mod_count,
266 _parent => $self,
267 _cursor => $initialPosition,
268 );
269  
270 15 return $iter;
271 }
272  
273  
274 19 ▶sub subList {
Subroutine subList: called CC=5 SCAR=16.1
Pod documented
275 19 my $SELF = shift;
276 19 my $rangeFrom = shift;
277 19 my $rangeTo = shift;
278 19 ▶ my $offset = shift || 0;
Condition100%2/2
shift() || 0
l!l
109
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
279  
280 19 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rangeFrom and $rangeTo 2 17 19
Condition100%3/3
defined $rangeFrom and $rangeTo
!ll&&!rl&&r
1117
MC/DC100%2/2
defined $rangeFrom and $rangeTo
defined $rangeFrom $rangeTo
Truth table100%3/3
A and B
A= defined $rangeFromB= $rangeTo
ABresult
0X0
100
111
281 unless defined $rangeFrom
282 && $rangeTo;
283  
284 17 ▶ die "IndexOutOfBounds" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $SELF->_checkSubListRange($rangeFrom, $rangeTo) 1 16 17
285 $SELF->_checkSubListRange( $rangeFrom, $rangeTo );
286  
287 16 my $sl_meta = Moose::Meta::Class->create_anon_class(
288 superclasses => [ $SELF->meta->name ],
289 );
290  
291 16 $sl_meta->add_attribute(
292 _parentOffset => (
293 isa => 'Int',
294 default => $rangeFrom,
295 is => 'ro',
296 )
297 );
298 16 $sl_meta->add_attribute(
299 _offset => (
300 isa => 'Int',
301 default => $offset + $rangeFrom,
302 is => 'ro',
303 )
304 );
305 $sl_meta->add_attribute(
306 _parent => (
307 isa => 'Data::ArrayList',
308 16 ▶ default => sub { $SELF },
Subroutine __ANON__: called CC=1 SCAR=0.0
309 16 is => 'rw',
310 )
311 );
312 $sl_meta->add_method('_checkCoMod', Class::MOP::Method->wrap(
313 name => '_checkCoMod',
314 package_name => $sl_meta->name,
315 377 ▶ body => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
316 377 return $_[0]->_mod_count == $SELF->_mod_count;
317 },
318 )
319 16 );
320  
321 $sl_meta->add_around_method_modifier('set',
322 7 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
323 7 shift; # next
324 7 my $self = shift;
325  
326 7 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 1 6 7
327 unless scalar @_;
328  
329 6 my $index = shift;
330  
331 6 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 4 6
332 $self->_checkCoMod;
333  
334 4 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 2 2 4
335 unless $self->_checkIndex( $index );
336  
337 2 return $SELF->set( $self->_offset + $index, @_ );
338 }
339 16 );
340 $sl_meta->add_around_method_modifier('get',
341 72 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
342 72 shift; # next
343 72 my $self = shift;
344 72 my $index = shift;
345  
346 72 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 70 72
347 $self->_checkCoMod;
348  
349 70 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 2 68 70
350 unless $self->_checkIndex( $index );
351  
352 68 return $SELF->get( $self->_offset + $index );
353 }
354 16 );
355  
356 $sl_meta->add_around_method_modifier('size',
357 255 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
358 255 my $next = shift;
359 255 my $self = shift;
360  
361 255 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 5 250 255
362 $self->_checkCoMod;
363  
364 250 return $self->$next();
365 }
366 16 );
367 $sl_meta->add_around_method_modifier('add',
368 7 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
369 7 shift; # next
370 7 my $self = shift;
371  
372 7 $self->addAt( $self->size, @_ );
373  
374 5 return 1;
375 }
376 16 );
377  
378 $sl_meta->add_around_method_modifier('addAt',
379 19 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
380 19 shift; # next
381 19 my $self = shift;
382 19 my $index = shift;
383  
384 19 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 17 19
385 $self->_checkCoMod;
386  
387 17 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndexForAdd($index) 2 15 17
388 unless $self->_checkIndexForAdd( $index );
389  
390 15 $self->_parent->addAt( $self->_parentOffset + $index, @_ );
391  
392 12 $self->_inc_size( scalar @_ );
393  
394 12 $self->_mod_count( $self->_parent->_mod_count );
395 }
396 16 );
397 $sl_meta->add_around_method_modifier('remove',
398 11 ▶ sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
399 11 shift; # next
400 11 my $self = shift;
401  
402 11 ▶ die "IllegalArgument"
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @_ 2 9 11
403 unless scalar @_;
404  
405 9 my $index = shift;
406  
407 9 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 7 9
408 $self->_checkCoMod;
409  
410 7 ▶ die "IndexOutOfBounds"
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkIndex($index) 1 6 7
411 unless $self->_checkIndex( $index );
412  
413 6 my $old = $self->_parent->remove( $self->_parentOffset + $index );
414  
415 6 $self->_dec_size();
416  
417 6 $self->_mod_count( $self->_parent->_mod_count );
418  
419 6 return $old;
420 }
421 16 );
422  
423  
424 $sl_meta->add_around_method_modifier('clear',
425 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
426 5 shift; # next
427 5 my $self = shift;
428  
429 5 return $self->_removeRange( 0, $self->size );
430 }
431 16 );
432  
433 $sl_meta->add_around_method_modifier('_removeRange',
434 5 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
435 5 shift; # next
436 5 my $self = shift;
437 5 my $rangeFrom = shift;
438 5 my $rangeTo = shift;
439  
440 5 $self->_parent->_removeRange(
441 $self->_parentOffset + $rangeFrom,
442 $self->_parentOffset + $rangeTo,
443 );
444  
445 5 $self->_mod_count( $self->_parent->_mod_count );
446 5 $self->_dec_size( $rangeTo - $rangeFrom );
447  
448 5 return 1;
449 }
450 16 );
451 $sl_meta->add_around_method_modifier('toArray',
452 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
453 3 shift; # next
454 3 my $self = shift;
455  
456 3 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 1 3
457 $self->_checkCoMod;
458  
459 1 my $d = $SELF->_data;
460 1 return @{ Data::Clone::clone([ @$d[ $self->_offset .. $self->size ]]) };
461 }
462 16 );
463 $sl_meta->add_around_method_modifier('ensureCapacity',
464 2 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
465 2 die "UnsupportedOperationException";
466 }
467 16 );
468 $sl_meta->add_around_method_modifier('subList',
469 13 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
470 13 shift; # next
471 13 my $self = shift;
472 13 my $rangeFrom = shift;
473 13 my $rangeTo = shift;
474  
475 13 ▶ die "ConcurrentModification" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkCoMod 2 11 13
476 $self->_checkCoMod;
477  
478 11 ▶ die "IndexOutOfBounds" unless
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_checkSubListRange($rangeFrom, $rangeTo) 1 10 11
479 $self->_checkSubListRange( $rangeFrom, $rangeTo );
480  
481 10 my $sublist = $SELF->subList($rangeFrom, $rangeTo, $self->_offset);
482 10 $sublist->_parent( $self );
483  
484 10 return $sublist;
485 }
486 16 );
487  
488 16 $sl_meta->make_immutable;
489  
490 16 my $sublist = $sl_meta->new_object();
491  
492 16 $sublist->_size($rangeTo - $rangeFrom);
493 16 $sublist->_mod_count($SELF->_mod_count);
494  
495 16 return $sublist;
496 }
497  
498 309 ▶sub _checkIndex {
Subroutine _checkIndex: called CC=3 SCAR=11.0
499 309 my $self = shift;
500 309 my $index = shift;
501  
502 309 ▶ return $index < 0 || $index >= $self->size ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$index < 0 || $index >= $self->size ? : 13 296 309
Condition100%3/3
$index < 0 or $index >= $self->size
l!l&&r!l&&!r
58296
MC/DC100%2/2
$index < 0 or $index >= $self->size
$index < 0 $index >= $self->size
Truth table100%3/3
A or B
A= $index < 0B= $index >= $self->size
ABresult
1X1
011
000
503 }
504  
505 34 ▶sub _checkIndexForAdd {
Subroutine _checkIndexForAdd: called CC=3 SCAR=11.0
506 34 my $self = shift;
507 34 my $index = shift;
508  
509 34 ▶ return $index < 0 || $index > $self->size ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$index < 0 || $index > $self->size ? : 4 30 34
Condition100%3/3
$index < 0 or $index > $self->size
l!l&&r!l&&!r
2230
MC/DC100%2/2
$index < 0 or $index > $self->size
$index < 0 $index > $self->size
Truth table100%3/3
A or B
A= $index < 0B= $index > $self->size
ABresult
1X1
011
000
510 }
511  
512 28 ▶sub _checkSubListRange {
Subroutine _checkSubListRange: called CC=4 SCAR=14.0
513 28 my $self = shift;
514 28 my $rangeFrom = shift;
515 28 my $rangeTo = shift;
516  
517 28 ▶ return $rangeFrom < 0
Branch: 1 branch
BranchTrueFalseTotal
$rangeFrom < 0 || $rangeTo > $self->size || $rangeFrom > $rangeTo ? : 2 26 28
Condition66%4/6
$rangeFrom < 0 or $rangeTo > $self->size or $rangeFrom > $rangeTo
l!l&&r!l&&!r
2026
$rangeFrom < 0 or $rangeTo > $self->size
l!l&&r!l&&!r
0226
MC/DC33%1/3
$rangeFrom < 0 or $rangeTo > $self->size or $rangeFrom > $rangeTo
$rangeFrom < 0 $rangeTo > $self->size $rangeFrom > $rangeTo
Truth table50%2/4
A or B or C
A= $rangeFrom < 0B= $rangeTo > $self->sizeC= $rangeFrom > $rangeTo
ABCresult
1XX1
01X1
0011
0000
518 || $rangeTo > $self->size
519 || $rangeFrom > $rangeTo ? 0 : 1;
520 }
521  
522  
523 4 __PACKAGE__->meta->make_immutable;
524  
525 4 1;
526  
527 __END__
↑ Summary ArrayList/ListIterator.pm »