PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/Main/Source/Libraries/TSFJavaLibrary/src/org/gpa/tsf/transport/SignalIndexCache.java

#
Java | 128 lines | 41 code | 11 blank | 76 comment | 0 complexity | d6307abd4e4420dc5bd7faccb5dde20d MD5 | raw file
Possible License(s): EPL-1.0
  1. //******************************************************************************************************
  2. // SignalIndexCache.java - Gbtc
  3. //
  4. // Copyright Š 2010, Grid Protection Alliance. All Rights Reserved.
  5. //
  6. // Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
  7. // the NOTICE file distributed with this work for additional information regarding copyright ownership.
  8. // The GPA licenses this file to you under the Eclipse Public License -v 1.0 (the "License"); you may
  9. // not use this file except in compliance with the License. You may obtain a copy of the License at:
  10. //
  11. // http://www.opensource.org/licenses/eclipse-1.0.php
  12. //
  13. // Unless agreed to in writing, the subject software distributed under the License is distributed on an
  14. // "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
  15. // License for the specific language governing permissions and limitations.
  16. //
  17. // Code Modification History:
  18. // ----------------------------------------------------------------------------------------------------
  19. // 04/12/2012 - Stephen C. Wills
  20. // Generated original version of source code.
  21. //
  22. //******************************************************************************************************
  23. package org.gpa.tsf.transport;
  24. import java.util.HashMap;
  25. import java.util.Map;
  26. import java.util.UUID;
  27. import org.gpa.tsf.MeasurementKey;
  28. /**
  29. * Represents a cache used to map 16-bit indexes to {@link MeasurementKey}s
  30. * in order to compact measurement IDs for the compact measurement format.
  31. */
  32. public class SignalIndexCache
  33. {
  34. private Map<Short, MeasurementKey> m_reference;
  35. private Map<UUID, Short> m_signalIdCache;
  36. /**
  37. * Constructs a new signal index cache.
  38. */
  39. public SignalIndexCache()
  40. {
  41. m_reference = new HashMap<Short, MeasurementKey>();
  42. m_signalIdCache = new HashMap<UUID, Short>();
  43. }
  44. /**
  45. * Adds a measurement key to the signal index
  46. * cache, identified by the given signal index.
  47. *
  48. * @param signalIndex the 16-bit index by which the
  49. * measurement key can be retrieved from the cache
  50. * @param signalId the measurement's globally unique identifier
  51. * @param source the measurement's source
  52. * @param id the measurement's simple numeric identifier
  53. * @throws IllegalArgumentException if the given {@code signalID} is
  54. * {@code null} or the given {@code source} is {@code null} or
  55. * empty
  56. */
  57. public void addMeasurementKey(short signalIndex, UUID signalId, String source, int id)
  58. {
  59. addMeasurementKey(signalIndex, new MeasurementKey(signalId, source, id));
  60. }
  61. /**
  62. * Adds a measurement key to the signal index
  63. * cache, identified by the given signal index.
  64. *
  65. * @param signalIndex the 16-bit index by which the
  66. * measurement key can be retrieved from the cache
  67. * @param key the measurement key to be placed in the cache
  68. */
  69. public void addMeasurementKey(short signalIndex, MeasurementKey key)
  70. {
  71. m_reference.put(signalIndex, key);
  72. m_signalIdCache.put(key.getSignalId(), signalIndex);
  73. }
  74. /**
  75. * Empties the signal index cache.
  76. */
  77. public void clear()
  78. {
  79. m_reference.clear();
  80. m_signalIdCache.clear();
  81. }
  82. /**
  83. * Determines whether a measurement key exists
  84. * in the cache for the given signal index.
  85. *
  86. * @param signalIndex the signal index to be searched for
  87. * @return true if the signal index refers to
  88. * a measurement key; false otherwise
  89. */
  90. public boolean contains(short signalIndex)
  91. {
  92. return m_reference.containsKey(signalIndex);
  93. }
  94. /**
  95. * Gets the measurement key identified by the given signal index.
  96. *
  97. * @param signalIndex the signal index which identifies the measurement key
  98. * @return the measurement key identified by the given signal index,
  99. * or {@code null} if no mapping exists for the signal index
  100. */
  101. public MeasurementKey getMeasurementKey(short signalIndex)
  102. {
  103. return m_reference.get(signalIndex);
  104. }
  105. /**
  106. * Provides a backwards mapping from the globally unique identifiers
  107. * of the measurements whose keys are stored in the cache to the
  108. * signal indexes that refer to them.
  109. *
  110. * @param signalId the measurement's globally unique identifier
  111. * @return the signal index which identifies the measurement key
  112. */
  113. short getSignalIndex(UUID signalId)
  114. {
  115. return m_signalIdCache.get(signalId);
  116. }
  117. }