http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/EventWrapper.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/EventWrapper.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/EventWrapper.java deleted file mode 100644 index fd2a17b..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/EventWrapper.java +++ /dev/null @@ -1,593 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.dom.client.Element; -import com.google.gwt.event.dom.client.KeyCodes; -import com.google.gwt.user.client.DOM; -import com.google.gwt.user.client.Event; -import org.waveprotocol.wave.client.common.util.UserAgent; - -import java.util.HashMap; -import java.util.Map; - -/** - * Wraps GWT's events class to give prettier access to event properties. - * - * NOTE(user): we expect a future version of GWT to do this directly on - * their Event class, at which point we can probably deprecate this - * wrapper - * - */ -@SuppressWarnings({ "serial" }) -// TODO(danilatos,user): Remove this class, since a lot of this functionality -// is exposed by GWT's new event system (i.e. NativeEvent). -public class EventWrapper { - - /** - * The event we are wrapping - */ - private final Event event; - - /** - * Modifier values, greater than 16 bits, to add to the keypresses - * to distinguish them. First shift - */ - private final static int SHIFT = 1 << 17; - - /** - * Alt - */ - private final static int ALT = 1 << 18; - - /** - * Ctrl - */ - private final static int CTRL = 1 << 19; - - /** - * Meta - */ - private final static int META = 1 << 20; - - - /** (Not defined in GWT's KeyCodes) */ - public final static int KEY_INSERT = 45; - - /** - * Map of keypresses and modifiers to our KeyCombo enum values. - * http://www.quirksmode.org/js/keys.html has some useful info about this. - * TODO(danilatos): Implement an IntMap JSO and use that - */ - private final static Map<Integer, KeyCombo> keyMap = - new HashMap<Integer, KeyCombo>() { - { - // Tab - put(9, KeyCombo.TAB); - put(9 + SHIFT, KeyCombo.SHIFT_TAB); - - // Space bar - put(32, KeyCombo.SPACE); - put(32 + SHIFT, KeyCombo.SHIFT_SPACE); - put(32 + CTRL, KeyCombo.CTRL_SPACE); - put(32 + SHIFT + CTRL, KeyCombo.CTRL_SHIFT_SPACE); - - // Escape - put(KeyCodes.KEY_ESCAPE, KeyCombo.ESC); - - // Enter - put(KeyCodes.KEY_ENTER, KeyCombo.ENTER); - put(KeyCodes.KEY_ENTER + SHIFT, KeyCombo.SHIFT_ENTER); - put(KeyCodes.KEY_ENTER + CTRL, KeyCombo.CTRL_ENTER); - - // Backspace - put(KeyCodes.KEY_BACKSPACE, KeyCombo.BACKSPACE); - put(KeyCodes.KEY_BACKSPACE + SHIFT, KeyCombo.SHIFT_BACKSPACE); - - // Delete - put(KeyCodes.KEY_DELETE, KeyCombo.DELETE); - put(KeyCodes.KEY_DELETE + SHIFT, KeyCombo.SHIFT_DELETE); - - // Insert - put(KEY_INSERT, KeyCombo.INSERT); - put(KEY_INSERT + SHIFT, KeyCombo.SHIFT_INSERT); - put(KEY_INSERT + CTRL, KeyCombo.CTRL_INSERT); - - // Ctrl-equals - put('=' + CTRL, KeyCombo.CTRL_EQUALS); - - // Ctrl-alpha combos - put('B' + CTRL, KeyCombo.CTRL_B); - put('D' + CTRL, KeyCombo.CTRL_D); - put('F' + CTRL, KeyCombo.CTRL_F); - put('I' + CTRL, KeyCombo.CTRL_I); - put('G' + CTRL, KeyCombo.CTRL_G); - put('H' + CTRL, KeyCombo.CTRL_H); - put('K' + CTRL, KeyCombo.CTRL_K); - put('O' + CTRL, KeyCombo.CTRL_O); - put('U' + CTRL, KeyCombo.CTRL_U); - put('W' + CTRL, KeyCombo.CTRL_W); - put('A' + CTRL, KeyCombo.CTRL_A); - put('R' + CTRL, KeyCombo.CTRL_R); - put('E' + CTRL, KeyCombo.CTRL_E); - put('L' + CTRL, KeyCombo.CTRL_L); - - put('L' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_L); - - put('F' + CTRL + ALT, KeyCombo.CTRL_ALT_F); - put('D' + CTRL + ALT, KeyCombo.CTRL_ALT_D); - put('G' + CTRL + ALT, KeyCombo.CTRL_ALT_G); - put('S' + CTRL + ALT, KeyCombo.CTRL_ALT_S); - - // Canned responses - put('!' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_1); - put('1' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_1); - put('@' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_2); - put('2' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_2); - put('#' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_3); - put('3' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_3); - put('%' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_5); - put('5' + CTRL + SHIFT, KeyCombo.CTRL_SHIFT_5); - - // Allowable ORDER combos - // ORDER is the Apple key on OS X and the Ctrl key in Windows and Linux. - // It is the key modifier used to perform actions such as copy/paste, - // save, print etc. - // XXX(user): These override the CTRL/META combos! Fix it! - { - final int orderKey = UserAgent.isMac() ? META : CTRL; - put('A' + orderKey, KeyCombo.ORDER_A); - put('B' + orderKey, KeyCombo.ORDER_B); - put('C' + orderKey, KeyCombo.ORDER_C); - put('D' + orderKey, KeyCombo.ORDER_D); - put('F' + orderKey, KeyCombo.ORDER_F); - put('G' + orderKey, KeyCombo.ORDER_G); - put('I' + orderKey, KeyCombo.ORDER_I); - put('K' + orderKey, KeyCombo.ORDER_K); - put('L' + orderKey, KeyCombo.ORDER_L); - put('N' + orderKey, KeyCombo.ORDER_N); - put('O' + orderKey, KeyCombo.ORDER_O); - put('P' + orderKey, KeyCombo.ORDER_P); - put('R' + orderKey, KeyCombo.ORDER_R); - put('T' + orderKey, KeyCombo.ORDER_T); - put('U' + orderKey, KeyCombo.ORDER_U); - put('V' + orderKey, KeyCombo.ORDER_V); - put('W' + orderKey, KeyCombo.ORDER_W); - put('X' + orderKey, KeyCombo.ORDER_X); - put('Z' + orderKey, KeyCombo.ORDER_Z); - put('Q' + orderKey, KeyCombo.ORDER_Q); - put('R' + orderKey + SHIFT, KeyCombo.ORDER_SHIFT_R); - put('V' + orderKey + SHIFT, KeyCombo.ORDER_SHIFT_V); - put('K' + orderKey + SHIFT, KeyCombo.ORDER_SHIFT_K); - put('5' + orderKey + SHIFT, KeyCombo.ORDER_SHIFT_5); - - // Plaintext paste in Safari - put('V' | orderKey | ALT | SHIFT, KeyCombo.ORDER_ALT_SHIFT_V); - } - - // Key navigation - put(KeyCodes.KEY_DOWN, KeyCombo.DOWN); - put(KeyCodes.KEY_UP, KeyCombo.UP); - put(KeyCodes.KEY_LEFT, KeyCombo.LEFT); - put(KeyCodes.KEY_RIGHT, KeyCombo.RIGHT); - put(KeyCodes.KEY_PAGEUP, KeyCombo.PAGE_UP); - put(KeyCodes.KEY_PAGEDOWN, KeyCombo.PAGE_DOWN); - put(KeyCodes.KEY_HOME, KeyCombo.HOME); - put(KeyCodes.KEY_END, KeyCombo.END); - - // Safari key navigation - // TODO(user): consider using deferred binding - put(63233, KeyCombo.DOWN); - put(63232, KeyCombo.UP); - put(63234, KeyCombo.LEFT); - put(63235, KeyCombo.RIGHT); - put(63276, KeyCombo.PAGE_UP); - put(63277, KeyCombo.PAGE_DOWN); - put(63273, KeyCombo.HOME); - put(63275, KeyCombo.END); - - // Meta combos - put(KeyCodes.KEY_LEFT + META, KeyCombo.META_LEFT); - put(KeyCodes.KEY_RIGHT + META, KeyCombo.META_RIGHT); - put(KeyCodes.KEY_HOME + META, KeyCombo.META_HOME); - } - }; - - /** - * @return true is even is a submit - */ - public boolean isSubmit() { - return event != null && isKeyEvent() && getKeyCombo() == KeyCombo.SHIFT_ENTER; - } - - /** - * Constructor - * - * @param event - */ - public EventWrapper(Event event) { - this.event = event; - } - - /** - * @return True if alt key was pressed - */ - public boolean getAltKey() { - return DOM.eventGetAltKey(event); - } - - /** - * @return The mouse buttons that were depressed as a bit-field, defined - * by {@link Event#BUTTON_LEFT}, {@link Event#BUTTON_MIDDLE}, and - * {@link Event#BUTTON_RIGHT} - */ - public int getButton() { - return DOM.eventGetButton(event); - } - - /** - * @return {@link #getKeyCode()} cast to char - */ - public char getCharCode() { - return (char) getKeyCode(); - } - - /** - * @return The mouse x-position within the browser window's client area. - */ - public int getClientX() { - return DOM.eventGetClientX(event); - } - - /** - * @return The mouse y-position within the browser window's client area. - */ - public int getClientY() { - return DOM.eventGetClientY(event); - } - - /** - * @return True if ctrl key was pressed - */ - public boolean getCtrlKey() { - return DOM.eventGetCtrlKey(event); - } - - /** - * @return The current event that is being fired. The current event is only - * available within the lifetime of the onBrowserEvent function. Once the - * onBrowserEvent method returns, the current event is reset to null. - */ - public static EventWrapper getCurrentEvent() { - Event current = DOM.eventGetCurrentEvent(); - return current != null ? - new EventWrapper(DOM.eventGetCurrentEvent()) : null; - } - - /** - * @return The event's current target element. This is the element - * whose listener fired last, not the element which fired the event - * initially. - */ - public Element getCurrentTarget() { - return DOM.eventGetCurrentTarget(event); - } - - /** - * @return The event - */ - public Event getEvent() { - return event; - } - - /** - * @return The element from which the mouse pointer was moved - * (only valid for {@link Event#ONMOUSEOVER}). - */ - public static Element getFromElement(Event event) { - return DOM.eventGetFromElement(event); - } - - /** - * @return The key code associated with this event. For - * {@link Event#ONKEYPRESS}, the Unicode value of the character generated. - * For {@link Event#ONKEYDOWN} and {@link Event#ONKEYUP}, the code - * associated with the physical key. - */ - public int getKeyCode() { - return getKeyCode(event); - } - - /** - * Wrapper for GWT's get[Key Char]Code() that conflates the two values. If - * there is no keyCode present, it returns charCode instead. This matches the - * values in {{@link #keyMap} above. - */ - public static int getKeyCode(Event evt) { - int keyCode = evt.getKeyCode(); - if (keyCode == 0) { - keyCode = evt.getCharCode(); - } - return keyCode; - } - - /** - * Semi-deprecated (will be deprecated once event signal has a mechanism for - * fast switching). - * @return An encoding of the event's keycode and modifiers - */ - public KeyCombo getKeyCombo() { - return getKeyCombo(getKeyCode(), getCtrlKey(), getShiftKey(), getAltKey(), getMetaKey()); - } - - public static KeyCombo getKeyCombo(SignalEvent signal) { - return getKeyCombo(signal.getKeyCode(), signal.getCtrlKey(), - signal.getShiftKey(), signal.getAltKey(), signal.getMetaKey()); - } - - /** - * @return the key-combo representation of the key event. - */ - public static KeyCombo getKeyCombo(Event evt) { - return getKeyCombo(getKeyCode(evt), DOM.eventGetCtrlKey(evt), - DOM.eventGetShiftKey(evt), DOM.eventGetAltKey(evt), DOM.eventGetMetaKey(evt)); - } - - private static KeyCombo getKeyCombo(int keyCode, boolean ctrl, boolean shift, boolean alt, - boolean meta) { - int gwtCode = keyCode; - gwtCode += - (ctrl ? CTRL : 0) - + (shift ? SHIFT : 0) - + (alt ? ALT : 0) + - + (meta ? META : 0); - if (!keyMap.containsKey(gwtCode) && keyCode >= 'a' && keyCode <= 'z') { - // HACK(danilatos): make it work cross-browser with the event signal updates. - // get rid of this class soon. - gwtCode += 'A' - 'a'; - } - return keyMap.containsKey(gwtCode) ? keyMap.get(gwtCode) : KeyCombo.OTHER; - } - - /** - * Converts the parameters to {@link KeyCodes} events to a KeyCombo. - * - * @return the key-combo representation of the key event. - */ - public static KeyCombo getKeyCombo(char keyCode, int modifiers) { - int gwtCode = keyCode - + (((modifiers & KeyCodes.KEY_CTRL) != 0) ? CTRL : 0) - + (((modifiers & KeyCodes.KEY_SHIFT) != 0) ? SHIFT : 0) - + (((modifiers & KeyCodes.KEY_ALT) != 0) ? ALT : 0); - return keyMap.containsKey(gwtCode) ? keyMap.get(gwtCode) : KeyCombo.OTHER; - } - - /** - * @return True if the metakey was pressed - */ - public boolean getMetaKey() { - return DOM.eventGetMetaKey(event); - } - - /** - * @return The velocity of the mouse wheel associated with the event - * along the Y axis. - */ - public static int getMouseWheelVelocityY(Event event) { - return DOM.eventGetMouseWheelVelocityY(event); - } - - /** - * @return True if this was an auto-repeat event - */ - public boolean getRepeat() { - return DOM.eventGetRepeat(event); - } - - /** - * @return The mouse x-position on the user's display - */ - public int getScreenX() { - return DOM.eventGetScreenX(event); - } - - /** - * @return The mouse y-position on the user's display - */ - public int getScreenY() { - return DOM.eventGetScreenY(event); - } - - /** - * @return True if the shift key was pressed - */ - public boolean getShiftKey() { - return DOM.eventGetShiftKey(event); - } - - /** - * @return The element that was the actual target of the event. - */ - public Element getTarget() { - return DOM.eventGetTarget(event); - } - - /** - * @return The element to which the mouse pointer was moved - * (only valid for {@link Event#ONMOUSEOUT}). - */ - public static Element getToElement(Event event) { - return DOM.eventGetToElement(event); - } - - /** - * @return The event's type - */ - public int getType() { - return DOM.eventGetType(event); - } - - /** - * @return true if event type is a focus event - */ - public boolean isFocusEvent() { - return (getType() & Event.FOCUSEVENTS) != 0; - } - - /** - * @return true if event type is a key event - */ - public boolean isKeyEvent() { - return (getType() & Event.KEYEVENTS) != 0; - } - - /** - * @return true if event type is a mouse event - */ - public boolean isMouseEvent() { - return (getType() & Event.MOUSEEVENTS) != 0; - } - - /** - * @return The event's type string - */ - public String getTypeString() { - return DOM.eventGetTypeString(event); - } - - /** - * Prevents default for current event - */ - public static void preventCurrentEventDefault() { - getCurrentEvent().event.preventDefault(); - } - - /** - * @return A string describing which modifier keys were pressed, - * and whether this was a repeat event, e.g., " shift ctrl" - */ - @SuppressWarnings("deprecation") - public static String modifiers(Event event) { - // repeat is deprecated, but useful for debugging - return (event.getAltKey() ? " alt" : "") - + (event.getShiftKey() ? " shift" : "") - + (event.getCtrlKey() ? " ctrl" : "") - + (event.getMetaKey() ? " meta" : "") - + ((event.getTypeInt() == Event.ONKEYDOWN) && event.getRepeat() ? " repeat" : ""); - } - - /** - * @return A string describing which mouse buttons were pressed, - * e.g., " left" - */ - private static String mouseButtons(Event event) { - if (event.getButton() == -1) { - return ""; - } else { - return ((event.getButton() & Event.BUTTON_LEFT) != 0 ? " left" : "") - + ((event.getButton() & Event.BUTTON_MIDDLE) != 0 ? " middle" : "") - + ((event.getButton() & Event.BUTTON_RIGHT) != 0 ? " right" : ""); - } - } - - /** - * @return String describing the event's key code, e.g., - * " 64 'a'" - */ - private static String key(Event event) { - return " " + event.getKeyCode() + " '" + (char) event.getKeyCode() + "'"; - } - - /** - * @return A string describing the client x,y position, - * e.g., " (100, 100)" - */ - private static String mousePoint(Event event) { - return " (" + event.getClientX() + ", " + event.getClientY() + ")"; - } - - @Override - public String toString() { - return asString(event); - } - - public static String asString(Event event) { - // Start with the event type string - String string = DOM.eventGetTypeString(event); - // Next type-specific fields - switch (event.getTypeInt()) { - case Event.ONKEYPRESS: - case Event.ONKEYUP: - case Event.ONKEYDOWN: - string += key(event) + modifiers(event); - break; - case Event.ONCLICK: - case Event.ONDBLCLICK: - case Event.ONMOUSEMOVE: - string += mousePoint(event) + modifiers(event); - break; - case Event.ONMOUSEDOWN: - case Event.ONMOUSEUP: - string += mousePoint(event) + mouseButtons(event) + modifiers(event); - break; - case Event.ONMOUSEOUT: - string += mousePoint(event) + modifiers(event) + " to: " + getToElement(event); - break; - case Event.ONMOUSEOVER: - string += mousePoint(event) + modifiers(event) + " from: " + getFromElement(event); - break; - case Event.ONMOUSEWHEEL: - string += " " + getMouseWheelVelocityY(event) + mousePoint(event) + modifiers(event); - break; - case Event.ONFOCUS: - case Event.ONBLUR: - case Event.ONCHANGE: - case Event.ONERROR: - case Event.ONLOAD: - case Event.ONLOSECAPTURE: - case Event.ONSCROLL: - break; - } - return string; - } - - /** - * In safari, there is X velocity and Y velocity. GWT code return the combination of the 2. - * This code only return the Y velocity. - * @return The y velocity of the mouse event and only the y velocity. - */ - public static int getMouseWheelVelocityYOnly(Event event) { - if (!UserAgent.isSafari()) { - return event.getMouseWheelVelocityY(); - } else { - return nativeGetMouseWheelVelocityYOnly(event); - } - } - - private static native int nativeGetMouseWheelVelocityYOnly(Event evt) /*-{ - // wheelDeltaY is not standard and only available in newer safari - if (evt.wheelDeltaY == undefined) { - // The following line is copied from DOMImplSafari.getMouseWheelVelocityY - return Math.round(-evt.wheelDelta / 40) || 0; - } else { - return Math.round(-evt.wheelDeltaY / 40) || 0; - } - }-*/; -}
http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/Factory.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/Factory.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/Factory.java deleted file mode 100644 index 87a8a4d..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/Factory.java +++ /dev/null @@ -1,34 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -/** - * A factory. - * - * @param <T> type created by this factory - */ -public interface Factory<T> { - /** - * Creates an object. - * - * @return a new object. - */ - T create(); -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/FakeSignalEvent.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/FakeSignalEvent.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/FakeSignalEvent.java deleted file mode 100644 index 9dce0aa..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/FakeSignalEvent.java +++ /dev/null @@ -1,132 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.dom.client.Element; - -import java.util.EnumSet; - -/** - * @author [email protected] (Daniel Danilatos) - * - */ -public class FakeSignalEvent extends SignalEventImpl { - - public static SignalEventFactory<FakeSignalEvent> FACTORY = - new SignalEventFactory<FakeSignalEvent>() { - @Override public FakeSignalEvent create() { - return new FakeSignalEvent(); - } - }; - - private static class FakeNativeEvent implements NativeEvent { - private final boolean altKey, ctrlKey, metaKey, shiftKey; - private final String type; - private final int mouseButton; - - boolean defaultPrevented = false; - boolean propagationStopped = false; - - public FakeNativeEvent(String type, - int mouseButton, EnumSet<KeyModifier> modifiers) { - this.type = type; - this.mouseButton = mouseButton; - this.altKey = modifiers != null && modifiers.contains(KeyModifier.ALT); - this.ctrlKey = modifiers != null && modifiers.contains(KeyModifier.CTRL); - this.metaKey = modifiers != null && modifiers.contains(KeyModifier.META); - this.shiftKey = modifiers != null && modifiers.contains(KeyModifier.SHIFT); - } - - @Override - public boolean getAltKey() { - return altKey; - } - - @Override - public int getButton() { - return mouseButton; - } - - @Override - public boolean getCtrlKey() { - return ctrlKey; - } - - @Override - public boolean getMetaKey() { - return metaKey; - } - - @Override - public boolean getShiftKey() { - return shiftKey; - } - - @Override - public String getType() { - return type; - } - - @Override - public void preventDefault() { - defaultPrevented = true; - } - - @Override - public void stopPropagation() { - propagationStopped = true; - } - } - - public static FakeSignalEvent createKeyPress( - KeySignalType type, int keyCode, EnumSet<KeyModifier> modifiers) { - return createKeyPress(FACTORY, type, keyCode, modifiers); - } - - public static <T extends FakeSignalEvent> T createEvent( - SignalEventFactory<T> factory, String type) { - return createInner(factory.create(), new FakeNativeEvent(type, 0, null), null); - } - - public static <T extends FakeSignalEvent> T createKeyPress(SignalEventFactory<T> factory, - KeySignalType type, int keyCode, EnumSet<KeyModifier> modifiers) { - SignalKeyLogic.Result keyLogic = new SignalKeyLogic.Result(); - keyLogic.keyCode = keyCode; - keyLogic.type = type; - return createInner(factory.create(), new FakeNativeEvent("keydown", 0, modifiers), keyLogic); - } - - public static <T extends FakeSignalEvent> T createClick(SignalEventFactory<T> factory, - EnumSet<KeyModifier> modifiers) { - return createInner(factory.create(), new FakeNativeEvent("click", 0, modifiers), null); - } - - public boolean defaultPrevented() { - return ((FakeNativeEvent) nativeEvent).defaultPrevented; - } - - public boolean propagationStopped() { - return ((FakeNativeEvent) nativeEvent).propagationStopped; - } - - @Override public Element getTarget() { - return null; - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/FastQueue.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/FastQueue.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/FastQueue.java deleted file mode 100644 index 72f6b02..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/FastQueue.java +++ /dev/null @@ -1,152 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import java.util.AbstractQueue; -import java.util.Iterator; -import java.util.NoSuchElementException; - -/** - * An efficent implementation of queue for use in GWT. - * Its efficiency is due to using JSO maps. - * - * Against the advice of AbstractQueue, this class accepts null. - */ -public class FastQueue<T> extends AbstractQueue<T>{ - private final IntMapJsoView<T> contents = IntMapJsoView.create(); - - private int currentGetIndex = 0; - private int currentPutIndex = 0; - private int numEntry = 0; - - @Override - public Iterator<T> iterator() { - return new Iterator<T>() { - int index = currentGetIndex; - int lastEntry = -1; - - @Override - public boolean hasNext() { - index = forwardGetIndex(index); - return index < currentPutIndex; - } - - @Override - public T next() { - index = forwardGetIndex(index); - lastEntry = index; - return contents.get(index++); - } - - @Override - public void remove() { - if (lastEntry >= 0) { - removeEntry(lastEntry); - } - } - }; - } - @Override - public int size() { - return numEntry; - } - - @Override - public boolean offer(T e) { - addEntry(e); - return true; - } - - @Override - public T peek() { - currentGetIndex = forwardGetIndex(currentGetIndex); - return currentGetIndex < currentPutIndex ? contents.get(currentGetIndex) : null; - } - - // This implementation of queue cannot use the inherited remove() method because a - // null element will be misinterpreted as the queue being empty. - @Override - public T remove() { - if (isEmpty()) { - throw new NoSuchElementException(); - } - return poll(); - } - - /** - * Move the index forward until it hit the next non empty index or the end. - */ - private int forwardGetIndex(int index) { - // the first possible index is the current get index. - index = Math.max(currentGetIndex, index); - while (index < currentPutIndex && !contents.has(index)) { - index++; - } - return index; - } - - @Override - public T poll() { - T ret = peek(); - if (!isEmpty()) { - removeEntry(currentGetIndex); - currentGetIndex++; - } - return ret; - } - - /** - * Remove the entry with the given index - * @param index - */ - protected T removeEntry(int index) { - T ret = null; - if (contents.has(index)) { - numEntry--; - ret = contents.get(index); - contents.remove(index); - } - assert numEntry < currentPutIndex - currentGetIndex; - return ret; - } - - /** - * Add an entry to the queue. - * - * @param e - * - * @return the index of the added entry. - */ - protected int addEntry(T e) { - numEntry++; - int putIndex = currentPutIndex++; - contents.put(putIndex, e); - assert numEntry <= currentPutIndex - currentGetIndex; - return putIndex; - } - - @Override - public void clear() { - currentPutIndex = 0; - currentGetIndex = 0; - numEntry = 0; - contents.clear(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/IdentityHashQueue.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/IdentityHashQueue.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/IdentityHashQueue.java deleted file mode 100644 index a38d735..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/IdentityHashQueue.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.CollectionUtils; -import org.waveprotocol.wave.model.util.IdentityMap; - -/** - * A fast queue implementation for GWT that also supports O(1) remove. - * - * @param <T> - * Key type. Restrictions: It most satisfy the construct that's needed - * for use with IdentityMap. The current restriction is it must NOT - * override Object's implementation of hashCode() or equals() - * - */ -public class IdentityHashQueue<T> extends FastQueue<T> { - /** - * A map between the inserted element and their position in the queue. - * Using identity map now, may want to investigate to see if we want to change it to a - * IdentityToIntMap<T> which maps a object into integer to avoid box/unbox. - */ - // TODO(user): Check to see if using Integer causes any slow down. - private final IdentityMap<T, Integer> indexMap = CollectionUtils.createIdentityMap(); - - @Override - protected int addEntry(T e) { - // should only add an item in the queue at most once - assert !indexMap.has(e); - if (indexMap.has(e)) { - throw new IllegalStateException("Add Entry " + e + " map " + indexMap); - } - int index = super.addEntry(e); - indexMap.put(e, index); - assert indexMap.has(e); - return index; - } - - @Override - protected T removeEntry(int index) { - T ret = super.removeEntry(index); - if (!indexMap.has(ret)) { - throw new IllegalStateException("Remove Entry " + ret + " map " + indexMap); - } - assert indexMap.has(ret); - indexMap.remove(ret); - return ret; - } - - @Override - @SuppressWarnings("unchecked") - public boolean remove(Object e) { - if (e != null && indexMap.has((T) e)) { - removeEntry(indexMap.get((T) e)); - return true; - } - return false; - } - - @Override - public void clear() { - super.clear(); - indexMap.clear(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/IntMapJsoView.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/IntMapJsoView.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/IntMapJsoView.java deleted file mode 100644 index e5cb0f5..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/IntMapJsoView.java +++ /dev/null @@ -1,278 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.core.client.JavaScriptObject; - -import org.waveprotocol.wave.model.util.ReadableIntMap.ProcV; - -import java.util.HashMap; -import java.util.Map; - -/** - * A super fast and memory efficient map (directly uses a js object as the map, - * and requires only 1 object). Only allows int keys, thus taking advantage of - * the "perfect hashing" of ints with respect to javascript. - * - * No one other than JsoIntMap should use this class. - * TODO(danilatos): Move the clever logic into JsoIntMap and delete this file. - * - * NOTE(danilatos): Does not use hasOwnProperty semantics. So it's possible for - * spurious entries to appear in the map if we're not careful. Easily fixable, - * but would incur a slight performance hit (I'm now just handwaving). - * - * TODO(dan): Use a different version from the GWT team once it is available - * - * @author [email protected] (Daniel Danilatos) - * @param <T> Type of values in the map. Keys are always ints. - */ -final class IntMapJsoView<T> extends JsoMapBase { - /** - * A function that accepts an accumulated value, a key and the corresponding - * item from the map and returns the new accumulated value. - * - * @see IntMapJsoView#reduce(Object, IntMapJsoView.Reduce) - * @param <E> - * int map's type parameter - * @param <R> - * The type of the value being accumulated - */ - public interface Reduce<E, R> { - /** The function */ - public R apply(R soFar, int key, E item); - } - - /** Construct an empty IntMap */ - public static native <T> IntMapJsoView<T> create() /*-{ - return {}; - }-*/; - - - /** - * Represent a java script native function that change the integer key into index key. - * This is done because in chrome, it is 20 times faster to store string index than to store - * numeric index if the number is > 2^15. - * - * The following time are recorded under chrome linux 4.0.266.0 - * The recorderd time are in ms. - * - * iterations Numeric Index String index Hybrid time - * 1024 1 2 0 - * 2048 0 5 1 - * 4096 1 14 1 - * 8192 4 30 4 - * 16384 8 63 9 - * 32768 20 132 24 - * 65536 266 264 148 - * 131072 5551 513 404 - * 262144 16719 1036 976 - * - * for numeric index < 2^15, it is 6 times faster to use numeric index - * for string index > 2^15, it is 20 times faster to use string index - * hybrid approach represents what we are doing here, i.e. use numeric index for value < 2^15 and - * string index for value > 2^15 - * - * DESPITE THIS RESULT, it is very bad to use numeric index at all. Using the numeric index - * slows down the javascript engine. We always uses String index in chrome. So DO NOT change - * the code to do the hybrid approach. - * - * This function must be represented by JavaScriptObject and not normal java interfaces because - * it's return data of different type depends on the input value. - */ - @SuppressWarnings("unused") // used in native method - private static JavaScriptObject prefixer; - - @SuppressWarnings("unused") // used in native method - private static JavaScriptObject evaler; - - static { - setupPrefix(UserAgent.isChrome()); - } - - private static native void setupPrefix(boolean usePrefix) /*-{ - if (usePrefix) { - @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer = function(a) { - return "a" + a; - }; - @org.waveprotocol.wave.client.common.util.IntMapJsoView::evaler = function(a) { - return a[0] == "a" ? parseInt(a.substr(1, a.length)) : parseInt(a); - } - } else { - @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer = function(a) { - return a; - }; - @org.waveprotocol.wave.client.common.util.IntMapJsoView::evaler = function(a) { - return parseInt(a); - } - } - }-*/; - - /** Construct a IntMap from a java Map */ - public static <T> IntMapJsoView<T> fromMap(Map<Integer, T> map) { - IntMapJsoView<T> intMap = create(); - for (int key : map.keySet()) { - intMap.put(key, map.get(key)); - } - return intMap; - } - - protected IntMapJsoView() { - } - - /** - * @param key - * @return true if a value indexed by the given key is in the map - */ - public native boolean has(int key) /*-{ - var prefixer = @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer; - return this[prefixer(key)] !== undefined; - }-*/; - - /** - * @param key - * @return The value with the given key, or null if not present - */ - public native T get(int key) /*-{ - var prefixer = @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer; - return this[prefixer(key)]; - }-*/; - - /** - * Put the value in the map at the given key. Note: Does not return the old - * value. - * - * @param key - * @param value - */ - public native void put(int key, T value) /*-{ - var prefixer = @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer; - this[prefixer(key)] = value; - }-*/; - - /** - * Remove the value with the given key from the map. Note: does not return the - * old value. - * - * @param key - */ - public native void remove(int key) /*-{ - var prefixer = @org.waveprotocol.wave.client.common.util.IntMapJsoView::prefixer; - delete this[prefixer(key)]; - }-*/; - - /** - * Same as {@link #remove(int)}, but returns what was previously there, if - * anything. - * - * @param key - * @return what was previously there or null - */ - public final T removeAndReturn(int key) { - T val = get(key); - remove(key); - return val; - } - - /** - * Ruby/prototype.js style iterating idiom, using a callbak. Equivalent to a - * for-each loop. TODO(danilatos): Implement break and through a la - * prototype.js if needed. - * - * @param proc - */ - public final native void each(ProcV<? super T> proc) /*-{ - var evaler = @org.waveprotocol.wave.client.common.util.IntMapJsoView::evaler; - for (var k in this) { - proc. - @org.waveprotocol.wave.model.util.ReadableIntMap.ProcV::apply(ILjava/lang/Object;) - (evaler(k), this[k]); - } - }-*/; - - public final native T someValue() /*-{ - for (var k in this) { - return this[k] - } - return null; - }-*/; - - - /** - * Same as ruby/prototype reduce. Same as functional foldl. Apply a function - * to an accumulator and key/value in the map. The function returns the new - * accumulated value. TODO(danilatos): Implement break and through a la - * prototype.js if needed. - * - * @param initial - * @param proc - * @return The accumulated value - * @param <R> - * The accumulating type - */ - public final native <R> R reduce(R initial, Reduce<T, R> proc) /*-{ - var reduction = initial; - var evaler = @org.waveprotocol.wave.client.common.util.IntMapJsoView::evaler; - for (var k in this) { - reduction = proc. - @org.waveprotocol.wave.client.common.util.IntMapJsoView.Reduce::apply(Ljava/lang/Object;ILjava/lang/Object;) - (reduction, evaler(k), this[k]); - } - return reduction; - }-*/; - - /** - * Convert to a java Map - */ - public final Map<Integer, T> toMap() { - return addToMap(new HashMap<Integer, T>()); - } - - /** - * Add all values to a java map. - * - * @param map - * The map to add values to - * @return The same map, for convenience. - */ - public final Map<Integer, T> addToMap(final Map<Integer, T> map) { - each(new ProcV<T>() { - public void apply(int key, T item) { - map.put(key, item); - } - }); - return map; - } - - /** - * Add all values to a IntMap. - * - * @param map - * The map to add values to - * @return The same map, for convenience. - */ - public final IntMapJsoView<T> addToMap(final IntMapJsoView<T> map) { - each(new ProcV<T>() { - public void apply(int key, T item) { - map.put(key, item); - } - }); - return map; - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsArraySort.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsArraySort.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsArraySort.java deleted file mode 100644 index 4eb9dc6..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsArraySort.java +++ /dev/null @@ -1,95 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.core.client.JsArray; -import com.google.gwt.core.client.JsArrayBoolean; -import com.google.gwt.core.client.JsArrayInteger; -import com.google.gwt.core.client.JsArrayNumber; -import com.google.gwt.core.client.JsArrayString; - -/** - * Utility to sort java.com.gwt.core.client.JsArray* classes. - * - * NOTE(user): - * This is done as a static helper class rather than within the JsArray* - * classes, to avoid passing in functors to a JSNI method (which make knorton - * and jgw nervous). - * - */ -public class JsArraySort { - /** - * Utility class, so private constructor. - */ - private JsArraySort() { - } - - /** - * Sorts a JsArray of type T. - * - * @param sortMe Array to be sorted. - * @param comparator Comparator to be used, per native JS sort() method. - */ - public static native <T extends JavaScriptObject> void sort(JsArray<T> sortMe, - JavaScriptObject comparator) /*-{ - sortMe.sort(comparator); - }-*/; - - /** - * Sorts a JsArray of booleans. - * - * @param sortMe Array to be sorted. - * @param comparator Comparator to be used, per native JS sort() method. - */ - public static native void sort(JsArrayBoolean sortMe, JavaScriptObject comparator) /*-{ - sortMe.sort(comparator); - }-*/; - - /** - * Sorts a JsArray of integers. - * - * @param sortMe Array to be sorted. - * @param comparator Comparator to be used, per native JS sort() method. - */ - public static native void sort(JsArrayInteger sortMe, JavaScriptObject comparator) /*-{ - sortMe.sort(comparator); - }-*/; - - /** - * Sorts a JsArray of doubles. - * - * @param sortMe Array to be sorted. - * @param comparator Comparator to be used, per native JS sort() method. - */ - public static native void sort(JsArrayNumber sortMe, JavaScriptObject comparator) /*-{ - sortMe.sort(comparator); - }-*/; - - /** - * Sorts a JsArray of strings. - * - * @param sortMe Array to be sorted. - * @param comparator Comparator to be used, per native JS sort() method. - */ - public static native void sort(JsArrayString sortMe, JavaScriptObject comparator) /*-{ - sortMe.sort(comparator); - }-*/; -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentityMap.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentityMap.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentityMap.java deleted file mode 100644 index 53e7dbe..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentityMap.java +++ /dev/null @@ -1,143 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.IdentityMap; - -/** - * Efficient (for js) implementation that will NOT work in hosted mode for - * non-JSO key types. (It will work for all key types in web mode). - * - * Unit tests for this must be run in web mode, or use JSO keys, because - * the implementation sets an expando property on each key object. (This - * also means things like StringMap are not suitable for keys, because of - * the extra property set). The property is 'x$h' - * - * TODO(danilatos): If necessary, make an even more efficient version that - * supports a different each & reduce interface that does not provide the - * key values. Currently, in order to support that, two jso maps are used - * internally, one for keys, one for values. In practice, probably not - * a big deal... - * - * @author [email protected] (Daniel Danilatos) - */ -public class JsIdentityMap<K, V> implements IdentityMap<K, V> { - private final IntMapJsoView<K> keys = IntMapJsoView.create(); - private final IntMapJsoView<V> values = IntMapJsoView.create(); - - /** {@inheritDoc} */ - @Override - public V get(K key) { - return values.get(getId(key)); - } - - /** {@inheritDoc} */ - @Override - public boolean has(K key) { - return keys.has(getId(key)); - } - - /** {@inheritDoc} */ - @Override - public void put(K key, V value) { - int id = getId(key); - keys.put(id, key); - values.put(id, value); - } - - /** {@inheritDoc} */ - @Override - public void remove(K key) { - int id = getId(key); - keys.remove(id); - values.remove(id); - } - - /** {@inheritDoc} */ - @Override - public V removeAndReturn(K key) { - int id = getId(key); - keys.remove(id); - return values.removeAndReturn(id); - } - - /** {@inheritDoc} */ - @Override - public void clear() { - keys.clear(); - values.clear(); - } - - /** {@inheritDoc} */ - @Override - public boolean isEmpty() { - return values.isEmpty(); - } - - /** - * Get the unique id for the object - */ - private int getId(K key) { - return key.hashCode(); - } - - /** {@inheritDoc} */ - @Override - public void each(ProcV<? super K, ? super V> proc) { - eachInner(keys, values, proc); - } - - /** {@inheritDoc} */ - @Override - public <R> R reduce(R initial, Reduce<? super K, ? super V, R> proc) { - return reduceInner(keys, values, initial, proc); - } - - private final native void eachInner(IntMapJsoView<K> keys, IntMapJsoView<V> values, - ProcV<? super K, ? super V> proc) /*-{ - for (var k in values) { - proc. - @org.waveprotocol.wave.model.util.IdentityMap.ProcV::apply(Ljava/lang/Object;Ljava/lang/Object;) - (keys[k], values[k]); - } - }-*/; - - private final native <R> R reduceInner(IntMapJsoView<K> keys, IntMapJsoView<V> values, - R initial, Reduce<? super K, ? super V, R> proc) /*-{ - var reduction = initial; - for (var k in values) { - reduction = proc. - @org.waveprotocol.wave.model.util.IdentityMap.Reduce::apply(Ljava/lang/Object;Ljava/lang/Object;Ljava/lang/Object;) - (reduction, keys[k], values[k]); - } - return reduction; - }-*/; - - /** {@inheritDoc} */ - @Override - public String toString() { - return values.toSource(); - } - - @Override - public int countEntries() { - return values.countEntries(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentitySet.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentitySet.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentitySet.java deleted file mode 100644 index da4300d..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsIdentitySet.java +++ /dev/null @@ -1,98 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.IdentitySet; - -/** - * Efficient (for js) implementation that will NOT work in hosted mode for - * non-JSO key types. (It will work for all key types in web mode). - * - * Unit tests for this must be run in web mode, or use JSO elements, because - * the implementation sets an expando property on each key object. (This - * also means things like StringMap are not suitable for elements, because of - * the extra property set). The property is 'x$h'. - * - * @author [email protected] (Daniel Danilatos) - * @see JsIdentityMap - */ -public final class JsIdentitySet<T> implements IdentitySet<T> { - private final IntMapJsoView<T> elements = IntMapJsoView.create(); - - @Override - public boolean contains(T key) { - return elements.has(getId(key)); - } - - @Override - public void add(T key) { - elements.put(getId(key), key); - } - - @Override - public void remove(T key) { - elements.remove(getId(key)); - } - - @Override - public void clear() { - elements.clear(); - } - - @Override - public boolean isEmpty() { - return elements.isEmpty(); - } - - @Override - public void each(Proc<? super T> proc) { - eachInner(elements, proc); - } - - @Override - public int countEntries() { - return elements.countEntries(); - } - - @Override - public T someElement() { - return elements.someValue(); - } - - /** - * Get the unique id for the object - */ - private int getId(T key) { - return key.hashCode(); - } - - private final native void eachInner(IntMapJsoView<T> elements, Proc<? super T> proc) /*-{ - for (var k in elements) { - proc. - @org.waveprotocol.wave.model.util.ReadableIdentitySet.Proc::apply(Ljava/lang/Object;) - (elements[k]); - } - }-*/; - - @Override - public String toString() { - return elements.toSource(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsRegExp.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsRegExp.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsRegExp.java deleted file mode 100644 index 64f4541..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsRegExp.java +++ /dev/null @@ -1,117 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.core.client.JavaScriptObject; -import com.google.gwt.core.client.JsArrayString; - -import org.waveprotocol.wave.model.util.Preconditions; - -import java.util.ArrayList; -import java.util.List; - -/** - * GWT/JS implementation of the regular expression interface. Uses JSNI - * to call JS methods. - * - */ -public class JsRegExp implements RegExp { - - private final JavaScriptObject regExp; - - /** - * Compiles a javascript regular expression. Returns null if the pattern - * failed to compile. - */ - private static native JavaScriptObject compileRegExp(String pattern, String flags) /*-{ - try { - return new RegExp(pattern, flags); - } catch (e) { - } - return null; - }-*/; - - private static native boolean test(JavaScriptObject regExp, String test) /*-{ - return regExp.test(test); - }-*/; - - private static native JsArrayString matches(JavaScriptObject regExp, String test) /*-{ - return test.match(regExp); - }-*/; - - /** - * Creates a local matching regular expression. Throws an - * {@link IllegalArgumentException} if a bad pattern is given. - * - * @param pattern the pattern to match against. - * @return JS regular expression object. - */ - public static JsRegExp createLocal(String pattern) { - JavaScriptObject jso = compileRegExp(pattern, ""); - Preconditions.checkArgument(jso != null, "Bad regex pattern"); - return new JsRegExp(jso); - } - - /** - * Creates a global matching regular expression. Throws an - * {@link IllegalArgumentException} if a bad pattern is given. - * - * @param pattern the pattern to match against. - * @return JS regular expression object. - */ - public static JsRegExp createGlobal(String pattern) { - JavaScriptObject jso = compileRegExp(pattern, "g"); - Preconditions.checkArgument(jso != null, "Bad regex pattern"); - return new JsRegExp(jso); - } - - /** - * Tests whether a given pattern is valid or not. - * - * @param pattern the pattern to match against. - * @return true if the pattern is valid. - */ - public static boolean isValidPattern(String pattern) { - JavaScriptObject jso = compileRegExp(pattern, ""); - return jso != null; - } - - private JsRegExp(JavaScriptObject regExp) { - this.regExp = regExp; - } - - @Override - public boolean test(String test) { - return test(regExp, test); - } - - @Override - public List<String> getMatches(String test) { - JsArrayString matches = matches(regExp, test); - if (matches != null && matches.length() > 0) { - List<String> result = new ArrayList<String>(matches.length()); - for (int i = 0; i < matches.length(); ++i) { - result.add(matches.get(i)); - } - return result; - } - return new ArrayList<String>(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoCollectionFactory.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoCollectionFactory.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoCollectionFactory.java deleted file mode 100644 index b2f2b07..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoCollectionFactory.java +++ /dev/null @@ -1,74 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.CollectionFactory; -import org.waveprotocol.wave.model.util.IdentityMap; -import org.waveprotocol.wave.model.util.IdentitySet; - -import java.util.Queue; - -/** - * An implementation of CollectionFactory based on JavaScript objects. - * - * @author [email protected] (Christian Ohler) - */ -public final class JsoCollectionFactory implements CollectionFactory { - - @Override - public <V> JsoStringMap<V> createStringMap() { - return JsoStringMap.create(); - } - - @Override - public <V> JsoNumberMap<V> createNumberMap() { - return JsoNumberMap.create(); - } - - @Override - public <V> JsoIntMap<V> createIntMap() { - return JsoIntMap.create(); - } - - @Override - public JsoStringSet createStringSet() { - return JsoStringSet.create(); - } - - @Override - public <T> IdentitySet<T> createIdentitySet() { - return new JsIdentitySet<T>(); - } - - @Override - public <E> Queue<E> createQueue() { - return new FastQueue<E>(); - } - - @Override - public org.waveprotocol.wave.model.util.NumberPriorityQueue createPriorityQueue() { - return new NumberPriorityQueue(); - } - - @Override - public <K, V> IdentityMap<K, V> createIdentityMap() { - return new JsIdentityMap<K, V>(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoIntMap.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoIntMap.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoIntMap.java deleted file mode 100644 index 1af2e9c..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoIntMap.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.IntMap; -import org.waveprotocol.wave.model.util.ReadableIntMap; - -import java.util.Map; - -/** - * An implementation of IntMap<V> based on JavaScript objects. - * - * @author [email protected] (Christian Ohler) - * - * @param <V> type of values in the map - */ -public class JsoIntMap<V> implements IntMap<V> { - - final org.waveprotocol.wave.client.common.util.IntMapJsoView<V> backend = - org.waveprotocol.wave.client.common.util.IntMapJsoView.create(); - - private JsoIntMap() {} - - public static <V> JsoIntMap<V> create() { - return new JsoIntMap<V>(); - } - - @Override - public void clear() { - backend.clear(); - } - - @Override - public boolean containsKey(int key) { - return backend.has(key); - } - - @Override - public V getExisting(int key) { - return backend.get(key); - } - - @Override - public V get(int key, V defaultValue) { - if (backend.has(key)) { - return backend.get(key); - } else { - return defaultValue; - } - } - - @Override - public V get(int key) { - return backend.get(key); - } - - @Override - public void put(int key, V value) { - backend.put(key, value); - } - - @Override - public void putAll(ReadableIntMap<V> pairsToAdd) { - // TODO(ohler): check instanceof here and implement a fallback. - ((JsoIntMap<V>) pairsToAdd).backend.addToMap(this.backend); - } - - @Override - public void putAll(Map<Integer, V> pairsToAdd) { - for (Map.Entry<Integer, V> e : pairsToAdd.entrySet()) { - backend.put(e.getKey(), e.getValue()); - } - } - - @Override - public void remove(int key) { - backend.remove(key); - } - - @Override - public void each(final ProcV<V> callback) { - backend.each(new ProcV<V>() { - @Override - public void apply(int key, V item) { - callback.apply(key, item); - } - }); - } - - @Override - public void filter(final EntryFilter<V> filter) { - backend.each(new ProcV<V>() { - @Override - public void apply(int key, V item) { - if (filter.apply(key, item)) { - // entry stays - } else { - backend.remove(key); - } - } - }); - } - - @Override - public boolean isEmpty() { - return backend.isEmpty(); - } - - @Override - public int countEntries() { - return backend.countEntries(); - } - - @Override - public String toString() { - return JsoMapStringBuilder.toString(backend); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapBase.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapBase.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapBase.java deleted file mode 100644 index dd24732..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapBase.java +++ /dev/null @@ -1,68 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.gwt.core.client.JavaScriptObject; - -/** - * Common type-independent methods for optimised JSO maps - * - * Designed to be subclassed, but also useful in its own right for its methods - * - * @author [email protected] (Daniel Danilatos) - */ -public abstract class JsoMapBase extends JavaScriptObject { - - protected JsoMapBase() {} - - /** - * Removes all entries from this map. - */ - public final native void clear() /*-{ - for (var key in this) { - delete this[key]; - } - }-*/; - - /** - * Tests whether this map is empty. - * - * @return true if this map has no entries. - */ - public final native boolean isEmpty() /*-{ - for (var k in this) { - return false; - } - return true; - }-*/; - - /** - * Counts the number of entries in this map. This is a time-consuming - * operation. - */ - public final native int countEntries() /*-{ - var n = 0; - for (var k in this) { - n++; - } - return n; - }-*/; - -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapStringBuilder.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapStringBuilder.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapStringBuilder.java deleted file mode 100644 index efea2b2..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoMapStringBuilder.java +++ /dev/null @@ -1,102 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.IdentityMap; -import org.waveprotocol.wave.model.util.ReadableIntMap; -import org.waveprotocol.wave.model.util.ReadableNumberMap; -import org.waveprotocol.wave.model.util.ReadableStringMap; - -/** - * Can render each kind of JsoMapBase as a string. - * - */ -public final class JsoMapStringBuilder implements - ReadableStringMap.ProcV<Object>, ReadableIntMap.ProcV<Object>, - ReadableNumberMap.ProcV<Object>, IdentityMap.ProcV<Object, Object> { - - /** Singleton used by statics. */ - private static final JsoMapStringBuilder INSTANCE = new JsoMapStringBuilder(); - - /** Builder used during lifetime of each doString. */ - private StringBuilder builder = null; - - public static String toString(IntMapJsoView<?> m) { - return INSTANCE.doString(m); - } - - public static String toString(NumberMapJsoView<?> m) { - return INSTANCE.doString(m); - } - - public static String toString(IdentityMap<?,?> m) { - return INSTANCE.doString(m); - } - - public String doString(IntMapJsoView<?> m) { - builder = new StringBuilder(); - builder.append("{"); - m.each(this); - builder.append("}"); - String result = builder.toString(); - builder = null; - return result; - } - - public String doString(NumberMapJsoView<?> m) { - builder = new StringBuilder(); - builder.append("{"); - m.each(this); - builder.append("}"); - String result = builder.toString(); - builder = null; - return result; - } - - public String doString(IdentityMap<?,?> m) { - builder = new StringBuilder(); - builder.append("{"); - m.each(this); - builder.append("}"); - String result = builder.toString(); - builder = null; - return result; - } - - @Override - public void apply(String key, Object item) { - builder.append(" " + key + ": " + item + "; "); - } - - @Override - public void apply(double key, Object item) { - builder.append(" " + key + ": " + item + "; "); - } - - @Override - public void apply(int key, Object item) { - builder.append(" " + key + ": " + item + "; "); - } - - @Override - public void apply(Object key, Object item) { - builder.append(" " + key + ": " + item + "; "); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoNumberMap.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoNumberMap.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoNumberMap.java deleted file mode 100644 index fc19d0a..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoNumberMap.java +++ /dev/null @@ -1,135 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import org.waveprotocol.wave.model.util.NumberMap; -import org.waveprotocol.wave.model.util.ReadableNumberMap; - -import java.util.Map; - -/** - * An implementation of NumberMap<V> based on JavaScript objects. - * - * @author [email protected] (Christian Ohler) - * - * @param <V> type of values in the map - */ -public class JsoNumberMap<V> implements NumberMap<V> { - - final org.waveprotocol.wave.client.common.util.NumberMapJsoView<V> backend = - org.waveprotocol.wave.client.common.util.NumberMapJsoView.create(); - - private JsoNumberMap() {} - - public static <V> JsoNumberMap<V> create() { - return new JsoNumberMap<V>(); - } - - @Override - public void clear() { - backend.clear(); - } - - @Override - public boolean containsKey(double key) { - return backend.has(key); - } - - @Override - public V getExisting(double key) { - return backend.get(key); - } - - @Override - public V get(double key, V defaultValue) { - if (backend.has(key)) { - return backend.get(key); - } else { - return defaultValue; - } - } - - @Override - public V get(double key) { - return backend.get(key); - } - - @Override - public void put(double key, V value) { - backend.put(key, value); - } - - @Override - public void putAll(ReadableNumberMap<V> pairsToAdd) { - // TODO(ohler): check instanceof here and implement a fallback. - ((JsoNumberMap<V>) pairsToAdd).backend.addToMap(this.backend); - } - - @Override - public void putAll(Map<Double, V> pairsToAdd) { - for (Map.Entry<Double, V> e : pairsToAdd.entrySet()) { - backend.put(e.getKey(), e.getValue()); - } - } - - @Override - public void remove(double key) { - backend.remove(key); - } - - @Override - public void each(final ProcV<V> callback) { - backend.each(new ProcV<V>() { - @Override - public void apply(double key, V item) { - callback.apply(key, item); - } - }); - } - - @Override - public void filter(final EntryFilter<V> filter) { - backend.each(new ProcV<V>() { - @Override - public void apply(double key, V item) { - if (filter.apply(key, item)) { - // entry stays - } else { - backend.remove(key); - } - } - }); - } - - @Override - public boolean isEmpty() { - return backend.isEmpty(); - } - - @Override - public int countEntries() { - return backend.countEntries(); - } - - @Override - public String toString() { - return JsoMapStringBuilder.toString(backend); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringMap.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringMap.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringMap.java deleted file mode 100644 index 58d107c..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringMap.java +++ /dev/null @@ -1,199 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import com.google.common.annotations.VisibleForTesting; - -import org.waveprotocol.wave.model.util.Preconditions; -import org.waveprotocol.wave.model.util.ReadableStringMap; -import org.waveprotocol.wave.model.util.ReadableStringSet; -import org.waveprotocol.wave.model.util.StringMap; - -import java.util.Map; - -/** - * An implementation of StringMap<V> based on JavaScript objects. - * - * @author [email protected] (Christian Ohler) - * - * @param <V> type of values in the map - */ -public class JsoStringMap<V> implements StringMap<V> { - - @VisibleForTesting public final JsoView backend; - - private JsoStringMap(JsoView backend) { - this.backend = backend; - } - - @SuppressWarnings("unchecked") - public static <V> JsoStringMap<V> create() { - return new JsoStringMap<V>(createBackend()); - } - - /** Construct an empty StringMap */ - static JsoView createBackend() { - if (!QuirksConstants.DOES_NOT_SUPPORT_JSO_PROTO_FIELD) { - return createProtoless(); - } else { - return JsoView.create(); - } - } - - /** Construct an empty StringMap with null for the hidden __proto__ field */ - private static native JsoView createProtoless() /*-{ - return {__proto__:null}; - }-*/; - - @Override - public void clear() { - backend.clear(); - } - - @Override - public boolean containsKey(String key) { - return backend.containsKey(escape(key)); - } - - @Override - public V getExisting(String key) { - key = escape(key); - if (!backend.containsKey(key)) { - // Not using Preconditions.checkState to avoid unecessary string concatenation - throw new IllegalStateException("getExisting: Key '" + key + "' is not in map"); - } - return backend.<V>getObjectUnsafe(key); - } - - @Override - public V get(String key, V defaultValue) { - key = escape(key); - if (backend.containsKey(key)) { - return backend.<V>getObjectUnsafe(key); - } else { - return defaultValue; - } - } - - @Override - public V get(String key) { - return backend.<V>getObjectUnsafe(escape(key)); - } - - @Override - public void put(String key, V value) { - backend.setObject(escape(key), value); - } - - @Override - public String someKey() { - return backend.firstKey(); - } - - /** - * The purpose of these methods is to prevent the __proto__ key from ever - * being set on the underlying JSO. - */ - static String escape(String key) { - Preconditions.checkNotNull(key, "StringMap/StringSet cannot contain null keys"); - return QuirksConstants.DOES_NOT_SUPPORT_JSO_PROTO_FIELD || key.startsWith("__") ? - '_' + key : key; - } - - static String unescape(String key) { - return QuirksConstants.DOES_NOT_SUPPORT_JSO_PROTO_FIELD || key.startsWith("__") ? - key.substring(1) : key; - } - - @Override - public void putAll(ReadableStringMap<V> pairsToAdd) { - // This cast should not fail as we should not have other - // implementations of ReadableStringMap in the client. - backend.putAll(((JsoStringMap<V>) pairsToAdd).backend); - } - - @Override - public void putAll(Map<String, V> pairsToAdd) { - for (Map.Entry<String, V> e : pairsToAdd.entrySet()) { - backend.setObject(escape(e.getKey()), e.getValue()); - } - } - - @Override - public void remove(String key) { - backend.remove(escape(key)); - } - - @Override - public void each(final ProcV<? super V> callback) { - backend.each(new ProcV<V>() { - @Override - public void apply(String key, V item) { - callback.apply(unescape(key), item); - } - }); - } - - @Override - public void filter(final EntryFilter<? super V> filter) { - backend.each(new ProcV<V>() { - @Override - public void apply(String key, V item) { - if (filter.apply(unescape(key), item)) { - // entry stays - } else { - backend.remove(key); - } - } - }); - } - - @Override - public boolean isEmpty() { - return backend.isEmpty(); - } - - @Override - public int countEntries() { - return backend.countEntries(); - } - - @SuppressWarnings("unchecked") // Safe because we return a read-only string set - @Override - public ReadableStringSet keySet() { - return new JsoStringSet(backend); - } - - @Override - public String toString() { - final StringBuilder b = new StringBuilder("{"); - each(new ProcV<V>() { - @Override - public void apply(String key, V item) { - if (b.length() > 1) { - b.append(","); - } - b.append(key + ":" + item); - } - }); - b.append("}"); - return b.toString(); - } -} http://git-wip-us.apache.org/repos/asf/incubator-wave/blob/051db092/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringSet.java ---------------------------------------------------------------------- diff --git a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringSet.java b/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringSet.java deleted file mode 100644 index b733bf6..0000000 --- a/wave/src/main/java/org/waveprotocol/wave/client/common/util/JsoStringSet.java +++ /dev/null @@ -1,203 +0,0 @@ -/** - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -package org.waveprotocol.wave.client.common.util; - -import static org.waveprotocol.wave.client.common.util.JsoStringMap.escape; -import static org.waveprotocol.wave.client.common.util.JsoStringMap.unescape; - -import com.google.common.annotations.VisibleForTesting; - -import org.waveprotocol.wave.model.util.ReadableStringSet; -import org.waveprotocol.wave.model.util.StringSet; -import org.waveprotocol.wave.model.util.ReadableStringMap.ProcV; - -import java.util.Set; - -/** - * An implementation of StringSet based on JavaScript objects. - * - * @author [email protected] (Christian Ohler) - */ -public class JsoStringSet implements StringSet { - - @VisibleForTesting - public final JsoView backend; - - private static final Object AN_OBJECT = new Object(); - - /** - * Exposed for JsoStringMap - * @param backend - */ - JsoStringSet(JsoView backend) { - this.backend = backend; - } - - public static JsoStringSet create() { - return new JsoStringSet(JsoStringMap.createBackend()); - } - - @Override - public void add(String s) { - backend.setObject(escape(s), AN_OBJECT); - } - - @Override - public void clear() { - backend.clear(); - } - - @Override - public boolean contains(String s) { - return backend.containsKey(escape(s)); - } - - @Override - public void each(final Proc callback) { - backend.each(new ProcV<Object>() { - @Override - public void apply(String key, Object item) { - callback.apply(unescape(key)); - } - }); - } - - @Override - public void filter(final StringPredicate filter) { - backend.each(new ProcV<Object>() { - @Override - public void apply(String key, Object item) { - if (filter.apply(unescape(key))) { - // entry stays - } else { - backend.remove(key); - } - } - }); - } - - @Override - public boolean isEmpty() { - return backend.isEmpty(); - } - - @Override - public String someElement() { - return backend.firstKey(); - } - - @Override - public void remove(String s) { - backend.remove(escape(s)); - } - - private static class False extends RuntimeException { - private False() { - super("Preallocated exception without a meaningful stacktrace"); - } - @Override - public Throwable fillInStackTrace() { - // don't fill in the stack trace, which is slow (especially on client) - return this; - } - } - private static final False FALSE = new False(); - - @Override - public boolean isSubsetOf(final Set<String> set) { - try { - each(new Proc() { - @Override - public void apply(String element) { - if (!set.contains(element)) { - throw FALSE; - } - } - }); - return true; - } catch (False e) { - assert e == FALSE; - return false; - } - } - - @Override - public void addAll(ReadableStringSet set) { - set.each(new Proc() { - public void apply(String element) { - add(element); - } - }); - } - - @Override - public void removeAll(ReadableStringSet set) { - set.each(new Proc() { - public void apply(String element) { - remove(element); - } - }); - } - - @Override - public boolean isSubsetOf(final ReadableStringSet other) { - try { - each(new Proc() { - @Override - public void apply(String element) { - if (!other.contains(element)) { - throw FALSE; - } - } - }); - } catch (RuntimeException e) { - if (e != FALSE) { - throw e; - } - return false; - } - return true; - } - - @Override - public int countEntries() { - return backend.countEntries(); - } - - @Override - public String toString() { - final StringBuilder b = new StringBuilder("{"); - each(new Proc() { - @Override - public void apply(String element) { - if (b.length() > 1) { - b.append(","); - } - try { - b.append("'" + element.replaceAll("\\\\", "\\\\").replaceAll("'", "\\'") + "'"); - } catch (RuntimeException e) { - b.append("REGEX DEATH - " + element); - } - } - }); - b.append("}"); - return b.toString(); - } -}
