package com.upweather.upobject;
import java.util.Calendar;
import com.uplibrary.upexception.UPInvalidParameterException;
/**
* This class is a generic representation of datetime type
* An object of this type has a year, a month, a day, a hour and
* a calendar object.
* @author Giuseppe Persico - University of Naples "Parthenope"
* @version 1.0
*/
public final class UPDateTime {
// The year
private Integer year;
// The month
private String month;
// The day
private String day;
// The hour
private String hour;
// Calendar object
private static Calendar cal;
// This static block initializes once the calendar istance
static {
cal = Calendar.getInstance();
}
/**
* The constructor initializes the attributes with default
* values.
*/
public UPDateTime() {
this.year = cal.get(Calendar.YEAR);
this.month = String.valueOf(cal.get(Calendar.MONTH));
this.day = String.valueOf(cal.get(Calendar.DAY_OF_MONTH));
this.hour = String.valueOf(cal.get(Calendar.HOUR_OF_DAY));
}
/**
* Overloaded constructor initializes the attributes with user
* defined parameters.
* @param year the year
* @param month the month
* @param day the day
* @param hour the hour
* @throws UPInvalidParameterException if the parameters specified are invalid.
*/
public UPDateTime(final Integer year, final Integer month, final Integer day, final Integer hour) throws UPInvalidParameterException {
this.setYear(year).setMonth(month).setDay(day).setHour(hour);
}
/**
* Overloaded constructor initializes the attributes with user
* defined parameters.
* @param year the year
* @param month the month
* @param day the day
* @param hour the hour
* @throws UPInvalidParameterException if the parameters specified are invalid.
*/
public UPDateTime(final Integer year, final String month, final String day, final String hour) throws UPInvalidParameterException {
this.setYear(year).setMonth(month).setDay(day).setHour(hour);
}
/**
* This method allows to give an integer value to the year attribute.
* @param year the year
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if year is null or less then the current year
*/
public final UPDateTime setYear(final Integer year) throws UPInvalidParameterException {
if (year==null) {
throw new UPInvalidParameterException("Year can not be a null value");
} else if (year<this.year) {
throw new UPInvalidParameterException("Year cannot be less then the current year");
}
this.year = year;
return this;
}
/**
* This method allows to give an integer value to the month attribute
* @param month the month
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if month is null or is less then the current month
*/
public final UPDateTime setMonth(final Integer month) throws UPInvalidParameterException {
if (month==null) {
throw new UPInvalidParameterException("Month can not be a null value");
} else if (month<Integer.valueOf(this.month)) {
throw new UPInvalidParameterException("Month cannot be less then the current month");
}
this.month = String.valueOf(month);
return this;
}
/**
* This method allows to give a string value to the month attribute.
* @param month the month
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if month is null
*/
public final UPDateTime setMonth(String month) throws UPInvalidParameterException {
if (month == null) {
throw new UPInvalidParameterException("Month can not be a null value");
} else if (month.length() == 1) {
this.month = "0" + month;
} else {
this.setMonth(Integer.valueOf(month));
}
return this;
}
/**
* This method allows to give an integer value to the day attribute.
* @param day the day
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if day is null or is less then the current day
*/
public final UPDateTime setDay(final Integer day) throws UPInvalidParameterException {
if (day == null) {
throw new UPInvalidParameterException("Day can not be a null value");
} else if (day<Integer.valueOf(this.day)) {
throw new UPInvalidParameterException("Day cannot be less then the current day");
}
this.day = String.valueOf(day);
return this;
}
/**
* This method allows to give a string value to the day attribute.
* @param day the day
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if day is null
*/
public final UPDateTime setDay(String day) throws UPInvalidParameterException {
if (day==null) {
throw new UPInvalidParameterException("Day can not be a null value");
} else if (day.length()==1) {
this.day = "0" + day;
} else {
this.setDay(Integer.valueOf(day));
}
return this;
}
/**
* This method allows to give an integer value to the hour attribute
* @param hour the hour
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if hour is null or is less then the current hour
*/
public final UPDateTime setHour(final Integer hour) throws UPInvalidParameterException {
if (hour==null) {
throw new UPInvalidParameterException("Hour can not be a null value");
} else if (hour<Integer.valueOf(this.hour)) {
throw new UPInvalidParameterException("Hour cannot be less then the current hour");
}
this.hour = String.valueOf(hour);
return this;
}
/**
* This method allows to give a string value to the hour attribute.
* @param hour the hour
* @return a reference to enable cascade calls
* @throws UPInvalidParameterException if hour is null.
*/
public final UPDateTime setHour(String hour) throws UPInvalidParameterException {
if (hour==null) {
throw new UPInvalidParameterException("Hour can not be a null value");
} else if (hour.length()==1) {
this.hour = "0" + hour;
} else {
this.setHour(Integer.valueOf(hour));
}
return this;
}
/**
* This method returns the year
* @return the year
*/
public final Integer getYear() {
return this.year;
}
/**
* This method returns the month
* @return the month
*/
public final String getMonth() {
return this.month;
}
/**
* This method returns the day
* @return the day
*/
public final String getDay() {
return this.day;
}
/**
* This method returns the hour
* @return the hour
*/
public final String getHour() {
return this.hour;
}
}