KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > infoglue > deliver > util > Slots


1 /* ===============================================================================
2 *
3 * Part of the InfoGlue Content Management Platform (www.infoglue.org)
4 *
5 * ===============================================================================
6 *
7 * Copyright (C)
8 *
9 * This program is free software; you can redistribute it and/or modify it under
10 * the terms of the GNU General Public License version 2, as published by the
11 * Free Software Foundation. See the file LICENSE.html for more information.
12 *
13 * This program is distributed in the hope that it will be useful, but WITHOUT
14 * ANY WARRANTY, including the implied warranty of MERCHANTABILITY or FITNESS
15 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along with
18 * this program; if not, write to the Free Software Foundation, Inc. / 59 Temple
19 * Place, Suite 330 / Boston, MA 02111-1307 / USA.
20 *
21 * ===============================================================================
22 */

23 package org.infoglue.deliver.util;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collections JavaDoc;
27 import java.util.List JavaDoc;
28
29 /**
30  *
31  */

32 public class Slots {
33     /**
34      * The total valuespace.
35      */

36     private final List JavaDoc allElements;
37
38     /**
39      * The index of the current slot.
40      */

41     private final int currentSlot;
42     
43     /**
44      * The maximum number of elements contained in a slot.
45      */

46     private final int slotSize;
47     
48     /**
49      * The maximum number of visible slots.
50      */

51     private final int slotCount;
52     
53     /**
54      * The maximum number of slots.
55      */

56     private final int maxSlots;
57     
58     /**
59      * The elements in allElements contained in the current slot.
60      */

61     private final List JavaDoc visibleElements;
62     
63     /**
64      * The indices of the visible slots.
65      */

66     private final List JavaDoc visibleSlots; // list of Integer
67

68     /**
69      *
70      */

71     public Slots(final List JavaDoc allElements, final int currentSlot, final int slotSize, final int slotCount)
72     {
73         this.allElements = (allElements == null) ? new ArrayList JavaDoc() : allElements;
74         this.currentSlot = currentSlot;
75         this.slotSize = slotSize;
76         this.slotCount = slotCount;
77         this.maxSlots = calculateMaxSlots(this.allElements.size());
78         validateArguments();
79         this.visibleElements = calculateVisibleElements();
80         this.visibleSlots = calculateVisibleSlots();
81     }
82     
83     /**
84      *
85      */

86     public Slots(final int currentSlot, final int slotSize, final int slotCount, final int maxSlots)
87     {
88         this.allElements = null;
89         this.currentSlot = currentSlot;
90         this.slotSize = slotSize;
91         this.slotCount = slotCount;
92         this.maxSlots = maxSlots;
93         validateArguments();
94         this.visibleElements = null;
95         this.visibleSlots = calculateVisibleSlots();
96     }
97     
98     /**
99      *
100      */

101     public List JavaDoc getVisibleElements()
102     {
103         return Collections.unmodifiableList(visibleElements);
104     }
105     
106     /**
107      *
108      */

109     public List JavaDoc getVisibleSlots()
110     {
111         return Collections.unmodifiableList(visibleSlots);
112     }
113     
114     /**
115      *
116      */

117     public Integer JavaDoc getLastSlot()
118     {
119         return new Integer JavaDoc(maxSlots);
120     }
121
122     /**
123      *
124      */

125     private void validateArguments()
126     {
127         if(slotSize <= 0)
128         {
129             throw new IllegalArgumentException JavaDoc("Slot size must be a positive number.");
130         }
131         if(slotCount <= 0)
132         {
133             throw new IllegalArgumentException JavaDoc("Slot count must be a positive number.");
134         }
135         if(currentSlot <= 0)
136         {
137             throw new IllegalArgumentException JavaDoc("Current slot must be a positive number.");
138         }
139         if(currentSlot > maxSlots)
140         {
141             throw new IllegalArgumentException JavaDoc("Current slot is not a valid slot [" + currentSlot + ">" + maxSlots + "]");
142         }
143     }
144     
145     /**
146      *
147      */

148     private List JavaDoc calculateVisibleElements()
149     {
150         return allElements.subList(getFromElementIndex(), getToElementIndex());
151     }
152
153     /**
154      *
155      */

156     private List JavaDoc calculateVisibleSlots()
157     {
158         final List JavaDoc result = new ArrayList JavaDoc();
159         final int start = startSlot();
160         final int end = Math.min(start + slotCount - 1, maxSlots);
161         
162         for(int i=start; i<=end; ++i)
163         {
164             result.add(new Integer JavaDoc(i));
165         }
166         return result;
167     }
168
169     /**
170      *
171      */

172     private int startSlot()
173     {
174         if(slotCount >= maxSlots)
175         {
176             return 1;
177         }
178         return Math.max(1, currentSlot - ((slotCount - 1) / 2));
179     }
180     
181     /**
182      *
183      */

184     private int getFromElementIndex()
185     {
186         return (currentSlot - 1) * slotSize;
187     }
188     
189     /**
190      *
191      */

192     private int getToElementIndex()
193     {
194         return Math.min(getFromElementIndex() + slotSize, allElements.size());
195     }
196     
197     /**
198      *
199      */

200     private int calculateMaxSlots(final int numberOfElements)
201     {
202         if(numberOfElements == 0 || slotSize == 0)
203         {
204             return 0;
205         }
206         
207         final int mod = numberOfElements / slotSize;
208         final int div = numberOfElements % slotSize;
209         return mod + (div == 0 ? 0 : 1);
210     }
211 }
212
Popular Tags