Skip to main content
summaryrefslogtreecommitdiffstats
blob: cc165992e0a66217af7bf40b392e739d07587cad (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/***************************************************************************
 * Copyright (c) 2004 - 2008 Eike Stepper, Germany.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *    Eike Stepper - initial API and implementation
 **************************************************************************/
package org.eclipse.net4j.util.tests.cache;

import org.eclipse.net4j.util.cache.Cache;

import java.lang.ref.Reference;
import java.lang.ref.ReferenceQueue;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author Eike Stepper
 */
public class RevisionManager extends Cache<Revision>
{
  private Map<Integer, TimeLine> timeLines = new HashMap<Integer, TimeLine>();

  public RevisionManager()
  {
  }

  public Revision getRevision(int id, int version)
  {
    TimeLine timeLine = getTimeLine(id);
    return timeLine.getRevision(version);
  }

  public void evictElements(int elementCount)
  {
  }

  protected TimeLine getTimeLine(int id)
  {
    TimeLine timeLine = timeLines.get(id);
    if (timeLine == null)
    {
      timeLine = new TimeLine(id);
      timeLines.put(id, timeLine);
    }

    return timeLine;
  }

  protected Revision loadRevision(int id, int version)
  {
    Revision revision = new Revision(this, id, version);
    return revision;
  }

  protected void finalizeRevision(Revision revision)
  {
    // TimeLine timeLine = getTimeLine(revision.getID());
    // timeLine.addRevision(revision);
  }

  @Override
  protected void unreachableElement(Reference<? extends Revision> reference)
  {
  }

  /**
   * @author Eike Stepper
   */
  private class TimeLine
  {
    private int id;

    private List<Reference<Revision>> revisions = new ArrayList<Reference<Revision>>();

    public TimeLine(int id)
    {
      this.id = id;
    }

    public int getID()
    {
      return id;
    }

    public Revision getRevision(int version)
    {
      for (Iterator<Reference<Revision>> it = revisions.iterator(); it.hasNext();)
      {
        Reference<Revision> reference = it.next();
        Revision revision = reference.get();
        if (revision != null)
        {
          if (revision.getVersion() == version)
          {
            return revision;
          }
        }
        else
        {
          it.remove();
          break;
        }
      }

      long time = System.currentTimeMillis();
      Revision revision = loadRevision(id, version);
      time = System.currentTimeMillis() - time;

      addRevision(revision);
      return revision;
    }

    public void addRevision(Revision revision)
    {
      revisions.add(new CacheElement(revision, getReferenceQueue()));
    }
  }

  /**
   * @author Eike Stepper
   */
  private static final class CacheElement extends SoftReference<Revision>
  {
    private int id;

    private int version;

    // private Reference<Revision> ref;

    public CacheElement(Revision revision, ReferenceQueue<Revision> queue)
    {
      super(revision, queue);
      // ref = new WeakReference<Revision>(revision, queue);
      id = revision.getID();
      version = revision.getVersion();
    }

    public int getID()
    {
      return id;
    }

    public int getVersion()
    {
      return version;
    }

    @Override
    public String toString()
    {
      return "R" + id + "v" + version + (get() == null ? "" : " UNCLEARED");
    }
  }
}

Back to the top