This topic has not yet been written. The content below is from the topic description.
6.3.1. save_state, restore_state, and type Example 6.3. Method save_state public boolean save_state (OutputObjectState os, int ObjectType) { if (!super.save_state(os, ObjectType)) return false; try { os.packInt(numberOfElements); if (numberOfElements > 0) { for (int i = 0; i < numberOfElements; i++) os.packInt(elements[i]); } return true; } catch (IOException e) { return false; } } Example 6.4. Method restore_state public boolean restore_state (InputObjectState os, int ObjectType) { if (!super.restore_state(os, ObjectType)) return false; try { numberOfElements = os.unpackInt(); if (numberOfElements > 0) { for (int i = 0; i < numberOfElements; i++) elements[i] = os.unpackInt(); } return true; } catch (IOException e) { return false; } } Example 6.5. Method type Because the Queue class is derived from the LockManager class, the operation type should be: public String type () { return "/StateManager/LockManager/TransactionalQueue"; } 6.3.2. enqueue and dequeue methods If the operations of the queue class are to be coded as atomic actions, then the enqueue operation might have the structure given below. The dequeue operation is similarly structured, but is not implemented here. Example 6.6. Method enqueue public void enqueue (int v) throws OverFlow, UnderFlow, QueueError { AtomicAction A = new AtomicAction(); boolean res = false; try { A.begin(0); if (setlock(new Lock(LockMode.WRITE), 0) == LockResult.GRANTED) { if (numberOfElements < QUEUE_SIZE) { elements[numberOfElements] = v; numberOfElements++; res = true; } else { A.rollback(); throw new UnderFlow(); } } if (res) A.commit(true); else { A.rollback(); throw new Conflict(); } } catch (Exception e1) { throw new QueueError(); } } 6.3.3. queueSize method Example 6.7. Method queueSize public int queueSize () throws QueueError, Conflict { AtomicAction A = new AtomicAction(); int size = -1; try { A.begin(0); if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED) size = numberOfElements; if (size != -1) A.commit(true); else { A.rollback(); throw new Conflict(); } } catch (Exception e1) { throw new QueueError(); } return size; } 6.3.4. inspectValue and setValue methods Note The setValue method is not implemented here, but is similar in structure to Example 6.8, “Method inspectValue�. Example 6.8. Method inspectValue public int inspectValue (int index) throws UnderFlow, OverFlow, Conflict, QueueError { AtomicAction A = new AtomicAction(); boolean res = false; int val = -1; try { A.begin(); if (setlock(new Lock(LockMode.READ), 0) == LockResult.GRANTED) { if (index < 0) { A.rollback(); throw new UnderFlow(); } else { // array is 0 - numberOfElements -1 if (index > numberOfElements -1) { A.rollback(); throw new OverFlow(); } else { val = elements[index]; res = true; } } } if (res) A.commit(true); else { A.rollback(); throw new Conflict(); } } catch (Exception e1) { throw new QueueError(); } return val; }