1 23 package org.infoglue.deliver.util; 24 25 import java.util.ArrayList ; 26 import java.util.Collections ; 27 import java.util.List ; 28 29 32 public class Slots { 33 36 private final List allElements; 37 38 41 private final int currentSlot; 42 43 46 private final int slotSize; 47 48 51 private final int slotCount; 52 53 56 private final int maxSlots; 57 58 61 private final List visibleElements; 62 63 66 private final List visibleSlots; 68 71 public Slots(final List allElements, final int currentSlot, final int slotSize, final int slotCount) 72 { 73 this.allElements = (allElements == null) ? new ArrayList () : 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 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 101 public List getVisibleElements() 102 { 103 return Collections.unmodifiableList(visibleElements); 104 } 105 106 109 public List getVisibleSlots() 110 { 111 return Collections.unmodifiableList(visibleSlots); 112 } 113 114 117 public Integer getLastSlot() 118 { 119 return new Integer (maxSlots); 120 } 121 122 125 private void validateArguments() 126 { 127 if(slotSize <= 0) 128 { 129 throw new IllegalArgumentException ("Slot size must be a positive number."); 130 } 131 if(slotCount <= 0) 132 { 133 throw new IllegalArgumentException ("Slot count must be a positive number."); 134 } 135 if(currentSlot <= 0) 136 { 137 throw new IllegalArgumentException ("Current slot must be a positive number."); 138 } 139 if(currentSlot > maxSlots) 140 { 141 throw new IllegalArgumentException ("Current slot is not a valid slot [" + currentSlot + ">" + maxSlots + "]"); 142 } 143 } 144 145 148 private List calculateVisibleElements() 149 { 150 return allElements.subList(getFromElementIndex(), getToElementIndex()); 151 } 152 153 156 private List calculateVisibleSlots() 157 { 158 final List result = new ArrayList (); 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 (i)); 165 } 166 return result; 167 } 168 169 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 184 private int getFromElementIndex() 185 { 186 return (currentSlot - 1) * slotSize; 187 } 188 189 192 private int getToElementIndex() 193 { 194 return Math.min(getFromElementIndex() + slotSize, allElements.size()); 195 } 196 197 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 |