1
2
3
4
5
6 package net.kwfgrid.gworkflowdl.protocol.server;
7
8 import net.kwfgrid.gworkflowdl.protocol.Protocol;
9
10 import java.util.*;
11
12 public class ModificationBuffer {
13 public static final int DEFAULT_SIZE = 200;
14
15 private int _offset;
16 private LinkedList _buffer;
17 private int _size;
18
19 /***
20 Creates a new modification buffer with default size.
21 @param initialversion The initial version number.
22 */
23 public ModificationBuffer(int initialversion) {
24 this (initialversion, DEFAULT_SIZE);
25 }
26
27 /***
28 Creates a new modification buffer with the specified size.
29 @param initialversion The initial version number.
30 @param size The maximum number of modifications in this buffer.
31 */
32 public ModificationBuffer(int initialversion, int size) {
33 _size = size;
34 _offset = initialversion;
35 _buffer = new LinkedList();
36 }
37
38 public void setInitialVersion(int version) throws IllegalStateException {
39 if (_buffer.size() > 0)
40 throw new IllegalStateException("Buffer is not empty. Can not set initial version.");
41
42 _offset = version;
43 }
44
45 /***
46 Write a modification to the buffer.
47 @param versionnumber The number of the version produced by the specified modification.
48 @param modification The modification.
49 @exception IndexOutOfBoundsException If the version number is not the successor of the last version number writte to this buffer.
50 */
51 public void write(int versionnumber, String modification) throws IndexOutOfBoundsException {
52 if (versionnumber > _offset + _buffer.size() + 1)
53 throw new IndexOutOfBoundsException("Missing version interval ["+(_offset + 1)+", "+(versionnumber - 1)+"].");
54
55 if (versionnumber < _offset + _buffer.size() + 1)
56 throw new IndexOutOfBoundsException("Version "+versionnumber+" already reached.");
57
58 _buffer.addLast(new String[] { ""+versionnumber, Protocol.IDENTIFIER_MODIFICATION, modification });
59
60 if (_buffer.size() > _size) {
61 _buffer.removeFirst();
62 _offset++;
63 }
64 }
65
66 /***
67 Read the modifications from the buffer which are necessary to get from the specified version to the current version.
68 @param fromversion The version of the structure which shall be updated to the current version.
69 @return The interval of modifications necessary to update the structure.
70 @exception IndexOutOfBoundsException If the specified version is to old and not all necessary modifications are buffered in this buffer;
71 if the specified version is newer than the current version of the structure which uses this buffer.
72 */
73 public String[][] read(int fromversion) throws IndexOutOfBoundsException {
74 if (fromversion < firstHandleableVersion())
75 throw new IndexOutOfBoundsException("No information about version interval ["+(fromversion + 1)+", "+(_offset - 1)+"].");
76
77 if (fromversion > lastHandleableVersion())
78 throw new IndexOutOfBoundsException("The specified version has not yet been reached ("+(_offset+_buffer.size()-1)+" < "+fromversion+").");
79
80 if (fromversion == lastHandleableVersion())
81 return new String[0][3];
82
83 int count = _buffer.size() - (fromversion - _offset);
84 List sub = _buffer.subList(fromversion - _offset, _buffer.size());
85
86 return (String[][])sub.toArray(new String[count][3]);
87 }
88
89 /***
90 Get the smallest version number suitable to call <code>read</code>-method of this buffer without <codeIndexOutOfBoundsException</code>.
91 */
92 public int firstHandleableVersion() {
93 return _offset;
94 }
95
96 /***
97 Get the greatest version number suitable to call <code>read</code>-method of this buffer without <codeIndexOutOfBoundsException</code>.
98 */
99 public int lastHandleableVersion() {
100 return _buffer.size() + _offset;
101 }
102 }