You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ValueMap.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /*
  2. * Copyright 2000-2018 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.client;
  17. import java.util.HashSet;
  18. import java.util.Set;
  19. import com.google.gwt.core.client.JavaScriptObject;
  20. import com.google.gwt.core.client.JsArray;
  21. import com.google.gwt.core.client.JsArrayString;
  22. public final class ValueMap extends JavaScriptObject {
  23. protected ValueMap() {
  24. }
  25. public native double getRawNumber(final String name)
  26. /*-{
  27. return this[name];
  28. }-*/;
  29. public native int getInt(final String name)
  30. /*-{
  31. return this[name];
  32. }-*/;
  33. public native boolean getBoolean(final String name)
  34. /*-{
  35. return Boolean(this[name]);
  36. }-*/;
  37. public native String getString(String name)
  38. /*-{
  39. return this[name];
  40. }-*/;
  41. public native JsArrayString getKeyArray()
  42. /*-{
  43. var a = new Array();
  44. var attr = this;
  45. for (var j in attr) {
  46. // workaround for the infamous chrome hosted mode hack (__gwt_ObjectId)
  47. if (attr.hasOwnProperty(j))
  48. a.push(j);
  49. }
  50. return a;
  51. }-*/;
  52. public Set<String> getKeySet() {
  53. final HashSet<String> attrs = new HashSet<>();
  54. JsArrayString attributeNamesArray = getKeyArray();
  55. for (int i = 0; i < attributeNamesArray.length(); i++) {
  56. attrs.add(attributeNamesArray.get(i));
  57. }
  58. return attrs;
  59. }
  60. public native JsArrayString getJSStringArray(String name)
  61. /*-{
  62. return this[name];
  63. }-*/;
  64. public native JsArray<ValueMap> getJSValueMapArray(String name)
  65. /*-{
  66. return this[name];
  67. }-*/;
  68. public String[] getStringArray(final String name) {
  69. JsArrayString stringArrayAttribute = getJSStringArray(name);
  70. final String[] s = new String[stringArrayAttribute.length()];
  71. for (int i = 0; i < stringArrayAttribute.length(); i++) {
  72. s[i] = stringArrayAttribute.get(i);
  73. }
  74. return s;
  75. }
  76. public int[] getIntArray(final String name) {
  77. JsArrayString stringArrayAttribute = getJSStringArray(name);
  78. final int[] s = new int[stringArrayAttribute.length()];
  79. for (int i = 0; i < stringArrayAttribute.length(); i++) {
  80. s[i] = Integer.parseInt(stringArrayAttribute.get(i));
  81. }
  82. return s;
  83. }
  84. public native boolean containsKey(final String name)
  85. /*-{
  86. return name in this;
  87. }-*/;
  88. public native ValueMap getValueMap(String name)
  89. /*-{
  90. return this[name];
  91. }-*/;
  92. public native String getAsString(String name)
  93. /*-{
  94. return '' + this[name];
  95. }-*/;
  96. public native JavaScriptObject getJavaScriptObject(String name)
  97. /*-{
  98. return this[name];
  99. }-*/;
  100. }