PageRenderTime 26ms CodeModel.GetById 15ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/ime/aimelib/src/com/google/android/marvin/aime/Position.java

http://eyes-free.googlecode.com/
Java | 139 lines | 63 code | 20 blank | 56 comment | 4 complexity | 50c8fec5686cad88c6a81cc381f637a3 MD5 | raw file
  1/*
  2 * Copyright (C) 2010 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.aime;
 18
 19import java.text.BreakIterator;
 20
 21/**
 22 * Stores begin and end positions of text. If any of the position, <code>mStart</code> or
 23 * <code>mEnd</code> is <code>INVALID_POSITION</code>, it is an invalid position.
 24 *
 25 * @author hiteshk@google.com (Hitesh Khandelwal)
 26 */
 27public class Position {
 28    /** Flag for indicating invalid Selection position. */
 29    public static final int INVALID_POSITION = BreakIterator.DONE;
 30
 31    /** Begin index of Selection. */
 32    public int mStart;
 33
 34    /** End index of Selection. */
 35    public int mEnd;
 36
 37    /** Flag for checking selection mode is turned on. */
 38    public boolean mSelected;
 39
 40    /** Maximum number of cached objects in pool. */
 41    private static final int MAX_POOL_SIZE = 2;
 42
 43    /** Lock object. */
 44    private static final Object mPoolLock = new Object();
 45
 46    /** Points to first object in the pool. */
 47    private static Position sPool;
 48
 49    /** Current size of the pool. */
 50    private static int sPoolSize;
 51
 52    /** Next object in the pool. */
 53    private Position mNext;
 54
 55    /** Flag for checking if current object is in the pool. */
 56    private boolean mIsInPool;
 57
 58    /**
 59     * Hide constructor.
 60     */
 61    private Position() {
 62        clear();
 63    }
 64
 65    /**
 66     * Sets Selection positions and selection mode.
 67     */
 68    private void setPosition(int start, int end, boolean selected) {
 69        mStart = start;
 70        mEnd = end;
 71        mSelected = selected;
 72    }
 73
 74    /**
 75     * Returns a cached instance if such is available or a new one is instantiated with appropriate
 76     * arguments.
 77     *
 78     * @return An instance.
 79     */
 80    public static Position obtain(int start, int end, boolean selected) {
 81        Position position = Position.obtain();
 82        position.setPosition(start, end, selected);
 83        return position;
 84    }
 85
 86    /**
 87     * Returns a cached instance if such is available or a new one is instantiated.
 88     *
 89     * @return An instance.
 90     */
 91    public static Position obtain() {
 92        synchronized (mPoolLock) {
 93            if (sPool != null) {
 94                Position position = sPool;
 95                sPool = sPool.mNext;
 96                sPoolSize--;
 97                position.mNext = null;
 98                position.mIsInPool = false;
 99                return position;
100            }
101            return new Position();
102        }
103    }
104
105    /**
106     * Return an instance back to be reused.
107     * <p>
108     * <b>Note: You must not touch the object after calling this function.</b>
109     */
110    public void recycle() {
111        if (mIsInPool) {
112            return;
113        }
114
115        clear();
116        synchronized (mPoolLock) {
117            if (sPoolSize <= MAX_POOL_SIZE) {
118                mNext = sPool;
119                sPool = this;
120                mIsInPool = true;
121                sPoolSize++;
122            }
123        }
124    }
125
126    /**
127     * Clears the state of this instance.
128     */
129    private void clear() {
130        mStart = INVALID_POSITION;
131        mEnd = INVALID_POSITION;
132        mSelected = false;
133    }
134    
135    @Override
136    public String toString() {
137        return "(" + mStart + ", " + mEnd + ") with selected = " + mSelected;
138    }
139}