Generics and Performance question.

Posted by Tarmon on Stack Overflow See other posts from Stack Overflow or by Tarmon
Published on 2010-05-02T21:47:50Z Indexed on 2010/05/02 21:58 UTC
Read the original article Hit count: 328

Hey Everyone,

I was wondering if anyone could look over a class I wrote, I am receiving generic warnings in Eclipse and I am just wondering if it could be cleaned up at all. All of the warnings I received are surrounded in ** in my code below.

The class takes a list of strings in the form of (hh:mm AM/PM) and converts them into HourMinute objects in order to find the first time in the list that comes after the current time.

I am also curious about if there are more efficient ways to do this. This works fine but the student in me just wants to find out how I could do this better.

public class FindTime {
    private String[] hourMinuteStringArray;

    public FindTime(String[] hourMinuteStringArray){
        this.hourMinuteStringArray = hourMinuteStringArray;
    }

    public int findTime(){

        HourMinuteList hourMinuteList = convertHMStringArrayToHMArray(hourMinuteStringArray);
        Calendar calendar = new GregorianCalendar();
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int minute = calendar.get(Calendar.MINUTE);
        HourMinute now = new HourMinute(hour,minute);
        int nearestTimeIndex = findNearestTimeIndex(hourMinuteList, now);
        return nearestTimeIndex;
    }

    private int findNearestTimeIndex(HourMinuteList hourMinuteList, HourMinute now){
        HourMinute current;
        int position = 0;
        Iterator<HourMinute> iterator = **hourMinuteList.iterator()**;
        while(iterator.hasNext()){
            current = (HourMinute) iterator.next();
            if(now.compareTo(current) == -1){
                return position;
            }
            position++;
        }
        return position;
    }


    private static HourMinuteList convertHMStringArrayToHMArray(String[] times){
        FindTime s = new FindTime(new String[1]);
        HourMinuteList list = s.new HourMinuteList();
        String[] splitTime = new String[3];
        for(String time : times ){
            String[] tempFirst = time.split(":");
            String[] tempSecond = tempFirst[1].split(" ");
            splitTime[0] = tempFirst[0];
            splitTime[1] = tempSecond[0];
            splitTime[2] = tempSecond[1];
            int hour = Integer.parseInt(splitTime[0]);
            int minute = Integer.parseInt(splitTime[1]);
            HourMinute hm;
            if(splitTime[2] == "AM"){
                hm = s.new HourMinute(hour,minute);
            }
            else if((splitTime[2].equals("PM")) && (hour < 12)){
                hm = s.new HourMinute(hour + 12,minute);
            }
            else{
                hm = s.new HourMinute(hour,minute);
            }

            **list.add(hm);**
        }
        return list;
    }
    class **HourMinuteList** extends **ArrayList** implements RandomAccess{

    }
    class HourMinute implements **Comparable** {
        int hour;
        int minute;

        public HourMinute(int hour, int minute) {
            setHour(hour);
            setMinute(minute);
        }

        int getMinute() {
            return this.minute;
        }
        String getMinuteString(){
            if(this.minute < 10){
                return "0" + this.minute;
            }else{
                return "" + this.minute;
            }
        }

        int getHour() {
            return this.hour;
        }

        void setHour(int hour) {
            this.hour = hour;
        }

        void setMinute(int minute) {
            this.minute = minute;
        }

        @Override
        public int compareTo(Object aThat) {

            if (aThat instanceof HourMinute) {
                HourMinute that = (HourMinute) aThat;
                if (this.getHour() == that.getHour()) {
                    if (this.getMinute() > that.getMinute()) {
                        return 1;
                    } else if (this.getMinute() < that.getMinute()) {
                        return -1;
                    } else {
                        return 0;
                    }
                } else if (this.getHour() > that.getHour()) {
                    return 1;
                } else if (this.getHour() < that.getHour()) {
                    return -1;
                } else {
                    return 0;
                }
            }

            return 0;
        }

    }


If you have any questions let me know. 

Thanks, 
Rob

© Stack Overflow or respective owner

Related posts about java

Related posts about generics