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