Hi again,
Disregard the last patch and use this instead. It uses iterators to provide
each (and include Enumerable) for all implementors of java.util.Collection
and java.util.Map.
Much nicer.
Regards
Ola
Index: src/builtin/java/collections.rb
===================================================================
RCS file: /cvsroot/jruby/jruby/src/builtin/java/collections.rb,v
retrieving revision 1.4
diff -u -r1.4 collections.rb
--- src/builtin/java/collections.rb 17 Oct 2005 15:02:04 -0000 1.4
+++ src/builtin/java/collections.rb 14 Jun 2006 13:13:00 -0000
@@ -1,35 +1,62 @@
# TODO java.util.Comparator support?
JavaUtilities.extend_proxy('java.util.Map') {
+ include Enumerable
def each(&block)
entrySet.each { |pair| block.call(pair.key, pair.value) }
end
+ def [](key)
+ get(key)
+ end
+ def []=(key,val)
+ put(key,val)
+ val
+ end
}
-JavaUtilities.extend_proxy('java.util.Set') {
+JavaUtilities.extend_proxy('java.lang.Comparable') {
+ include Comparable
+ def <=>(a)
+ compareTo(a)
+ end
+}
+
+JavaUtilities.extend_proxy('java.util.Collection') {
+ include Enumerable
def each(&block)
iter = iterator
while iter.hasNext
block.call(iter.next)
end
end
-}
-
-JavaUtilities.extend_proxy('java.lang.Comparable') {
- include Comparable
- def <=>(a)
- compareTo(a)
+ def <<(a); add(a); self; end
+ def +(oth)
+ nw = self.dup
+ nw.addAll(oth)
+ nw
+ end
+ def -(oth)
+ nw = self.dup
+ nw.removeAll(oth)
+ nw
end
}
-JavaUtilities.extend_proxy('java.util.List') {
- include Enumerable
- def each
-# TODO: With 'def each(&block)' the following line will not work.
-# 0.upto(size-1) { |index| block.call(get(index)) }
- 0.upto(size-1) { |index| yield(get(index)) }
+JavaUtilities.extend_proxy('java.util.List') {
+ def [](ix)
+ if ix < size
+ get(ix)
+ else
+ nil
+ end
+ end
+ def []=(ix,val)
+ if size < ix
+ ((ix-size)+1).times { self << nil }
+ end
+ set(ix,val)
+ val
end
- def <<(a); add(a); end
def sort()
include_class 'java.util.ArrayList'
include_class 'java.util.Collections'
@@ -76,4 +103,4 @@
p = yield(*args)
p p
end
-}
\ No newline at end of file
+}
_______________________________________________
Jruby-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jruby-devel