| 162 | } |
| 163 | |
| 164 | private String getFormat(long time) { |
| 165 | |
| 166 | long seconds = time / 1000; |
| 167 | |
| 168 | /* |
| 169 | * First step: if we have seen this timestamp during the previous call, return the previous value. |
| 170 | */ |
| 171 | if (seconds == previousSeconds) { |
| 172 | return previousFormat; |
| 173 | } |
| 174 | |
| 175 | /* Second step: Try to locate in cache */ |
| 176 | previousSeconds = seconds; |
| 177 | int index = (offset + (int) (seconds - first)) % cacheSize; |
| 178 | if (index < 0) { |
| 179 | index += cacheSize; |
| 180 | } |
| 181 | if (seconds >= first && seconds <= last) { |
| 182 | if (cache[index] != null) { |
| 183 | /* Found, so remember for next call and return. */ |
| 184 | previousFormat = cache[index]; |
| 185 | return previousFormat; |
| 186 | } |
| 187 | |
| 188 | /* Third step: not found in cache, adjust cache and add item */ |
| 189 | } else if (seconds >= last + cacheSize || seconds <= first - cacheSize) { |
| 190 | first = seconds; |
| 191 | last = first + cacheSize - 1; |
| 192 | index = 0; |
| 193 | offset = 0; |
| 194 | for (int i = 1; i < cacheSize; i++) { |
| 195 | cache[i] = null; |
| 196 | } |
| 197 | } else if (seconds > last) { |
| 198 | for (int i = 1; i < seconds - last; i++) { |
| 199 | cache[(index + cacheSize - i) % cacheSize] = null; |
| 200 | } |
| 201 | first = seconds - (cacheSize - 1); |
| 202 | last = seconds; |
| 203 | offset = (index + 1) % cacheSize; |
| 204 | } else { |
| 205 | for (int i = 1; i < first - seconds; i++) { |
| 206 | cache[(index + i) % cacheSize] = null; |
| 207 | } |
| 208 | first = seconds; |
| 209 | last = seconds + (cacheSize - 1); |
| 210 | offset = index; |
| 211 | } |
| 212 | |
| 213 | /* |
| 214 | * Last step: format new timestamp either using parent cache or locally. |
| 215 | */ |
| 216 | if (parent != null) { |
| 217 | synchronized (parent) { |
| 218 | previousFormat = parent.getFormat(time); |
| 219 | } |
| 220 | } else { |
| 221 | currentDate.setTime(time); |