[+gwtc] ---------- Forwarded message ---------- From: <amitman...@google.com> Date: Thu, May 21, 2009 at 11:37 AM Subject: Re: Patch for issue 3279 To: dannydaemo...@gmail.com
Hi Danny, How about refactoring BitSet.java as follows. I have introduced 3 private methods to do the handling and I have included sample implementations of public methods 'and' and 'andNot'. private static native int getWord(JsArrayInteger array, int index) /*-{ var packed = array[index]; if (packed === undefined) { return 0; } return packed; }-*/; private static native JsArrayInteger getWordPositions(JsArrayInteger thisArray) /*-{ var positions = new Array(); for (var property in thisArray) { var number = property >>> 0; positions.push(number); } return positions; }-*/; private static native void setWord(JsArrayInteger array, int index, int value) /*-{ if (value != 0) { array[index] = value; } else { delete array[index]; } }-*/; public void and(BitSet set) { if (this == set) { return; } JsArrayInteger positions = getWordPositions(this.array); for (int i = 0; i < positions.length(); i++) { int position = positions.get(i); setWord(this.array, position, getWord(this.array, position) & getWord(set.array, position)); } } public void andNot(BitSet set) { JsArrayInteger positions = getWordPositions(this.array); for (int i = 0; i < positions.length(); i++) { int position = positions.get(i); setWord(this.array, position, getWord(this.array, position) & (~getWord(set.array, position))); } As you can see, the implementations of 'and', 'andNot' are much simpler now. Do you see a problem with this refactoring? If other public methods were implemented similarly, BitSet.java would become smaller in size and simpler to maintain. You probably want to introduce another java method and use it instead of ">> 5" in the code: private static int wordIndex(int bitIndex) { return bitIndex >>> 5; // unsigned right shift } Btw, adding a comment in case of BitSet.toString() is fine. Sorry for not carefully looking at the method. Amit http://gwt-code-reviews.appspot.com/33815 --~--~---------~--~----~------------~-------~--~----~ http://groups.google.com/group/Google-Web-Toolkit-Contributors -~----------~----~----~----~------~----~------~--~---