1 package org.simantics.db.impl.query;
3 import java.util.ArrayList;
4 import java.util.Collection;
6 public class CacheCollectionResult {
7 public static final int LEVELS = 10;
9 private int position = -1;
10 private int currentLength = 0;
12 private ArrayList<CacheEntryBase> currentLevel;
13 public ArrayList<CacheEntryBase> levels[] = new ArrayList[LEVELS];
14 public CacheCollectionResult() {
15 for(int i=0;i<LEVELS;i++) {
16 levels[i] = new ArrayList<CacheEntryBase>();
19 public void add(CacheEntryBase entry) {
20 short level = entry.getLevel();
21 if(level < (LEVELS-1)) levels[level].add(entry);
22 else levels[(LEVELS-1)].add(entry);
25 public Collection<CacheEntryBase> toCollection() {
26 ArrayList<CacheEntryBase> result = new ArrayList<CacheEntryBase>(size());
27 for(int i=0;i<LEVELS;i++)
28 result.addAll(levels[i]);
34 public boolean isAtStart() {
35 return level == 0 && position == -1;
37 public void restart() {
40 currentLevel = levels[0];
41 currentLength = currentLevel.size();
43 public CacheEntryBase next(int maxLevel) {
44 //System.err.println("next " + position + " " + level);
46 if(position >= currentLength) {
47 if(level == (LEVELS-1)) return null;
48 if(level == maxLevel) return null;
51 currentLevel = levels[level];
52 currentLength = currentLevel.size();
53 return next(maxLevel);
55 return currentLevel.get(position);
57 public void remove() {
58 int lastPosition = currentLevel.size()-1;
59 CacheEntryBase last = currentLevel.remove(lastPosition);
60 if(lastPosition > position) {
61 currentLevel.set(position, last);
67 public void setLevel(CacheEntryBase current, int level) {
68 if(level == current.level) return;
69 current.level = (short)level;