00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 package org.objectweb.proactive.core.body.ft.message;
00032
00033 import java.io.Serializable;
00034 import java.util.Iterator;
00035 import java.util.List;
00036 import java.util.Vector;
00037
00038 import org.objectweb.proactive.core.body.ft.exception.ProtocolErrorException;
00039
00040
00046 public class ReceptionHistory implements Serializable {
00047
00048 private List elements;
00049
00050
00051 private long lastCommited;
00052
00053
00054 private long base;
00055
00056
00057
00058 private long lastRecoverable;
00059
00063 public ReceptionHistory() {
00064 this.elements = new Vector();
00065 this.lastCommited = -1;
00066 this.lastRecoverable = -1;
00067 this.base = 0;
00068 }
00069
00074 public void updateHistory(HistoryUpdater hu) {
00075 long toAddBase = hu.base;
00076 long toAddLast = hu.last;
00077 List toAdd = hu.elements;
00078
00079
00080
00081
00082 if (toAddBase > (this.lastCommited + 1)) {
00083
00084 this.elements = toAdd;
00085 this.base = toAddBase;
00086 this.lastCommited = toAddLast;
00087 } else if (this.lastCommited < toAddLast) {
00088
00089 Iterator it = toAdd.iterator();
00090
00091
00092 for (long i = toAddBase; i <= this.lastCommited; i++) {
00093 it.next();
00094 }
00095
00096
00097 while (it.hasNext()) {
00098 this.elements.add(it.next());
00099 }
00100 this.lastCommited = toAddLast;
00101 }
00102 }
00103
00110 public void goToNextBase(long nextBase) {
00111 if (nextBase < this.base) {
00112 throw new ProtocolErrorException(
00113 "nextBase is lower than current base !");
00114 }
00115 int shift = (int) (nextBase - this.base);
00116
00117
00118
00119 if (shift == (this.elements.size() + 1)) {
00120 this.elements.clear();
00121 this.base = nextBase;
00122 } else {
00123 this.elements.subList(0, shift).clear();
00124 this.base = nextBase;
00125 }
00126 }
00127
00132 public void confirmLastUpdate() {
00133 this.lastRecoverable = this.lastCommited;
00134 }
00135
00136 public long getLastCommited() {
00137 return this.lastCommited;
00138 }
00139
00140 public long getLastRecoverable() {
00141 return this.lastRecoverable;
00142 }
00143
00149 public List getRecoverableHistory() {
00150 if (this.lastCommited == this.lastRecoverable) {
00151 return this.elements;
00152 } else {
00153 Vector toRet = new Vector();
00154 for (int i = 0; i <= (this.lastRecoverable - this.base); i++) {
00155 toRet.add(this.elements.get(i));
00156 }
00157 this.elements = toRet;
00158 return toRet;
00159 }
00160 }
00161
00165 public void compactHistory() {
00166 if (this.lastCommited > this.lastRecoverable) {
00167 this.elements.subList((int) ((this.lastRecoverable + 1) -
00168 this.base), (int) ((this.lastCommited + 1) - this.base));
00169 this.lastCommited = this.lastRecoverable;
00170 }
00171 }
00172 }