KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > util > SequenceValidator


1 /**
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright
3  * notice. All rights reserved.
4  */

5 package com.tc.util;
6
7 import com.tc.logging.TCLogger;
8 import com.tc.logging.TCLogging;
9
10 import java.util.Collection JavaDoc;
11 import java.util.HashMap JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.SortedSet JavaDoc;
14 import java.util.TreeSet JavaDoc;
15
16 public class SequenceValidator {
17
18   private static final TCLogger logger = TCLogging.getLogger(SequenceValidator.class);
19
20   private final Map JavaDoc sequences = new HashMap JavaDoc();
21   private final long start;
22
23   public SequenceValidator(long start) {
24     this.start = start;
25   }
26
27   // Used in tests
28
public synchronized boolean isNext(Object JavaDoc key, SequenceID candidate) {
29     if (candidate.isNull()) return true;
30     Sequencer sequencer = getOrCreate(key);
31     return sequencer.isNext(candidate);
32   }
33
34   public synchronized void setCurrent(Object JavaDoc key, SequenceID next) throws InvalidSequenceIDException {
35     if (key == null || SequenceID.NULL_ID.equals(next)) return;
36     Sequencer s = getOrCreate(key);
37     s.setCurrent(next);
38   }
39
40   // Used in tests
41
public synchronized SequenceID getCurrent(Object JavaDoc key) {
42     Sequencer s = (Sequencer) sequences.get(key);
43     Assert.assertNotNull(s);
44     return s.getCurrent();
45
46   }
47
48   public synchronized void initSequence(Object JavaDoc key, Collection JavaDoc sequenceIDs) {
49     Assert.assertFalse(sequences.containsKey(key));
50     sequences.put(key, new Sequencer(key, start, sequenceIDs));
51   }
52
53   public synchronized void remove(Object JavaDoc key) {
54     sequences.remove(key);
55   }
56
57   public synchronized int size() {
58     return sequences.size();
59   }
60
61   private Sequencer getOrCreate(Object JavaDoc key) {
62     Sequencer sequencer = (Sequencer) sequences.get(key);
63     if (sequencer == null) {
64       sequencer = new Sequencer(key, start);
65       sequences.put(key, sequencer);
66     }
67     return sequencer;
68   }
69
70   private static class Sequencer {
71
72     SortedSet JavaDoc sequenceIDs;
73     SequenceID current;
74
75     Sequencer(Object JavaDoc key, long start, Collection JavaDoc sequenceIDs) {
76       if (sequenceIDs.size() > 0) {
77         this.sequenceIDs = new TreeSet JavaDoc(SequenceID.COMPARATOR);
78         this.sequenceIDs.addAll(sequenceIDs);
79         current = new SequenceID(start);
80       } else {
81         throw new AssertionError JavaDoc("Sequencer should be set to a valid SequenceID Sequence !!!");
82       }
83       logger.info("Setting initial Sequence IDs for " + key + " current = " + current + " next = " + this.sequenceIDs);
84     }
85
86     Sequencer(Object JavaDoc key, long start) {
87       current = new SequenceID(start);
88       logger.debug("Setting initial Sequence IDs for " + key + " current = " + current);
89     }
90
91     public boolean isNext(SequenceID candidate) {
92       if (candidate.toLong() <= current.toLong()) {
93         logger.warn("Sequence IDs = " + sequenceIDs + " current = " + current + " but candidate = " + candidate);
94         return false;
95       }
96       if (sequenceIDs == null) {
97         return current.toLong() + 1 == candidate.toLong();
98       } else {
99         return (((SequenceID) sequenceIDs.first()).toLong() == candidate.toLong());
100       }
101     }
102
103     public void setCurrent(SequenceID next) throws InvalidSequenceIDException {
104       if (!isNext(next)) { throw new InvalidSequenceIDException("Trying to set to " + next + " but current = "
105                                                                 + current); }
106       if (sequenceIDs != null) {
107         logger.info("Setting current Sequence IDs from current = " + current + " to next = " + next);
108         sequenceIDs.headSet(next.next()).clear();
109         if (sequenceIDs.size() == 0) {
110           sequenceIDs = null;
111         }
112       }
113       current = next;
114     }
115
116     public SequenceID getCurrent() {
117       return current;
118     }
119   }
120 }
121
Popular Tags