root/lu/LUMap.lua

Revision 814 (checked in by rsz, 4 years ago)

cleanup

Line 
1 --------------------------------------------------------------------------------
2 -- Title:               LUMap.lua
3 -- Description:         Like a square peg in a round hole
4 -- Author:              Raphaël Szwarc http://alt.textdrive.com/lua/
5 -- Creation Date:       February 1, 2005
6 -- Legal:               Copyright (C) 2005 Raphaël Szwarc
7 --------------------------------------------------------------------------------
8
9 -- import dependencies
10 local LUObject = require( "LUObject" )
11 local LUObjectReader = require( "LUObjectReader" )
12 local LUObjectWriter = require( "LUObjectWriter" )
13 local LUList = require( "LUList" )
14 local LUString = require( "LUString" )
15 local io = require( "io" )
16 local table = require( "table" )
17
18 -- define the class
19 local super = LUObject
20 local self = super()
21
22 -- method for initialization
23 function self:init( aContent, aMode )
24         self = super.init( self )
25
26         if aMode ~= nil then
27                 setmetatable( self:content(), { __mode = aMode } )
28         end
29        
30         self:putAll( aContent )
31
32         return self
33 end
34
35 -- method to clear this map
36 function self:clear()
37         for aKey, aValue in self:iterator() do
38                 self:remove( aKey )
39         end
40        
41         return self
42 end
43
44 -- method to access this map content
45 function self:content()
46         if self._content == nil then
47                 self._content = {}
48         end
49
50         return self._content
51 end
52
53 -- method to copy this map
54 function self:copy()
55         return self:new():putAll( self )
56 end
57
58 -- method to check if this map contains any data
59 function self:hasData()
60         for aKey, aValue in self:iterator() do
61                 return true
62         end
63        
64         return false
65 end
66
67 -- method to check if this map contains a given key
68 function self:hasKey( aKey )
69         if self:get( aKey ) ~= nil then
70                 return true
71         end
72        
73         return false
74 end
75
76 -- method to check if this map contains a given value
77 function self:hasValue( aValue )
78         if aValue ~= nil then
79                 for aKey, anotherValue in self:iterator() do
80                         if aValue == anotherValue then
81                                 return true, aKey
82                         end
83                 end
84         end
85        
86         return false
87 end
88
89 -- method to return a value for a given key
90 function self:get( aKey )
91         if aKey ~= nil then
92                 return self:content()[ aKey ]
93         end
94        
95         return nil
96 end
97
98 -- method to compare a given object with this map for equality
99 function self:equals( anObject )
100         if self:isKindOf( anObject ) == true then
101                 if self:size() == anObject:size() then
102                         for aKey, aValue in self:iterator() do
103                                 local anotherValue = anObject:get( aKey )
104
105                                 if aValue ~= anotherValue then
106                                         return false
107                                 end
108                         end
109                        
110                         return true
111                 end
112         end
113        
114         return false
115 end
116
117 -- method to access this map iterator
118 function self:iterator()
119         return pairs( self:content() )
120 end
121
122 -- method to access all the keys of this map
123 function self:keys()
124         local someKeys = LUList()
125        
126         for aKey, aValue in self:iterator() do
127                 someKeys:add( aKey )
128         end
129        
130         return someKeys
131 end
132
133 -- method to return all the keys with a given value
134 function self:keysWithValue( aValue )
135         if aValue ~= nil then
136                 local someKeys = LUList()
137                
138                 for aKey, anotherValue in self:iterator() do
139                         if aValue == anotherValue then
140                                 someKeys:add( aKey )
141                         end
142                 end
143                
144                 if someKeys:size() > 0 then
145                         return someKeys
146                 end
147         end
148        
149         return nil
150 end
151
152 -- method to associate a given value with a given key
153 function self:put( aKey, aValue )
154         if aKey ~= nil then
155                 self:content()[ aKey ] = aValue
156         end
157
158         return self
159 end
160
161 -- method to add all the key-value pairs from a given map
162 function self:putAll( aMap )
163         if self:isKindOf( aMap ) == true then
164                 for aKey, aValue in aMap:iterator() do
165                         self:put( aKey, aValue )
166                 end
167         elseif type( aMap ) == "table" then
168                 for aKey, aValue in pairs( aMap ) do
169                         self:put( aKey, aValue )
170                 end
171         end
172        
173         return self
174 end
175
176 -- method to remove a given key
177 function self:remove( aKey )
178         if aKey ~= nil then
179                 self:content()[ aKey ] = nil
180         end
181
182         return self
183 end
184
185 -- method to remove the given keys
186 function self:removeAll( someKeys )
187         if someKeys ~= nil then
188                 for anIndex, aValue in someKeys:iterator() do
189                         self:remove( aValue )
190                 end
191         end
192
193         return self
194 end
195
196 -- method to returns the size of this map
197 function self:size()
198         local aSize = 0
199
200         for aKey, aValue in self:iterator() do
201                 aSize = aSize + 1
202         end
203        
204         return aSize
205 end
206
207 -- method for string representation
208 function self:toString( isFormal )
209         local aBuffer = {}
210        
211         aBuffer[ #aBuffer + 1 ] = "{ "
212        
213         if self:size() > 0 then
214                 for aKey, aValue in self:iterator() do
215                         if isFormal == true then
216                                 aBuffer[ #aBuffer + 1 ] = "[ "
217                         end
218                        
219                         aBuffer[ #aBuffer + 1 ] = LUString:toString( aKey, isFormal )
220
221                         if isFormal == true then
222                                 aBuffer[ #aBuffer + 1 ] = " ]"
223                         end
224                        
225                         aBuffer[ #aBuffer + 1 ] = " = "
226
227                         aBuffer[ #aBuffer + 1 ] = LUString:toString( aValue, isFormal )
228                        
229                         aBuffer[ #aBuffer + 1 ] = ", "
230                 end
231
232                 aBuffer[ #aBuffer ] = nil
233         end
234        
235         aBuffer[ #aBuffer + 1 ] = " }"
236        
237         return table.concat( aBuffer, "" )
238 end
239
240 -- method to access all the values of this map
241 function self:values()
242         local someValues = LUList()
243
244         for aKey, aValue in self:iterator() do
245                 someValues:add( aValue )
246         end
247        
248         return someValues
249 end
250
251 -- method to load this map content from a given location
252 function self:load( aPath )
253         if aPath == nil then
254                 aPath = self._path
255         else
256                 aPath = tostring( aPath )
257                 self._path = aPath
258         end
259
260         if aPath ~= nil then
261                 local aFile = io.open( aPath, "r" )
262                
263                 if aFile ~= nil then
264                         local aContent = aFile:read( "*all" )
265                        
266                         aFile:close()
267                        
268                         if aContent ~= nil then
269                                 self:putAll( LUObjectReader:read( aContent ) )
270                         else
271                                 return nil, ( "invalid content at '%s'" ):format( aPath )
272                         end
273                 else
274                         return nil, ( "invalid path '%s'" ):format( aPath )
275                 end
276         else
277                 return nil, "invalid path"
278         end
279        
280         return self
281 end
282
283 -- method to store this map at a given location
284 function self:store( aPath )
285         if aPath == nil then
286                 aPath = self._path
287         else
288                 aPath = tostring( aPath )
289                 self._path = aPath
290         end
291
292         if aPath ~= nil then
293                 local aFile = io.open( aPath, "w" )
294                
295                 if aFile ~= nil then                       
296                         aFile:write( LUObjectWriter:write( self:content() ) )
297                         aFile:close()
298                 else
299                         return nil, ( "invalid path at '%s'" ):format( aPath )
300                 end
301         else
302                 return nil, "invalid path"
303         end
304        
305         return self
306 end
307
308 return self
Note: See TracBrowser for help on using the browser.