PageRenderTime 20ms CodeModel.GetById 14ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

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