KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > ristretto > imap > SequenceEntry


1 /* ***** BEGIN LICENSE BLOCK *****
2  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
3  *
4  * The contents of this file are subject to the Mozilla Public License Version
5  * 1.1 (the "License"); you may not use this file except in compliance with
6  * the License. You may obtain a copy of the License at
7  * http://www.mozilla.org/MPL/
8  *
9  * Software distributed under the License is distributed on an "AS IS" basis,
10  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
11  * for the specific language governing rights and limitations under the
12  * License.
13  *
14  * The Original Code is Ristretto Mail API.
15  *
16  * The Initial Developers of the Original Code are
17  * Timo Stich and Frederik Dietz.
18  * Portions created by the Initial Developers are Copyright (C) 2004
19  * All Rights Reserved.
20  *
21  * Contributor(s):
22  *
23  * Alternatively, the contents of this file may be used under the terms of
24  * either the GNU General Public License Version 2 or later (the "GPL"), or
25  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
26  * in which case the provisions of the GPL or the LGPL are applicable instead
27  * of those above. If you wish to allow use of your version of this file only
28  * under the terms of either the GPL or the LGPL, and not to allow others to
29  * use your version of this file under the terms of the MPL, indicate your
30  * decision by deleting the provisions above and replace them with the notice
31  * and other provisions required by the GPL or the LGPL. If you do not delete
32  * the provisions above, a recipient may use your version of this file under
33  * the terms of any one of the MPL, the GPL or the LGPL.
34  *
35  * ***** END LICENSE BLOCK ***** */

36 package org.columba.ristretto.imap;
37
38 /**
39  * A part of the sequence set.
40  *
41  * @author tstich
42  */

