001package com.hfg.math;
002
003
004import com.hfg.exception.ProgrammingException;
005import com.hfg.util.CompareUtil;
006
007//------------------------------------------------------------------------------
008/**
009 Integer range object.
010 <p></p>
011 @deprecated Use Range&lt;Integer&gt;
012 @author J. Alex Taylor, hairyfatguy.com
013 */
014//------------------------------------------------------------------------------
015// com.hfg Library
016//
017// This library is free software; you can redistribute it and/or
018// modify it under the terms of the GNU Lesser General Public
019// License as published by the Free Software Foundation; either
020// version 2.1 of the License, or (at your option) any later version.
021//
022// This library is distributed in the hope that it will be useful,
023// but WITHOUT ANY WARRANTY; without even the implied warranty of
024// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
025// Lesser General Public License for more details.
026//
027// You should have received a copy of the GNU Lesser General Public
028// License along with this library; if not, write to the Free Software
029// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
030//
031// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
032// jataylor@hairyfatguy.com
033//------------------------------------------------------------------------------
034@Deprecated
035public class IntRange implements Cloneable, Comparable<IntRange>
036{
037   private Integer mStart;
038   private Integer mEnd;
039
040
041   //###########################################################################
042   // CONSTRUCTORS
043   //###########################################################################
044
045   //--------------------------------------------------------------------------
046   public IntRange()
047   {
048   }
049
050   //--------------------------------------------------------------------------
051   public IntRange(Integer inStart, Integer inEnd)
052   {
053      mStart = inStart;
054      mEnd   = inEnd;
055   }
056
057   //###########################################################################
058   // PUBLIC METHODS
059   //###########################################################################
060
061   //---------------------------------------------------------------------------
062   @Override
063   public IntRange clone()
064   {
065      IntRange cloneObj;
066      try
067      {
068         cloneObj = (IntRange) super.clone();
069      }
070      catch (CloneNotSupportedException e)
071      {
072         throw new ProgrammingException(e);
073      }
074
075      return cloneObj;
076   }
077
078   //--------------------------------------------------------------------------
079   @Override
080   public int hashCode()
081   {
082      int hashCode = (getStart() != null ? getStart().hashCode() : 0);
083      hashCode += 31 * (getEnd() != null ? getEnd().hashCode() : 0);
084
085      return hashCode;
086   }
087
088   //--------------------------------------------------------------------------
089   @Override
090   public boolean equals(Object inObj2)
091   {
092      boolean result = false;
093
094      if (inObj2 != null
095            && inObj2 instanceof IntRange)
096      {
097         result = (0 == compareTo((IntRange)inObj2));
098      }
099
100      return result;
101   }
102
103   //--------------------------------------------------------------------------
104   @Override
105   public int compareTo(IntRange inObj2)
106   {
107      int result = 0;
108
109      if (null == inObj2)
110      {
111         result = 1;
112      }
113      else
114      {
115         result = CompareUtil.compare(getStart(), inObj2.getStart());
116         if (0 == result)
117         {
118            result = CompareUtil.compare(getEnd(), inObj2.getEnd());
119         }
120      }
121
122      return result;
123   }
124
125
126   //--------------------------------------------------------------------------
127   public IntRange setStart(Integer inValue)
128   {
129      mStart = inValue;
130      return this;
131   }
132
133   //--------------------------------------------------------------------------
134   public Integer getStart()
135   {
136      return mStart;
137   }
138
139   //--------------------------------------------------------------------------
140   public IntRange setEnd(Integer inValue)
141   {
142      mEnd = inValue;
143      return this;
144   }
145
146   //--------------------------------------------------------------------------
147   public Integer getEnd()
148   {
149      return mEnd;
150   }
151
152   //--------------------------------------------------------------------------
153   @Override
154   public String toString()
155   {
156      return String.format("[%d, %d]", getStart(), getEnd());
157   }
158
159   //--------------------------------------------------------------------------
160   public Integer length()
161   {
162      Integer length = null;
163      if (getStart() != null
164            && getEnd() != null)
165      {
166         length = getEnd() - getStart() + 1;
167      }
168
169      return length;
170   }
171
172   //--------------------------------------------------------------------------
173   public boolean contains(int inValue)
174   {
175      return ((getStart() != null
176               && getEnd() != null
177               && inValue >= getStart()
178               && inValue <= getEnd())
179              || (null == getStart()
180                  && getEnd() != null
181                  && inValue <= getEnd())
182              || (null == getEnd()
183                  && getStart() != null
184                  && inValue >= getStart())
185              || (null == getStart()
186                  && null == getEnd()));
187   }
188
189   //--------------------------------------------------------------------------
190   public boolean contains(IntRange inRange2)
191   {
192      return (contains(inRange2.getStart())
193              && contains(inRange2.getEnd()));
194   }
195
196   //--------------------------------------------------------------------------
197   public IntRange intersection(IntRange inRange2)
198   {
199      IntRange intersection = null;
200
201      if (inRange2 != null)
202      {
203         if (getStart() != null)
204         {
205            if (inRange2.getStart() != null)
206            {
207               if (getEnd() != null)
208               {
209                  if (inRange2.getEnd() != null)
210                  {
211                     if (getStart() <= inRange2.getEnd()
212                           && getEnd() >= inRange2.getStart())
213                     {
214                        //          -----
215                        //             -----
216
217                        intersection = new IntRange()
218                                           .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart())
219                                           .setEnd(getEnd() > inRange2.getEnd() ? inRange2.getEnd() : getEnd());
220                     }
221                  }
222                  else // Range 2 extends infinitely to the right
223                  {
224                     //          -----
225                     //             ----->
226                     if (inRange2.contains(getStart()))
227                     {
228                        intersection = new IntRange()
229                              .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart())
230                              .setEnd(getEnd());
231                     }
232                  }
233               }
234               else if (inRange2.getEnd() != null) // Range 1 extends infinitely to the right
235               {
236                  //             ----->
237                  //          -----
238
239                  if (getStart() <= inRange2.getEnd())
240                  {
241                     intersection = new IntRange()
242                           .setStart(getStart() < inRange2.getStart() ? inRange2.getStart() : getStart())
243                           .setEnd(inRange2.getEnd());
244                  }
245               }
246               else
247               {
248                  // Both ranges extend infinitely to the right
249                  //               ----->
250                  //             ------->
251                  intersection = new IntRange()
252                        .setStart(getStart() > inRange2.getStart() ? inRange2.getStart() : getStart())
253                        .setEnd(getStart() > inRange2.getStart() ? getStart() : inRange2.getStart());
254               }
255            }
256            else // Range 2 extends infinitely to the left
257            {
258               if (getEnd() != null)
259               {
260                  if (inRange2.getEnd() != null)
261                  {  //               -----
262                     //             <-------
263                     if (getStart() <= inRange2.getEnd())
264                     {
265                        intersection = new IntRange()
266                              .setStart(getStart())
267                              .setEnd(getEnd() > inRange2.getEnd() ? inRange2.getEnd() : getEnd());
268                     }
269                  }
270                  else // Range 2 extends infinitely to the left & right
271                  {    //               -----
272                       //             <------->
273                     intersection = clone();
274                  }
275               }
276               else if (inRange2.getEnd() != null) // Range 1 extends infinitely to the right
277               {   //             ----->
278                   //          <---
279                  if (inRange2.contains(getStart()))
280                  {
281                     intersection = new IntRange()
282                           .setStart(getStart())
283                           .setEnd(inRange2.getEnd());
284                  }
285               }
286               else
287               {
288                  // Both ranges extend infinitely to the right
289                  //             ----->
290                  //          <------->
291                  intersection = new IntRange()
292                        .setStart(getStart());
293               }
294            }
295         }
296         else if (inRange2.getStart() != null)
297         {
298            if (getEnd() != null)
299            {
300               if (inRange2.getEnd() != null)
301               {
302                  //             <-----
303                  //               -------
304                  if (inRange2.getStart() <= getEnd())
305                  {
306                     intersection = new IntRange()
307                           .setStart(inRange2.getStart())
308                           .setEnd(getEnd() <= inRange2.getEnd() ? getEnd() : inRange2.getEnd());
309                  }
310               }
311               else
312               {
313                  //             <-----
314                  //               ------->
315                  if (inRange2.getStart() <= getEnd())
316                  {
317                     intersection = new IntRange()
318                           .setStart(inRange2.getStart())
319                           .setEnd(getEnd());
320                  }
321               }
322            }
323            else
324            {
325               if (inRange2.getEnd() != null)
326               {
327                  //         <----->
328                  //            ---
329                  intersection = inRange2.clone();
330               }
331               else
332               {
333                  //         <----->
334                  //            --->
335                  intersection = new IntRange()
336                         .setStart(inRange2.getStart());
337
338               }
339            }
340         }
341         else
342         {
343            if (getEnd() != null)
344            {
345               if (inRange2.getEnd() != null)
346               {
347                  //         <-----
348                  //         <---
349                  intersection = new IntRange()
350                        .setEnd(getEnd() <= inRange2.getEnd() ? getEnd() : inRange2.getEnd());
351               }
352               else
353               {
354                  //         <-----
355                  //         <-------->
356                  intersection = clone();
357               }
358            }
359            else
360            {
361               if (inRange2.getEnd() != null)
362               {
363                  //         <----->
364                  //         <---
365                  intersection = inRange2.clone();
366               }
367               else
368               {
369                  //         <----->
370                  //         <----->
371                  intersection = clone();
372               }
373            }
374         }
375      }
376
377      return intersection;
378   }
379
380   //--------------------------------------------------------------------------
381   public boolean intersects(IntRange inRange2)
382   {
383      boolean result = false;
384
385      if (getStart() != null)
386      {
387         if (inRange2.getStart() != null)
388         {
389            if (getEnd() != null)
390            {
391               if (inRange2.getEnd() != null)
392               {
393                  result = getStart() <= inRange2.getEnd() && getEnd() >= inRange2.getStart();
394               }
395               else // Range 2 extends infinitely to the right
396               {
397                  result = inRange2.getStart() <= getEnd();
398               }
399            }
400            else if (inRange2.getEnd() != null)
401            {
402               result = inRange2.getEnd() >= getStart();
403            }
404            else
405            {
406               // Both ranges extend infinitely to the right
407               result = true;
408            }
409         }
410         else // Range 2 extends infinitely to the left
411         {
412            if (inRange2.getEnd() != null)
413            {
414               result = getStart() <= inRange2.getEnd();
415            }
416            else
417            {
418               // Range2 is infinite
419               result = true;
420            }
421         }
422      }
423      else if (inRange2.getStart() != null)
424      {
425         if (getEnd() != null)
426         {
427            result = inRange2.getStart() <= getEnd();
428         }
429         else
430         {
431            result = true;
432         }
433      }
434      else
435      {
436         // Both ranges extend infinitely to the left
437         result = true;
438      }
439
440      return result;
441   }
442}