source: xssim/src/test/rewolucja/reservation/ReservationListNew.java @ 104

Revision 104, 5.3 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package test.rewolucja.reservation;
2
3import java.util.ArrayList;
4import java.util.Collection;
5import java.util.Comparator;
6import java.util.Iterator;
7
8import org.apache.commons.logging.Log;
9import org.apache.commons.logging.LogFactory;
10
11import schedframe.scheduling.TimeResourceAllocation;
12import test.rewolucja.reservation.ReservationNew.Status;
13
14
15public class ReservationListNew extends ArrayList<ReservationNew> {
16       
17        protected static Log log = LogFactory.getLog(ReservationListNew.class);
18
19        private static final long serialVersionUID = -7805575807997748584L;
20
21        protected Comparator<ReservationNew> comparator;
22       
23        /**
24         * Default constructor
25         *
26         */
27        public ReservationListNew() {
28                super();
29                this.comparator = new ReservationComparator();
30        }
31
32        public boolean add(ReservationNew r){
33                int i = findPositionFor(r);
34                super.add(i, r);
35                return true;
36        }
37
38        public ReservationNew get(ReservationNew r){
39                int size = size();
40
41                // find location estimation of the reservation r
42                int idx = bisection(r.getStartMillis(), 0, size - 1);
43                int i = idx;
44
45                // go left and check if desired reservation is there
46                ReservationNew res = get(i);
47                        // check only these reservations which start time is later
48                        // or equal to r start time
49                while(i >= 0 && comparator.compare(res, r) >= 0){
50                        if(res.equals(r))
51                                return res;
52                        i--;
53                        res = get(i);
54                }
55               
56                // if it is not there, then go to the right
57                i = idx + 1;
58                res = get(i);
59                        // check only these reservations which start time is earlier
60                        // or equal to r start time
61                while(i < size && comparator.compare(res, r) <= 0){
62                        if(res.equals(r))
63                                return res;
64                        i++;
65                        res = get(i);
66                }
67               
68                throw new IllegalArgumentException("There is no reservation " + r + " on the list");
69        }
70       
71        private int bisection(long startTime, int leftIdx, int rightIdx){
72
73                // 4 elements can be iterated by for loop
74                if((rightIdx - leftIdx) < 4)
75                        return leftIdx;
76               
77                long first_startTime = get(leftIdx).getStartMillis();
78                long last_startTime = get(rightIdx).getStartMillis();
79               
80                if(startTime < first_startTime){
81                        int idx = leftIdx;
82                        while(idx > 0 && startTime < get(idx).getStartMillis())
83                                idx--;
84                        return idx;
85                }
86               
87                /* measure distance between new reservation start time and
88                 * both ends of the reservations list
89                 */
90                long delta_f = startTime - first_startTime;
91                long delta_l = last_startTime - startTime;
92
93                int middleIdx = leftIdx + ((rightIdx - leftIdx)/2);
94               
95                /* choose the correct half of the reservation list,
96                 * where "correct" means this one, which end is closer
97                 * to the new reservation start time
98                 */
99                if(delta_f < delta_l){
100                        return bisection(startTime, leftIdx, middleIdx + 1);
101                } else {
102                        return bisection(startTime, middleIdx - 1, rightIdx);
103                }
104               
105        }
106       
107        protected int findPositionFor(ReservationNew r){
108                long startTime = r.getStartMillis();
109                boolean trace = false;
110               
111                int size = size();
112                int i = bisection(startTime, 0, size - 1);
113                if(trace)
114                        log.debug("asdfgh: " + r.getStart() + " " + r.getId() + " " + i);
115                while(i < size && comparator.compare(get(i), r) < 1){
116                        i++;
117                }
118               
119                if(trace) {
120                        log.debug("asdfgh: " + r.getId() + " " + i);
121                        for(int j = 0; j < size(); j++){
122                                ReservationNew lr = get(j);
123                                log.debug("asdfgh: "+ j +".\t" + lr.getStart() + " id=" + lr.getId() + " jobId="+ lr.getJobId());
124                        }
125                }
126               
127                return i;
128        }
129       
130        /**
131         * Creates reservation for requested time resource allocation
132         * @param key
133         * @param resourceUsage
134         * @return reservation
135         */
136        public ReservationNew add(TimeResourceAllocation resourceUsage, Status status){
137                throw new RuntimeException("This method is not implemented");
138        }
139       
140        public void add(int i, ReservationNew r){
141                super.add(i, r);
142                log.warn("Using this method may violate order of the reservations.");
143        }
144       
145        public boolean addAll(Collection<? extends ReservationNew> c){
146                Iterator<? extends ReservationNew> itr = c.iterator();
147                while(itr.hasNext()){
148                        this.add(itr.next());
149                }
150                return true;
151        }
152       
153        public boolean addAll(int i, Collection<? extends ReservationNew> c){
154                boolean ret = super.addAll(c);
155                log.warn("Using this method may violate order of the reservations.");
156                return ret;
157        }
158       
159       
160        public TimeResourceAllocation findInterval(TimeResourceAllocation timeSlot, long interval, int size) {
161                throw new RuntimeException("Not implemented");
162                /*if (timeSlot.getEndTimeInMillis() <= timeSlot.getStartTimeInMillis()
163                                || interval == 0 || timeSlot.getDurationInMillis() < interval)
164                        return null;
165                LinkedHashSet<Long> timestampsSet = createTimestampsSet();
166                TreeMap<Long, Integer> calendar = createCalendar(timestampsSet);
167                if(calendar.size()==0){
168                        return new TimeSlot(timeSlot.getStartTimeInMillis(), timeSlot.getStartTimeInMillis() + interval);
169                }
170                Date availableStartDate = getAvailableStartDate(calendar, timeSlot
171                                .getStartTime().getTime(), timeSlot.getEndTime().getTime(),
172                                interval, size);
173                if (availableStartDate == null)
174                        return null;
175                TimeSlot availableTimeSlot = new TimeSlot(availableStartDate.getTime(),
176                                availableStartDate.getTime() + interval);
177                return availableTimeSlot;
178                */
179        }
180       
181        protected class ReservationComparator implements Comparator<ReservationNew>{
182
183                public int compare(ReservationNew o1, ReservationNew o2) {
184                        long start1 = o1.getStart().getMillis();
185                        long start2 = o2.getStart().getMillis();
186
187                        if(start1 < start2)
188                                return -1;
189                       
190                        if(start1 > start2)
191                                return 1;
192                       
193                        return 0;
194                }
195               
196        }
197}
Note: See TracBrowser for help on using the repository browser.