Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

Payload.java 3.3KB

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