⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 appointmentblockarray.java

📁 Rapla是一个灵活的多用户资源管理系统。它提供的一些功能有:日历GUI
💻 JAVA
字号:
/*--------------------------------------------------------------------------*
 | Copyright (C) 2006 Christopher Kohlhaas                                  |
 |                                                                          |
 | This program is free software; you can redistribute it and/or modify     |
 | it under the terms of the GNU General Public License as published by the |
 | Free Software Foundation. A copy of the license has been included with   |
 | these distribution in the COPYING file, if not go to www.fsf.org .       |
 |                                                                          |
 | As a special exception, you are granted the permissions to link this     |
 | program with every library, which license fulfills the Open Source       |
 | Definition as published by the Open Source Initiative (OSI).             |
 *--------------------------------------------------------------------------*/
package org.rapla.entities.domain;

import java.util.Arrays;

/** This class is introduced only for performance reasons.
    Stores the single appointment-blocks of one or more appointments
    in a given period.
    @see Appointment#createBlocks
*/
public class AppointmentBlockArray
{
    long[] startDates;
    long[] endDates;
    Appointment[] appointments;
    boolean isException;
    int size = 0;
    private static final int initialSize = 4;
    
    public void add(long start,long end,Appointment appointment,boolean isException) {
        this.isException = isException;
        if (startDates == null)
            startDates = new long[initialSize];
        if (endDates == null)
            endDates = new long[initialSize];
        if (appointments == null)
            appointments = new Appointment[initialSize];

        if (startDates.length == size) {
            // double array size
            int newSize = size * 2;
            long[] newStartDates = new long[newSize];
            System.arraycopy(startDates,0,newStartDates,0,startDates.length);
            startDates = newStartDates;
            long[] newEndDates = new long[newSize];
            System.arraycopy(endDates,0,newEndDates,0,endDates.length);
            endDates = newEndDates;
            Appointment[] newAppointments = new Appointment[newSize];
            System.arraycopy(appointments,0,newAppointments,0,appointments.length);
            appointments = newAppointments;
        }

        startDates[size]=start;
        endDates[size]= end;
        appointments[size] = appointment;
        size ++;
    }

    public int size() {
        return size;
    }

    /** sorts the entries according to their start-dates */
    public void sort() {
        Entry[] entries = new Entry[ size ];
        for (int i=0;i<size; i++) {
            entries[i] = new Entry();
            entries[i].appointment = appointments[i];
            entries[i].start = startDates[i];
            entries[i].end = endDates[i];
        }
        Arrays.sort( entries );
        for (int i=0;i<size; i++) {
            appointments[i] = entries[i].appointment;
            startDates[i] = entries[i].start;
            endDates[i] = entries[i].end;
        }
    }
    
    class Entry implements Comparable {
        Appointment appointment;
        long start;
        long end;
        public int compareTo(Object obj) {
            Entry other = (Entry) obj;
            if (other.start > start)
                return -1;
            if (other.start < start) 
                return 1;
            if (other.end > end)
                return 1;
            if (other.end < end)
                return -1;
            return 0;
        }
    }
    
    private void checkIndex(int index) {
        if (index<0 || index>=size)
            throw new ArrayIndexOutOfBoundsException("Appointment index " + index + " not found. Current block size " + size());
    }

    public long getStartAt(int index) {
        checkIndex(index);
        return startDates[index];
    }

    public long getEndAt(int index) {
        checkIndex(index);
        return endDates[index];
    }
    
    public boolean isException( int index ){
        return isException;
    }

    public Appointment getAppointmentAt(int index) {
        checkIndex(index);
        return appointments[index];
    }
}









⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -