您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

KeyMapper.java 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /*
  2. @ITMillApache2LicenseForJavaFiles@
  3. */
  4. package com.itmill.toolkit.terminal;
  5. import java.util.Hashtable;
  6. /**
  7. * <code>KeyMapper</code> is the simple two-way map for generating textual
  8. * keys for objects and retrieving the objects later with the key.
  9. *
  10. * @author IT Mill Ltd.
  11. * @version
  12. * @VERSION@
  13. * @since 3.0
  14. */
  15. public class KeyMapper {
  16. private int lastKey = 0;
  17. private final Hashtable objectKeyMap = new Hashtable();
  18. private final Hashtable keyObjectMap = new Hashtable();
  19. /**
  20. * Gets key for an object.
  21. *
  22. * @param o
  23. * the object.
  24. */
  25. public String key(Object o) {
  26. if (o == null) {
  27. return "null";
  28. }
  29. // If the object is already mapped, use existing key
  30. String key = (String) objectKeyMap.get(o);
  31. if (key != null) {
  32. return key;
  33. }
  34. // If the object is not yet mapped, map it
  35. key = String.valueOf(++lastKey);
  36. objectKeyMap.put(o, key);
  37. keyObjectMap.put(key, o);
  38. return key;
  39. }
  40. /**
  41. * Checks if the key belongs to a new id.
  42. * <p>
  43. * Usage of new id:s are specific to components, but for example Select
  44. * component uses newItemId:s for selection of newly added items in
  45. * <code>allowNewItems</code>-mode.
  46. *
  47. * @param key
  48. * @return <code>true</code> if the key belongs to the new id,otherwise
  49. * <code>false</code>.
  50. */
  51. public boolean isNewIdKey(String key) {
  52. return "NEW".equals(key);
  53. }
  54. /**
  55. * Retrieves object with the key.
  56. *
  57. * @param key
  58. * the name with the desired value.
  59. * @return the object with the key.
  60. */
  61. public Object 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. public void remove(Object removeobj) {
  71. final String key = (String) objectKeyMap.get(removeobj);
  72. if (key != null) {
  73. objectKeyMap.remove(key);
  74. keyObjectMap.remove(removeobj);
  75. }
  76. }
  77. /**
  78. * Removes all objects from the mapper.
  79. */
  80. public void removeAll() {
  81. objectKeyMap.clear();
  82. keyObjectMap.clear();
  83. }
  84. }