/** * Creates a new range. * * @param start * @param end * @exception IllegalArgumentException if either start or end is null */ public DateTimeRange(DateTime start, DateTime end) { assertArgumentNotNull(start, "start"); assertArgumentNotNull(end, "end"); this.start = start; this.end = end; }
/** * Converts empty ranges into {@link DateTime}. * * @return a DateTime object * @exception IllegalStateException if the start and end date are * distinct, which means the dateTimeRange is neither a date * nor a dateTime (see {@link #isDateTimeOnly()}) */ public DateTime toDateTime() { if (!isDateTimeOnly()) { throw new IllegalStateException("This is a valid range, with distinct " + "start and end date. It cannot be converted to one DateTime value. " + "(Check with isDateTimeOnly() first): " + this); } return start; }
/** * Creates a {@link com.google.api.gbase.client.GoogleBaseAttribute} of type * {@link GoogleBaseAttributeType#DATE_TIME_RANGE} * and initializes it using the current state of the object. * * @param name attribute name * @param range attribute value * @return a new {@link com.google.api.gbase.client.GoogleBaseAttribute} */ public static GoogleBaseAttribute createAttribute(String name, DateTimeRange range) { return new GoogleBaseAttribute(name, GoogleBaseAttributeType.DATE_TIME_RANGE, range.toString()); }
/** * Extracts a {@link DateTimeRange} from the value of a * {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * @param attribute * @exception NumberFormatException if the conversion failed, either * because on of the date/time was invalid or if the expected start * and end tags were missing */ public static DateTimeRange extractDateTimeRange(GoogleBaseAttribute attribute) { String range = attribute.getValueAsString(); int space = range.indexOf(' '); if (space == -1) { /* Not really a range, but since date and dateTime are subtypes * of DateTimeRange, try and convert them into a DateTimeRange. */ return new DateTimeRange(toDateOrDateTime(range)); } else { String startStr = range.substring(0, space); String endStr = range.substring(space+1); return new DateTimeRange(toDateOrDateTime(startStr), toDateOrDateTime(endStr)); } }
/** * Creates a {@link com.google.api.gbase.client.GoogleBaseAttribute} of type * {@link GoogleBaseAttributeType#DATE_TIME_RANGE} * and initializes it using the current state of the object. * * @param name attribute name * @param range attribute value * @return a new {@link com.google.api.gbase.client.GoogleBaseAttribute} */ public static GoogleBaseAttribute createAttribute(String name, DateTimeRange range) { return new GoogleBaseAttribute(name, GoogleBaseAttributeType.DATE_TIME_RANGE, range.toString()); }
/** * Extracts a {@link DateTimeRange} from the value of a * {@link com.google.api.gbase.client.GoogleBaseAttribute}. * * @param attribute * @exception NumberFormatException if the conversion failed, either * because on of the date/time was invalid or if the expected start * and end tags were missing */ public static DateTimeRange extractDateTimeRange(GoogleBaseAttribute attribute) { String range = attribute.getValueAsString(); int space = range.indexOf(' '); if (space == -1) { /* Not really a range, but since date and dateTime are subtypes * of DateTimeRange, try and convert them into a DateTimeRange. */ return new DateTimeRange(toDateOrDateTime(range)); } else { String startStr = range.substring(0, space); String endStr = range.substring(space+1); return new DateTimeRange(toDateOrDateTime(startStr), toDateOrDateTime(endStr)); } }
/** * Converts empty ranges into {@link DateTime}. * * @return a DateTime object * @exception IllegalStateException if the start and end date are * distinct, which means the dateTimeRange is neither a date * nor a dateTime (see {@link #isDateTimeOnly()}) */ public DateTime toDateTime() { if (!isDateTimeOnly()) { throw new IllegalStateException("This is a valid range, with distinct " + "start and end date. It cannot be converted to one DateTime value. " + "(Check with isDateTimeOnly() first): " + this); } return start; }
/** * Creates a new range. * * @param start * @param end * @exception IllegalArgumentException if either start or end is null */ public DateTimeRange(DateTime start, DateTime end) { assertArgumentNotNull(start, "start"); assertArgumentNotNull(end, "end"); this.start = start; this.end = end; }
/** * Adds an attribute of type * {@link GoogleBaseAttributeType#DATE_TIME_RANGE}. * * This method will never remove an attribute, even if it has * the same name as the new attribute. If you would like to set * an attribute that can only appear once, call * {@link #removeAttributes(String, GoogleBaseAttributeType)} first. * * @param name attribute name * @param dateTimeRange attribute value * @return the attribute object that has been created and added to the item * @exception IllegalArgumentException if the DateTimeRange is an empty * range, in which case {@link #addDateTimeAttribute(String, * com.google.gdata.data.DateTime)} should be used instead. */ public GoogleBaseAttribute addDateTimeRangeAttribute(String name, DateTimeRange dateTimeRange) { if (dateTimeRange.isDateTimeOnly()) { // The server would reject such a range throw new IllegalArgumentException("Empty DateTimeRange. Add " + "it as a single DateTime using addDateTimeAttribute() instead."); } return addAttribute(ConversionUtil.createAttribute(name, dateTimeRange)); }
/** * Adds an attribute of type * {@link GoogleBaseAttributeType#DATE_TIME_RANGE}. * * This method will never remove an attribute, even if it has * the same name as the new attribute. If you would like to set * an attribute that can only appear once, call * {@link #removeAttributes(String, GoogleBaseAttributeType)} first. * * @param name attribute name * @param dateTimeRange attribute value * @return the attribute object that has been created and added to the item * @exception IllegalArgumentException if the DateTimeRange is an empty * range, in which case {@link #addDateTimeAttribute(String, * com.google.gdata.data.DateTime)} should be used instead. */ public GoogleBaseAttribute addDateTimeRangeAttribute(String name, DateTimeRange dateTimeRange) { if (dateTimeRange.isDateTimeOnly()) { // The server would reject such a range throw new IllegalArgumentException("Empty DateTimeRange. Add " + "it as a single DateTime using addDateTimeAttribute() instead."); } return addAttribute(ConversionUtil.createAttribute(name, dateTimeRange)); }