PageRenderTime 19ms CodeModel.GetById 10ms app.highlight 3ms RepoModel.GetById 2ms app.codeStats 0ms

/library/kernel/json_array.e

http://github.com/Eiffel-World/ejson-ise-svn
Specman e | 141 lines | 103 code | 28 blank | 10 comment | 1 complexity | 5854eeed95af408a6fc7f7c2aa873b92 MD5 | raw file
  1note
  2    description: "[
  3            JSON_ARRAY represent an array in JSON.
  4            An array in JSON is an ordered set of names.
  5            Examples
  6            array
  7                []
  8                [elements]
  9            ]"
 10
 11    author: "Javier Velilla"
 12    date: "2008/08/24"
 13    revision: "Revision 0.1"
 14
 15class
 16    JSON_ARRAY
 17
 18inherit
 19    JSON_VALUE
 20
 21    DEBUG_OUTPUT
 22
 23create
 24    make_array
 25
 26feature {NONE} -- Initialization
 27
 28    make_array
 29            -- Initialize JSON Array
 30        do
 31            create values.make (10)
 32        end
 33
 34feature -- Access
 35
 36    i_th alias "[]" (i: INTEGER): JSON_VALUE
 37            -- Item at `i'-th position
 38        require
 39            is_valid_index: valid_index (i)
 40        do
 41            Result := values.i_th (i)
 42        end
 43
 44    representation: STRING
 45        local
 46            i: INTEGER
 47        do
 48            Result := "["
 49            from
 50                i := 1
 51            until
 52                i > count
 53            loop
 54                Result.append (i_th (i).representation)
 55                i := i + 1
 56                if i <= count then
 57                    Result.append_character (',')
 58                end
 59            end
 60            Result.append_character (']')
 61        end
 62
 63feature -- Visitor pattern
 64
 65    accept (a_visitor: JSON_VISITOR)
 66            -- Accept `a_visitor'.
 67            -- (Call `visit_json_array' procedure on `a_visitor'.)
 68        do
 69            a_visitor.visit_json_array (Current)
 70        end
 71
 72feature -- Mesurement
 73
 74    count: INTEGER
 75            -- Number of items.
 76        do
 77            Result := values.count
 78        end
 79
 80feature -- Status report
 81
 82    valid_index (i: INTEGER): BOOLEAN
 83            -- Is `i' a valid index?
 84        do
 85            Result := (1 <= i) and (i <= count)
 86        end
 87
 88feature -- Change Element
 89
 90    add (value: JSON_VALUE)
 91        require
 92            value_not_null: value /= void
 93        do
 94            values.extend (value)
 95        ensure
 96            has_new_value: old values.count + 1 = values.count and
 97                              values.has (value)
 98        end
 99
100feature -- Report
101
102    hash_code: INTEGER
103            -- Hash code value
104        do
105            from
106                values.start
107                Result := values.item.hash_code
108            until
109                values.off
110            loop
111                Result:= ((Result \\ 8388593) |<< 8) + values.item.hash_code
112                values.forth
113            end
114            Result := Result \\ values.count
115        end
116
117feature -- Conversion
118
119    array_representation: ARRAYED_LIST [JSON_VALUE]
120            -- Representation as a sequences of values
121        do
122            Result := values
123        end
124
125feature -- Status report
126
127    debug_output: STRING
128            -- String that should be displayed in debugger to represent `Current'.
129        do
130            Result := count.out
131        end
132
133feature {NONE} -- Implementation
134
135    values: ARRAYED_LIST [JSON_VALUE]
136            -- Value container
137
138invariant
139     value_not_void: values /= Void
140
141end