/talkback_preics/src/com/google/android/marvin/talkback/Utterance.java

http://eyes-free.googlecode.com/ · Java · 160 lines · 55 code · 19 blank · 86 comment · 4 complexity · 22d402cce593f56eccc0e3557ebce7a7 MD5 · raw file

  1. /*
  2. * Copyright (C) 2009 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of 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,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.google.android.marvin.talkback;
  17. import java.util.HashMap;
  18. /**
  19. * This class represents an utterance composed of text to be spoken
  20. * and meta data about how this text to be spoken. The utterances
  21. * are cached in a pool of instances to be reused as an optimization
  22. * to reduce new object instantiation.
  23. *
  24. * @author svetoslavganov@google.com (Svetoslav Ganov)
  25. *
  26. */
  27. public class Utterance {
  28. /**
  29. * Key for obtaining the queuing meta-data property.
  30. */
  31. public static final String KEY_METADATA_QUEUING = "queuing";
  32. /**
  33. * The maximal size of the pool with cached utterances.
  34. */
  35. private static final int MAX_POOL_SIZE = 3;
  36. /**
  37. * Mutex lock for accessing the utterance pool.
  38. */
  39. private static final Object sPoolLock = new Object();
  40. /**
  41. * Pool of cached utterances.
  42. */
  43. private static Utterance sPool;
  44. /**
  45. * The current size of the utterance pool.
  46. */
  47. private static int sPoolSize;
  48. /**
  49. * The next cached utterance
  50. */
  51. private Utterance mNext;
  52. /**
  53. * Denotes if an utterance is currently in the cache pool.
  54. */
  55. private boolean mIsInPool;
  56. /**
  57. * The text of the utterance.
  58. */
  59. private final StringBuilder mText = new StringBuilder();
  60. /**
  61. * Meta-data of how the utterance should be spoken.
  62. */
  63. private final HashMap<String, Object> mMetadata = new HashMap<String, Object>();
  64. /**
  65. * Creates a new instance.
  66. */
  67. private Utterance() {
  68. /* do nothing - reducing constructor visibility */
  69. }
  70. /**
  71. * Returns a cached instance if such is available or a new
  72. * one is instantiated.
  73. *
  74. * @return An instance.
  75. */
  76. public static Utterance obtain() {
  77. return obtain("");
  78. }
  79. /**
  80. * Returns a cached instance if such is available or a new
  81. * one is instantiated and sets its <code>text</code>.
  82. *
  83. * @param text The text of the returned utterance.
  84. * @return An instance.
  85. */
  86. public static Utterance obtain(String text) {
  87. synchronized (sPoolLock) {
  88. if (sPool != null) {
  89. Utterance utterance = sPool;
  90. sPool = sPool.mNext;
  91. sPoolSize--;
  92. utterance.mNext = null;
  93. utterance.mIsInPool = false;
  94. return utterance;
  95. }
  96. return new Utterance();
  97. }
  98. }
  99. /**
  100. * Gets the text of this utterance.
  101. *
  102. * @return The utterance text.
  103. */
  104. public StringBuilder getText() {
  105. return mText;
  106. }
  107. /**
  108. * Gets the mets-data of this utterance.
  109. *
  110. * @return The utterance meta-data.
  111. */
  112. public HashMap<String, Object> getMetadata() {
  113. return mMetadata;
  114. }
  115. /**
  116. * Return an instance back to be reused.
  117. * <p>
  118. * <b>Note: You must not touch the object after calling this function.</b>
  119. */
  120. public void recycle() {
  121. if (mIsInPool) {
  122. return;
  123. }
  124. clear();
  125. synchronized (sPoolLock) {
  126. if (sPoolSize <= MAX_POOL_SIZE) {
  127. mNext = sPool;
  128. sPool = this;
  129. mIsInPool = true;
  130. sPoolSize++;
  131. }
  132. }
  133. }
  134. /**
  135. * Clears the state of this instance.
  136. */
  137. private void clear() {
  138. mText.delete(0, mText.length());
  139. mMetadata.clear();
  140. }
  141. }