43 public class SequenceEntry implements Comparable JavaDoc {
44     /**
45      * Entry type.
46      */

47     public static final int SINGLE = 0;
48     /**
49      * Entry type.
50      */

51     public static final int RANGE = 1;
52     /**
53      * Entry type.
54      */

55     public static final int OPEN_RANGE = 2;
56     /**
57      * Entry type.
58      */

59     public static final int ALL = 3;
60     
61     /**
62      * Entry type.
63      */

64     public static final int STAR = -1;
65     
66     
67     //NOTE: for the types SINGLE and OPEN_RANGE
68
//"a" and "b" must have the same value !
69
private int a;
70     private int b;
71     private int type;
72     
73     /**
74      * Constructs the SequenceEntry.
75      * @param a
76      */

77     public SequenceEntry(int a) {
78         this.a = a;
79         this.b = a;
80         type = SINGLE;
81     }
82     
83     /**
84      * Constructs the SequenceEntry.
85      *
86      * @param a
87      * @param b
88      */

89     public SequenceEntry(int a, int b) {
90         if( a == b ) {
91             this.a = a;
92             this.b = a;
93             type = SINGLE;
94             
95             return;
96         }
97         
98         if( a == STAR || b == STAR) {
99             type = OPEN_RANGE;
100             this.a = Math.max(a,b);
101             this.b = this.a;
102             
103             if( this.a == 1) {
104                 type = ALL;
105             }
106         } else {
107             this.a = Math.min(a,b);
108             this.b = Math.max(a,b);
109             
110             type = RANGE;
111         }
112     }
113     
114     /**
115      * Check if this can be merged in one.
116      *
117      * @param s
118      * @return <code>true</code> if the SequenceEntries can be merged.
119      */

120     public boolean canMergeWith(SequenceEntry s ) {
121         if( s.type == ALL || this.type == ALL ) return true;
122         
123         // Handle open ranges
124
if( this.type == OPEN_RANGE && (s.b >= this.b || s.b == STAR)) return true;
125         if( s.type == OPEN_RANGE && (this.b >= s.b || this.b == STAR)) return true;
126         
127         // otherwise if there is a MAX/STAR in one of the ranges
128
// merging is not possible
129
if( s.b == STAR || this.b == STAR ) return false;
130         
131         // included/overlaps/connects with in Range
132
if( this.type == RANGE && ( this.a <= s.a && this.b >= s.a-1 )) return true;
133         if( this.type == RANGE && ( this.a <= s.b-1 && this.b >= s.b )) return true;
134         if( s.type == RANGE && ( s.a <= this.a && s.b >= this.a-1 )) return true;
135         if( s.type == RANGE && ( s.a <= this.b-1 && s.b >= this.b )) return true;
136         
137         return (this.a - s.a) * (this.a - s.a) <= 1 || (this.b - s.b) * (this.b - s.b) <= 1;
138     }
139     
140     /**
141      * Merge the given Sequencentry with this entry. There is
142      * no further check if this is possible so use #canMergeWith(SequenceSet)
143      * to test if it is possible!
144      *
145      * @param s
146      */

147     public void merge(SequenceEntry s) {
148         if( s.type == ALL || this.type == ALL ) {
149             this.type = ALL;
150             return;
151         }
152         
153         if( this.type == OPEN_RANGE && s.b >= this.a - 1) {
154             this.type = OPEN_RANGE;
155             this.a = this.b = Math.min(s.a,this.a);
156             if( this.a == 1) type = ALL;
157         }
158         
159         if( s.type == OPEN_RANGE && this.b >= s.a - 1) {
160             this.type = OPEN_RANGE;
161             this.a = this.b = Math.min(s.a,this.a);
162             if( this.a == 1) type = ALL;
163             return;
164         }
165         
166         if( this.type == RANGE && ( this.a <= s.a || this.b >= s.b )) {
167             this.a = s.a<this.a?s.a:this.a;
168             this.b = s.b>this.b?s.b:this.b;
169             return;
170         }
171
172         if( s.type == RANGE && ( s.a <= this.a || s.b >= this.b )) {
173             this.type = RANGE;
174             this.a = s.a<this.a?s.a:this.a;
175             this.b = s.b>this.b?s.b:this.b;
176             return;
177         }
178         
179         this.type = RANGE;
180         this.a = s.a<this.a?s.a:this.a;
181         this.b = s.b>this.b?s.b:this.b;
182     }
183     
184     /**
185      * @see java.lang.Comparable#compareTo(java.lang.Object)
186      */

187     public int compareTo(Object JavaDoc o) {
188         SequenceEntry s = (SequenceEntry) o;
189         
190         // ALL is the smallest value
191
// this is better for pack
192
if( type == ALL ) return -1;
193         if( s.type == ALL ) return 1;
194         
195         // Sort open ranges to the borders
196
//if( type == LEFT_OPEN_RANGE || s.type == RIGHT_OPEN_RANGE) return -1;
197
//if( type == RIGHT_OPEN_RANGE || s.type == LEFT_OPEN_RANGE) return 1;
198

199         // now both types can only be RANGE or SINGLE
200
// check if one is a STAR
201
if( s.a == STAR ) return -1;
202         if( a == STAR ) return 1;
203         
204         // -> we only have to deal with "a"
205

206         return a<s.a?-1:1;
207     }
208     /**
209      * @see java.lang.Object#toString()
210      */

211     public String JavaDoc toString() {
212         switch( type ) {
213             case ALL : return "1:*";
214             
215             case SINGLE : {
216                 if( a == STAR ) {
217                     return "*";
218                 } else {
219                     return Integer.toString(a);
220                 }
221             }
222             
223             case RANGE : return Integer.toString(a) + ':' + Integer.toString(b);
224             
225             case OPEN_RANGE : return Integer.toString(a) + ":*";
226         }
227         
228         return null;
229     }
230     /**
231      * @return Returns the a.
232      */

233     public int getA() {
234         return a;
235     }
236     /**
237      * @return Returns the b.
238      */

239     public int getB() {
240         return b;
241     }
242     /**
243      * @return Returns the type.
244      */

245     public int getType() {
246         return type;
247     }
248 }
249
Popular Tags