KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > resources > MarkerDeltaManager


1 /*******************************************************************************
2  * Copyright (c) 2003, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM - Initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.core.internal.resources;
12
13 import java.util.*;
14
15 /**
16  * The notification mechanism can request marker deltas for several overlapping intervals
17  * of time. This class maintains a history of marker deltas, and upon request can
18  * generate a map of marker deltas for any interval. This is done by maintaining
19  * batches of marker deltas keyed by the change Id at the start of that batch.
20  * When the delta factory requests a delta, it specifies the start generation, and
21  * this class assembles the deltas for all generations between then and the most
22  * recent delta.
23  */

24 class MarkerDeltaManager {
25     private static final int DEFAULT_SIZE = 10;
26     private long[] startIds = new long[DEFAULT_SIZE];
27     private Map[] batches = new Map[DEFAULT_SIZE];
28     private int nextFree = 0;
29
30     /**
31      * Returns the deltas from the given start id up until the present. Returns null
32      * if there are no deltas for that interval.
33      */

34     protected Map assembleDeltas(long start) {
35         Map result = null;
36         for (int i = 0; i < nextFree; i++)
37             if (startIds[i] >= start)
38                 result = MarkerDelta.merge(result, batches[i]);
39         return result;
40     }
41
42     /**
43      * Flushes all delta batches up to but not including the given start Id.
44      */

45     protected void resetDeltas(long startId) {
46         //find offset of first batch to keep
47
int startOffset = 0;
48         for (; startOffset < nextFree; startOffset++)
49             if (startIds[startOffset] >= startId)
50                 break;
51         if (startOffset == 0)
52             return;
53         long[] newIds = startIds;
54         Map[] newBatches = batches;
55         //shrink the arrays if it has grown too large
56
if (startIds.length > DEFAULT_SIZE && (nextFree - startOffset < DEFAULT_SIZE)) {
57             newIds = new long[DEFAULT_SIZE];
58             newBatches = new Map[DEFAULT_SIZE];
59         }
60         //copy and compact into the new array
61
int remaining = nextFree - startOffset;
62         System.arraycopy(startIds, startOffset, newIds, 0, remaining);
63         System.arraycopy(batches, startOffset, newBatches, 0, remaining);
64         //clear the end of the array
65
Arrays.fill(startIds, remaining, startIds.length, 0);
66         Arrays.fill(batches, remaining, startIds.length, null);
67         startIds = newIds;
68         batches = newBatches;
69         nextFree = remaining;
70     }
71
72     protected Map newGeneration(long start) {
73         int len = startIds.length;
74         if (nextFree >= len) {
75             long[] newIds = new long[len * 2];
76             Map[] newBatches = new Map[len * 2];
77             System.arraycopy(startIds, 0, newIds, 0, len);
78             System.arraycopy(batches, 0, newBatches, 0, len);
79             startIds = newIds;
80             batches = newBatches;
81         }
82         startIds[nextFree] = start;
83         batches[nextFree] = new HashMap(11);
84         return batches[nextFree++];
85     }
86 }
87
Popular Tags