View Javadoc

1   /*
2    * Copyright (C) 2007 Alf Mikula
3    * 
4    * This file is part of PromoteGo.
5    *
6    * PromoteGo is free software: you can redistribute it and/or modify
7    * it under the terms of the GNU General Public License as published by
8    * the Free Software Foundation, either version 3 of the License, or
9    * (at your option) any later version.
10   *
11   * PromoteGo is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   * GNU General Public License for more details.
15   *
16   * You should have received a copy of the GNU General Public License
17   * along with PromoteGo.  If not, see <http://www.gnu.org/licenses/>.
18   */
19  package org.promotego.logic.storehours;
20  
21  import java.util.Comparator;
22  
23  import org.apache.commons.lang.math.IntRange;
24  
25  /***
26   * @author alf
27   *
28   */
29  public class HourSpecificationComparator implements
30  		Comparator<HourSpecification>
31  {
32  	public int compare(HourSpecification arg0, HourSpecification arg1)
33  	{
34  		Day minDay0 = getMinimumDay(arg0);
35  		Day minDay1 = getMinimumDay(arg1);
36  		
37  		int retval = minDay0.compareTo(minDay1);
38  		if (retval != 0)
39  		{
40  			return retval;
41  		}
42  		else
43  		{
44  			// Warning: we're depending on the fact that the ranges are
45  			// stored in sorted order.
46  			IntRange range0 = arg0.getSecondRanges().get(0);
47  			IntRange range1 = arg1.getSecondRanges().get(0);
48  			retval = range0.getMinimumInteger() - range1.getMinimumInteger();
49  			if (retval != 0)
50  			{
51  				return retval;
52  			}
53  			else
54  			{
55  				return range0.getMaximumInteger() - range1.getMaximumInteger();
56  			}
57  		}
58  	}
59  
60  	private Day getMinimumDay(HourSpecification arg0)
61  	{
62  		Day retval = null;
63  		for (Day thisDay : arg0.getDays())
64  		{
65  			if (retval == null)
66  			{
67  				retval = thisDay;
68  			}
69  			else
70  			{
71  				if (thisDay.ordinal() < retval.ordinal())
72  				{
73  					retval = thisDay;
74  				}
75  			}
76  		}
77  		
78  		return retval;
79  	}
80  }