Skip to content

Package: Buffer

Buffer

nameinstructionbranchcomplexitylinemethod
Buffer(Integer)
M: 0 C: 32
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 7
100%
M: 0 C: 1
100%
peek()
M: 6 C: 19
76%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 6
67%
M: 0 C: 1
100%
put(Object)
M: 6 C: 19
76%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 7
70%
M: 0 C: 1
100%
remove()
M: 6 C: 19
76%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 3 C: 6
67%
M: 0 C: 1
100%
toString()
M: 28 C: 0
0%
M: 2 C: 0
0%
M: 2 C: 0
0%
M: 5 C: 0
0%
M: 1 C: 0
0%

Coverage

1: package basic;
2:
3: import java.util.ArrayList;
4: import java.util.List;
5:
6: import scanner.ScannerConstants;
7:
8: /**
9: * This class represents a Buffer and will be used to send the data from scanner to parser.
10: *
11: * @author HFW410
12: *
13: * @param <T>
14: */
15: public class Buffer<T> {
16:         /**
17:          * This attribute is the list of data.
18:          */
19:         private final List<T> data;
20:         /**
21:          * queue if the buffer is empty.
22:          */
23:         private final Semaphor usedSpace;
24:         /**
25:          * queue if the buffer is full.
26:          */
27:         private final Semaphor freeSpace;
28:         /**
29:          * lock and unlock the semaphore.
30:          */
31:         private final Semaphor mutex;
32:
33:         /**
34:          * This is the capacity for the buffer.
35:          */
36:         private final Integer capacity;
37:
38:         /**
39:          * This is the constructor. It initialize a new buffer with the capacity @param capacity No
40:          * side-effects.
41:          *
42:          * @param capacity
43:          * The capacity of the buffer.
44:          */
45:         public Buffer(final Integer capacity) {
46:                 super();
47:                 this.data = new ArrayList<T>();
48:                 this.capacity = capacity;
49:                 this.usedSpace = new Semaphor(0);
50:                 this.freeSpace = new Semaphor(this.capacity);
51:                 this.mutex = new Semaphor(1);
52:         }
53:
54:         /**
55:          *
56:          * This method put a new object @param object into the buffer.
57:          *
58:          * @param object
59:          * The object to put into the buffer.
60:          * @throws InterruptedException
61:          * if mutex.down does not work
62:          *
63:          */
64:         public void put(final T object) throws InterruptedException {
65:                 this.freeSpace.down();
66:                 try {
67:                         this.mutex.down();
68:                         this.data.add(object);
69:                         this.mutex.up();
70:                         this.usedSpace.up();
71:                 } catch (final InterruptedException ie) {
72:                         this.freeSpace.up();
73:                         throw ie;
74:
75:                 }
76:         }
77:
78:         /**
79:          * removes the first element of the buffer.
80:          *
81:          * @return the removed data
82:          * @throws InterruptedException
83:          * if mutex.down() did not work
84:          */
85:         public T remove() throws InterruptedException {
86:                 this.usedSpace.down();
87:                 try {
88:                         this.mutex.down();
89:                         final T result = this.data.remove(0);
90:                         this.mutex.up();
91:                         this.freeSpace.up();
92:                         return result;
93:                 } catch (final InterruptedException ie) {
94:                         this.usedSpace.up();
95:                         throw ie;
96:                 }
97:         }
98:
99:         /**
100:          *
101:          * @return the first element
102:          * @throws InterruptedException
103:          * if mutex.down did not work
104:          */
105:         public T peek() throws InterruptedException {
106:                 this.usedSpace.down();
107:                 try {
108:                         this.mutex.down();
109:                         final T result = this.data.get(0);
110:                         this.mutex.up();
111:                         this.usedSpace.up();
112:                         return result;
113:                 } catch (final InterruptedException ie) {
114:                         this.usedSpace.up();
115:                         throw ie;
116:                 }
117:         }
118:
119:         @Override
120:         public String toString() {
121:                 final StringBuffer result = new StringBuffer(ScannerConstants.BUFFER);
122:•                for (int i = 0; i < this.data.size(); i++) {
123:                         result.append(this.data.get(i));
124:                         result.append(ScannerConstants.COMMASPACE);
125:                 }
126:                 return result.toString();
127:         }
128: }