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.

KeyMapper.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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.server;
  17. import java.io.Serializable;
  18. import java.util.HashMap;
  19. import com.vaadin.data.provider.DataKeyMapper;
  20. /**
  21. * <code>KeyMapper</code> is the simple two-way map for generating textual keys
  22. * for objects and retrieving the objects later with the key.
  23. *
  24. * @author Vaadin Ltd.
  25. * @since 3.0
  26. */
  27. public class KeyMapper<V> implements DataKeyMapper<V>, Serializable {
  28. private int lastKey = 0;
  29. private final HashMap<V, String> objectKeyMap = new HashMap<>();
  30. private final HashMap<String, V> keyObjectMap = new HashMap<>();
  31. /**
  32. * Gets key for an object.
  33. *
  34. * @param o
  35. * the object.
  36. */
  37. @Override
  38. public String key(V o) {
  39. if (o == null) {
  40. return "null";
  41. }
  42. // If the object is already mapped, use existing key
  43. String key = objectKeyMap.get(o);
  44. if (key != null) {
  45. return key;
  46. }
  47. // If the object is not yet mapped, map it
  48. key = String.valueOf(++lastKey);
  49. objectKeyMap.put(o, key);
  50. keyObjectMap.put(key, o);
  51. return key;
  52. }
  53. /**
  54. * Retrieves object with the key.
  55. *
  56. * @param key
  57. * the name with the desired value.
  58. * @return the object with the key.
  59. */
  60. @Override
  61. public V get(String key) {
  62. return keyObjectMap.get(key);
  63. }
  64. /**
  65. * Removes object from the mapper.
  66. *
  67. * @param removeobj
  68. * the object to be removed.
  69. */
  70. @Override
  71. public void remove(V removeobj) {
  72. final String key = objectKeyMap.get(removeobj);
  73. if (key != null) {
  74. objectKeyMap.remove(removeobj);
  75. keyObjectMap.remove(key);
  76. }
  77. }
  78. /**
  79. * Removes all objects from the mapper.
  80. */
  81. @Override
  82. public void removeAll() {
  83. objectKeyMap.clear();
  84. keyObjectMap.clear();
  85. }
  86. /**
  87. * Checks if the given key is mapped to an object.
  88. *
  89. * @since 7.7
  90. *
  91. * @param key
  92. * the key to check
  93. * @return <code>true</code> if the key is currently mapped,
  94. * <code>false</code> otherwise
  95. */
  96. public boolean containsKey(String key) {
  97. return keyObjectMap.containsKey(key);
  98. }
  99. }