001package biweekly.property; 002 003import java.util.ArrayList; 004import java.util.Date; 005import java.util.List; 006 007import biweekly.Warning; 008import biweekly.component.ICalComponent; 009import biweekly.parameter.FreeBusyType; 010import biweekly.util.Duration; 011import biweekly.util.Period; 012 013/* 014 Copyright (c) 2013, Michael Angstadt 015 All rights reserved. 016 017 Redistribution and use in source and binary forms, with or without 018 modification, are permitted provided that the following conditions are met: 019 020 1. Redistributions of source code must retain the above copyright notice, this 021 list of conditions and the following disclaimer. 022 2. Redistributions in binary form must reproduce the above copyright notice, 023 this list of conditions and the following disclaimer in the documentation 024 and/or other materials provided with the distribution. 025 026 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 027 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 028 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 029 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 030 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 031 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 032 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 033 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 034 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 035 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 036 */ 037 038/** 039 * <p> 040 * Defines a person's availability over certain time periods (for example, 041 * "busy" between 1pm-3pm and 4pm-5pm). Note that this property can contain 042 * multiple time periods, but only one availability type may be defined (e.g. 043 * "busy" or "free"). 044 * </p> 045 * <p> 046 * <b>Code sample:</b> 047 * 048 * <pre class="brush:java"> 049 * VFreeBusy fb = new VFreeBusy(); 050 * 051 * FreeBusy freebusy = new FreeBusy(); 052 * freebusy.setType(FreeBusyType.BUSY); 053 * 054 * Date onePM = ... 055 * Date threePM = ... 056 * freebusy.addValue(onePM, threePM); 057 * 058 * Date fourPM = ... 059 * Duration oneHour = Duration.builder().hours(1).build(); 060 * freeBusy.addValue(fourPM, oneHour); 061 * 062 * fb.addFreeBusy(freebusy); 063 * </pre> 064 * 065 * </p> 066 * @author Michael Angstadt 067 * @see <a href="http://tools.ietf.org/html/rfc5545#page-100">RFC 5545 068 * p.100-1</a> 069 */ 070public class FreeBusy extends ICalProperty { 071 private final List<Period> values = new ArrayList<Period>(); 072 073 /** 074 * Adds a time period. 075 * @param start the start date 076 * @param end the end date 077 */ 078 public void addValue(Date start, Date end) { 079 values.add(new Period(start, end)); 080 } 081 082 /** 083 * Adds a time period. 084 * @param start the start date 085 * @param duration the duration 086 */ 087 public void addValue(Date start, Duration duration) { 088 values.add(new Period(start, duration)); 089 } 090 091 /** 092 * Gets all time periods. 093 * @return the time periods 094 */ 095 public List<Period> getValues() { 096 return values; 097 } 098 099 /** 100 * Gets the person's status over the time periods that are specified in this 101 * property (for example, "free" or "busy"). If not set, the user should be 102 * considered "busy". 103 * @return the type or null if not set 104 * @see <a href="http://tools.ietf.org/html/rfc5545#page-20">RFC 5545 105 * p.20</a> 106 */ 107 public FreeBusyType getType() { 108 return parameters.getFreeBusyType(); 109 } 110 111 /** 112 * Sets the person's status over the time periods that are specified in this 113 * property (for example, "free" or "busy"). If not set, the user should be 114 * considered "busy". 115 * @param fbType the type or null to remove 116 * @see <a href="http://tools.ietf.org/html/rfc5545#page-20">RFC 5545 117 * p.20</a> 118 */ 119 public void setType(FreeBusyType fbType) { 120 parameters.setFreeBusyType(fbType); 121 } 122 123 @Override 124 protected void validate(List<ICalComponent> components, List<Warning> warnings) { 125 if (values.isEmpty()) { 126 warnings.add(Warning.validate(38)); 127 return; 128 } 129 130 for (Period timePeriod : values) { 131 if (timePeriod.getStartDate() == null) { 132 warnings.add(Warning.validate(39)); 133 break; 134 } 135 } 136 137 for (Period timePeriod : values) { 138 if (timePeriod.getEndDate() == null && timePeriod.getDuration() == null) { 139 warnings.add(Warning.validate(40)); 140 break; 141 } 142 } 143 } 144}