comparison lib/IMPL/Object/List.pm @ 407:c6e90e02dd17 ref20150831

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