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