View Javadoc

1   /*
2    * Copyright (c) 2004-2007 Creative Sphere Limited.
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *
10   *   Creative Sphere - initial API and implementation
11   *
12   */
13  package org.abstracthorizon.mercury.imap.util;
14  
15  import java.util.ArrayList;
16  import java.util.Collections;
17  import java.util.Iterator;
18  import java.util.List;
19  
20  /**
21   * Composed sequence - an ordered sequence of sequences.
22   *
23   * @author Daniel Sendula
24   */
25  public class ComposedSequence implements Sequence {
26  
27      /** Sequences */
28      protected List<Sequence> sequences = new ArrayList<Sequence>();
29  
30      /** Current pointer */
31      protected int ptr = 0;
32  
33      /**
34       * Constructor
35       */
36      public ComposedSequence() {
37      }
38  
39      /**
40       * Adds new sequence to composed sequence
41       * @param s sequence to be added
42       */
43      public void add(Sequence s) {
44          if (!sequences.contains(s)) {
45              sequences.add(s);
46          }
47          Collections.sort(sequences);
48      }
49  
50      /**
51       * Removes sequence from list of sequences
52       * @param s sequence to be removed
53       */
54      public void remove(Sequence s) {
55          sequences.remove(s);
56      }
57  
58      /**
59       * Clears list of sequences
60       */
61      public void clear() {
62          sequences.clear();
63      }
64  
65      /**
66       * Returns <code>true</code> if sequence exists
67       * @param s sequence
68       * @return <code>true</code> if sequence exists
69       */
70      public boolean contains(Sequence s) {
71          return sequences.contains(s);
72      }
73  
74      /**
75       * Returns minimum number from the sequence
76       *
77       * @return minimum number from the sequence
78       */
79      public int getMin() {
80          if (sequences.size() > 0) {
81              Sequence s = (Sequence) sequences.get(0);
82              return s.getMin();
83          }
84          return 0;
85      }
86  
87      /**
88       * Returns maximum number from the sequence
89       *
90       * @return maximum number from the sequence
91       */
92      public int getMax() {
93          if (sequences.size() > 0) {
94              Sequence s = (Sequence) sequences.get(sequences.size() - 1);
95              return s.getMax();
96          }
97          return 0;
98      }
99  
100     /**
101      * Sets the lower limit in the sequence removing subsequences below that point
102      * @param lower lower limit
103      */
104     public void setLowerLimit(int lower) {
105         int i = 0;
106         boolean ok = true;
107         while ((i < sequences.size()) && ok) {
108             Sequence s = (Sequence) sequences.get(i);
109             int min = s.getMin();
110             int max = s.getMax();
111             if (lower > max) {
112                 sequences.remove(i);
113             } else if (lower > min) {
114                 s.setLowerLimit(lower);
115                 i = i + 1;
116             } else if (lower <= min) {
117                 ok = false;
118             }
119         } // while
120     }
121 
122     /**
123      * Sets the upper limit in the sequence removing subsequences above that point
124      * @param lower upper limit
125      */
126     public void setUpperLimit(int upper) {
127         int i = sequences.size();
128         boolean ok = true;
129         while ((i > 0) && ok) {
130             i = i - 1;
131             Sequence s = (Sequence) sequences.get(i);
132             int min = s.getMin();
133             int max = s.getMax();
134             if (upper < min) {
135                 sequences.remove(i);
136             } else if (upper < max) {
137                 s.setUpperLimit(upper);
138                 i = i - 1;
139             } else if (upper >= max) {
140                 ok = false;
141             }
142         } // while
143     }
144 
145     /**
146      * Sets pointer to first element in sequences
147      */
148     public void first() {
149         int ptr = 0;
150         if (sequences.size() > 0) {
151             ((Sequence) sequences.get(ptr)).first();
152         }
153     }
154 
155     /**
156      * Returns <code>true</code> if there are more elements in the sequence
157      * @return <code>true</code> if there are more elements in the sequence
158      */
159     public boolean more() {
160         if (ptr == sequences.size()) {
161             return false;
162         }
163         Sequence s = (Sequence) sequences.get(ptr);
164         if (s.more()) {
165             return true;
166         }
167 
168         ptr = ptr + 1;
169         if (ptr == sequences.size()) {
170             return false;
171         }
172         ((Sequence) sequences.get(ptr)).first();
173         return true;
174     }
175 
176     /**
177      * Returns next element from the sequence
178      * @return next element from the sequence
179      */
180     public int next() {
181         if (ptr == sequences.size()) {
182             return 0;
183         }
184         Sequence s = (Sequence) sequences.get(ptr);
185         return s.next();
186     }
187 
188     /**
189      * Returns sequences list iterator
190      * @return sequences list iterator
191      */
192     public Iterator<Sequence> getSequencesAsIterator() {
193         return sequences.iterator();
194     }
195 
196     /**
197      * Creates a string representing all sequences
198      * @return a string representing all sequences
199      */
200     public String toString() {
201         StringBuffer res = new StringBuffer();
202         if (sequences.size() > 0) {
203             for (int i = 0; i < sequences.size(); i++) {
204                 if (i > 0) {
205                     res.append(',');
206                 }
207                 res.append(sequences.get(i).toString());
208             }
209         }
210         return res.toString();
211     }
212 
213     /**
214      * Compares two sequences returning -1, 0 or 1
215      * @param other other sequence
216      * @return -1, 0 or 1
217      */
218     public int compareTo(Sequence other) {
219         Sequence s = (Sequence) other;
220         int i = getMin() + getMax();
221         int t = s.getMax() + s.getMin();
222         if (i < t) {
223             return -1;
224         } else if (i > t) {
225             return 1;
226         } else {
227             return 0;
228         }
229     }
230 
231 }