annotate lib/IMPL/Object/List.pm @ 422:b0481c071bea ref20150831

IMPL::Config::Container tests, YAMLConfiguration now works and tested
author cin
date Sun, 20 Aug 2017 00:20:41 +0300
parents c6e90e02dd17
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
407
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
1 package IMPL::Object::List;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
2 use strict;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
3 use warnings;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
4
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
5 use Carp qw(carp);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
6 use parent qw(IMPL::Object::ArrayObject);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
7 require IMPL::Exception;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
8
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
9 sub as_list {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
10 return $_[0];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
11 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
12
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
13 sub CTOR {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
14 my ($this,$data) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
15
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
16 if ($data) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
17 die new IMPL::InvalidArgumentException("The parameter should be a reference to an array") unless UNIVERSAL::isa($data,"ARRAY");
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
18 @$this = @$data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
19 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
20 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
21
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
22 sub Append {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
23 carp "Appen method is obsolete use Push instead";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
24 push @{$_[0]}, @_[1 .. $#_];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
25 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
26
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
27 sub Push {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
28 push @{$_[0]}, @_[1 .. $#_];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
29 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
30
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
31 sub AddLast {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
32 carp "Appen method is obsolete use Push instead";
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
33 push @{$_[0]}, @_[1 .. $#_];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
34 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
35
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
36 sub RemoveLast {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
37 return pop @{$_[0]};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
38 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
39
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
40 sub AddFirst {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
41 return unshift @{$_[0]}, $_[1];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
42 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
43
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
44 sub RemoveFirst {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
45 return shift @{$_[0]};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
46 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
47
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
48 sub Count {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
49 return scalar @{$_[0]};
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
50 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
51
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
52 sub Item {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
53 return $_[0]->[$_[1]];
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
54 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
55
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
56 sub InsertAt {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
57 my ($this,$index,@val) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
58
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
59 splice @$this,($index||0),0,@val;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
60 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
61
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
62 sub RemoveAt {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
63 my ($this,$index,$count) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
64
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
65 $count ||= 1;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
66
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
67 return splice @$this,$index,$count;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
68 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
69
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
70 sub RemoveItem {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
71 my ($this,$item) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
72
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
73 @$this = grep $_ != $item, @$this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
74
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
75 return $this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
76 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
77
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
78 sub RemoveItemStr {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
79 my ($this,$item) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
80
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
81 @$this = grep $_ ne $item, @$this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
82
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
83 return $this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
84 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
85
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
86 sub FindItem {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
87 my ($this,$item) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
88
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
89 for (my $i = 0; $i < @$this; $i++ ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
90 return $i if $this->[$i] == $item
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
91 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
92 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
93 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
94
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
95 sub FindItemStr {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
96 my ($this,$item) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
97
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
98 for (my $i = 0; $i < @$this; $i++ ) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
99 return $i if $this->[$i] eq $item
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
100 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
101 return undef;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
102 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
103
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
104 sub save {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
105 my ($this,$ctx) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
106
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
107 $ctx->AddVar( item => $_ ) foreach @$this;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
108 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
109
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
110 sub restore {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
111 my ($class,$data,$surrogate) = @_;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
112
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
113 my $i = 0;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
114
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
115 if ($surrogate) {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
116 @$surrogate = grep { ($i++)%2 } @$data;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
117 } else {
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
118 $surrogate = $class->new([grep { ($i++)%2 } @$data]);
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
119 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
120
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
121 return $surrogate;
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
122 }
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
123
c6e90e02dd17 renamed Lib->lib
cin
parents:
diff changeset
124 1;