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.

Payload.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. /*
  2. * Copyright 2000-2016 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.shared.ui.dnd.criteria;
  17. import java.io.Serializable;
  18. /**
  19. * Stores key/value pairs and the value type. Payload is set in
  20. * DragSourceExtension and is transferred during drag operation. It is used for
  21. * comparing values to acceptance criteria.
  22. *
  23. * @since 8.1
  24. */
  25. public class Payload implements Serializable {
  26. /**
  27. * Type of the payload's value.
  28. */
  29. public enum ValueType {
  30. STRING, INTEGER, DOUBLE;
  31. }
  32. /**
  33. * Prefix of the payload data type.
  34. */
  35. public static final String ITEM_PREFIX = "v-item";
  36. private String key;
  37. private String value;
  38. private ValueType valueType;
  39. /**
  40. * Mandatory zero arg constructor.
  41. */
  42. private Payload() {
  43. }
  44. /**
  45. * Creates a payload object.
  46. *
  47. * @param key
  48. * key of the payload
  49. * @param value
  50. * value of the payload
  51. * @param valueType
  52. * type of the payload value
  53. */
  54. public Payload(String key, String value, ValueType valueType) {
  55. this.key = key;
  56. this.value = value;
  57. this.valueType = valueType;
  58. }
  59. /**
  60. * Gets the key of this payload.
  61. *
  62. * @return key identifying this payload
  63. */
  64. public String getKey() {
  65. return key;
  66. }
  67. /**
  68. * Gets the value of this payload.
  69. *
  70. * @return value of this payload
  71. */
  72. public String getValue() {
  73. return value;
  74. }
  75. /**
  76. * Gets the value type of this payload.
  77. *
  78. * @return the type of the value of this payload
  79. */
  80. public ValueType getValueType() {
  81. return valueType;
  82. }
  83. /**
  84. * Returns the string representation of this payload. It is used as the data
  85. * type in the {@code DataTransfer} object.
  86. *
  87. * @return the string representation of this payload
  88. */
  89. public String getPayloadString() {
  90. return ITEM_PREFIX + ":" + valueType.name().toLowerCase() + ":" + key
  91. + ":" + value;
  92. }
  93. /**
  94. * Parses a payload string and returns a payload object represented by that
  95. * string.
  96. *
  97. * @param payloadString
  98. * string that represents a payload object
  99. * @return a payload object represented by the given string
  100. */
  101. public static Payload parse(String payloadString) {
  102. String[] parts = payloadString.split(":");
  103. if (parts.length != 4 || !ITEM_PREFIX.equals(parts[0])) {
  104. throw new IllegalArgumentException(
  105. "Data type does not have a valid payload format");
  106. }
  107. // Create payload object of the given parts. Value type is converted to
  108. // upper case to match the enum's case.
  109. return new Payload(parts[2], parts[3],
  110. ValueType.valueOf(parts[1].toUpperCase()));
  111. }
  112. }