lib/java: add some Java collections
[nit.git] / lib / java / collections.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 # Basic Java collections
16 #
17 # ~~~
18 # var coll = new JavaArray(2)
19 #
20 # assert coll[0].is_java_null
21 # coll[0] = "zero".to_java_string
22 # coll[1] = "one".to_java_string
23 #
24 # assert coll.length == 2
25 # assert coll.first.to_s == "zero"
26 # assert coll[1].to_s == "one"
27 # assert [for e in coll do e.to_s] == ["zero", "one"]
28 # ~~~
29 module collections
30
31 import java
32
33 # Java primitive array
34 #
35 # These have fixed size so they offer the same services as `SequenceRead` and
36 # `[]=`, but would not support `Sequence::add`.
37 extern class AbstractJavaArray[E: Object]
38 super SequenceRead[E]
39 super JavaObject
40
41 # Set the `value` at `key`
42 fun []=(key: Int, value: E) is abstract
43
44 redef fun iterator do return new JavaArrayIterator[E](self)
45
46 redef fun reverse_iterator do return new JavaArrayReverseIterator[E](self)
47 end
48
49 # Java primitive array `float[]`
50 #
51 # Note that Nit `Float` is the size of a double, so storing them in a
52 # `JavaFloatArray` may lead to a loss of precision.
53 extern class JavaFloatArray in "Java" `{ float[] `}
54 super AbstractJavaArray[Float]
55
56 # Get a new array of the given `size`
57 new(size: Int) in "Java" `{ return new float[(int)size]; `}
58
59 redef fun [](i) in "Java" `{ return (double)recv[(int)i]; `}
60
61 redef fun []=(i, e) in "Java" `{ recv[(int)i] = (float)e; `}
62
63 redef fun length in "Java" `{ return recv.length; `}
64 end
65
66 # Java primitive array `double[]`
67 extern class JavaDoubleArray in "Java" `{ double[] `}
68 super AbstractJavaArray[Float]
69
70 # Get a new array of the given `size`
71 new(size: Int) in "Java" `{ return new double[(int)size]; `}
72
73 redef fun [](i) in "Java" `{ return recv[(int)i]; `}
74
75 redef fun []=(i, e) in "Java" `{ recv[(int)i] = (float)e; `}
76
77 redef fun length in "Java" `{ return recv.length; `}
78 end
79
80 # Java primitive array `Object[]`
81 extern class JavaArray in "Java" `{ java.lang.Object[] `}
82 super AbstractJavaArray[JavaObject]
83
84 # Get a new array of the given `size`
85 new(size: Int) in "Java" `{ return new Object[(int)size]; `}
86
87 redef fun [](i) in "Java" `{ return recv[(int)i]; `}
88
89 redef fun []=(i, e) in "Java" `{ recv[(int)i] = e; `}
90
91 redef fun length in "Java" `{ return recv.length; `}
92 end
93
94 # TODO other primitive arrays:
95 # * Java primitive array `byte[]`
96 # * Java primitive array `short[]`
97 # * Java primitive array `int[]`
98 # * Java primitive array `long[]`
99 # * Java primitive array `boolean[]`
100 # * Java primitive array `char[]`
101
102 # An `Iterator` on Java primitive arrays
103 private class JavaArrayIterator[E: Object]
104 super IndexedIterator[E]
105
106 var array: AbstractJavaArray[E]
107
108 redef fun item do return array[index]
109
110 redef fun is_ok do return index < array.length
111
112 redef fun next do index += 1
113
114 redef var index = 0
115 end
116
117 # A reverse `Iterator` on Java primitive arrays
118 private class JavaArrayReverseIterator[E: Object]
119 super IndexedIterator[E]
120
121 var array: AbstractJavaArray[E]
122
123 redef fun item do return array[index]
124
125 redef fun is_ok do return index >= 0
126
127 redef fun next do index -= 1
128
129 redef var index = array.length - 1
130 end