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  /**
16   * A class representing simple implementation of Sequence. It has
17   * min and max, and maintains current pointer.
18   *
19   * @author Daniel Sendula
20   */
21  public class SimpleSequence implements Sequence {
22  
23      /** Default maximum value */
24      protected int min = Integer.MIN_VALUE;
25  
26      /** Default minimum value */
27      protected int max = Integer.MAX_VALUE;
28  
29      /** Current pointer */
30      protected int ptr = min;
31  
32      /**
33       * Constructor
34       */
35      public SimpleSequence() {
36      }
37  
38      /**
39       * Minimum element in the sequence
40       * @return minimum element in the sequence
41       */
42      public int getMin() {
43          return min;
44      }
45  
46      /**
47       * Sets minimum element in the sequence
48       * @param min minimum element in the sequence
49       */
50      public void setMin(int min) {
51          this.min = min;
52      }
53  
54      /**
55       * Maximum element in the sequence
56       * @return maximum element in the sequence
57       */
58      public int getMax() {
59          return max;
60      }
61  
62      /**
63       * Sets maximum element in the sequence
64       * @param max maximum element in the sequence
65       */
66      public void setMax(int max) {
67          this.max = max;
68      }
69  
70      /**
71       * Sets the lower limit
72       * @param lower lower limit
73       */
74      public void setLowerLimit(int lower) {
75          if (lower > min) {
76              min = lower;
77          }
78      }
79  
80      /**
81       * Sets upper limit
82       * @param upper upper limit
83       */
84      public void setUpperLimit(int upper) {
85          if (upper < max) {
86              max = upper;
87          }
88      }
89  
90      /**
91       * Resets internal iterator
92       */
93      public void first() {
94          ptr = min;
95      }
96  
97      /**
98       * Returns <code>true</code> if there are more elements in internal interator
99       * @return <code>true</code> if there are more elements in internal interator
100      */
101     public boolean more() {
102         return ptr <= max;
103     }
104 
105     /**
106      * Returns next element from the internal interator
107      * @return next element from the internal interator
108      */
109     public int next() {
110         int r = ptr;
111         ptr = ptr + 1;
112         return r;
113     }
114 
115     /**
116      * Returns <code>true</code> if two sequences are equal
117      * @param o other sequence
118      * @return <code>true</code> if two sequences are equal
119      */
120     public boolean equals(Object o) {
121         if (o instanceof SimpleSequence) {
122             SimpleSequence s = (SimpleSequence)o;
123             if ((s.getMax() == max) && (s.getMin() == min)) {
124                 return true;
125             }
126         }
127         return false;
128     }
129 
130     /**
131      * Returns sum of min and max
132      * @return sum of min and max
133      */
134     public int hashCode() {
135         return min+max;
136     }
137 
138     /**
139      * Compares two sequences
140      * @param o other sequence
141      * @return -1, 0 and 1
142      */
143     public int compareTo(Sequence o) {
144         Sequence s = (Sequence)o;
145         int i = getMin()+getMax();
146         int t = s.getMax()+s.getMin();
147         if (i < t) {
148             return -1;
149         } else if (i > t) {
150             return 1;
151         } else {
152             return 0;
153         }
154     }
155 
156     /**
157      * Returns sequence as string
158      * @return sequence as string
159      */
160     public String toString() {
161         StringBuffer res = new StringBuffer();
162         if (min == max) {
163             res.append(min);
164         } else {
165             if (min == Integer.MIN_VALUE) {
166                 res.append('*');
167             } else {
168                 res.append(min);
169             }
170             res.append(':');
171             if (max == Integer.MAX_VALUE) {
172                 res.append('*');
173             } else {
174                 res.append(max);
175             }
176         }
177         return res.toString();
178     }
179 }