001package biweekly.property;
002
003import java.util.List;
004
005import biweekly.ICalVersion;
006import biweekly.Warning;
007import biweekly.component.ICalComponent;
008import biweekly.util.VersionNumber;
009
010/*
011 Copyright (c) 2013-2015, Michael Angstadt
012 All rights reserved.
013
014 Redistribution and use in source and binary forms, with or without
015 modification, are permitted provided that the following conditions are met: 
016
017 1. Redistributions of source code must retain the above copyright notice, this
018 list of conditions and the following disclaimer. 
019 2. Redistributions in binary form must reproduce the above copyright notice,
020 this list of conditions and the following disclaimer in the documentation
021 and/or other materials provided with the distribution. 
022
023 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
024 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
025 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
026 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
027 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
029 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
030 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
031 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
032 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
033 */
034
035/**
036 * <p>
037 * Defines the min/max iCalendar versions a consumer must support in order to
038 * successfully parse the iCalendar object.
039 * </p>
040 * <p>
041 * <b>Code sample:</b>
042 * 
043 * <pre class="brush:java">
044 * ICalendar ical = new ICalendar();
045 * 
046 * //all ICalendar objects are given a VERSION property on creation
047 * ical.getVersion(); //&quot;2.0&quot;
048 * 
049 * //get the default iCal version
050 * Version version = Version.v2_0();
051 * ical.setVersion(version);
052 * </pre>
053 * 
054 * </p>
055 * @author Michael Angstadt
056 * @see <a href="http://tools.ietf.org/html/rfc5545#page-79">RFC 5545
057 * p.79-80</a>
058 * @see <a href="http://tools.ietf.org/html/rfc2445#page-76">RFC 2445 p.76-7</a>
059 * @see <a href="http://www.imc.org/pdi/vcal-10.doc">vCal 1.0 p.24</a>
060 */
061public class Version extends ICalProperty {
062        public static final VersionNumber VCAL = new VersionNumber(ICalVersion.V1_0.getVersion());
063        public static final VersionNumber ICAL = new VersionNumber(ICalVersion.V2_0.getVersion());
064
065        private VersionNumber minVersion, maxVersion;
066
067        /**
068         * Creates a new version property.
069         * @param version the version that a consumer must support in order to
070         * successfully parse the iCalendar object
071         */
072        public Version(ICalVersion version) {
073                this((version == null) ? null : version.getVersion());
074        }
075
076        /**
077         * Creates a new version property.
078         * @param version the version that a consumer must support in order to
079         * successfully parse the iCalendar object
080         * @throws IllegalArgumentException if the version string is invalid
081         */
082        public Version(String version) {
083                this(null, version);
084        }
085
086        /**
087         * Creates a new version property.
088         * @param minVersion the minimum version that a consumer must support in
089         * order to successfully parse the iCalendar object
090         * @param maxVersion the maximum version that a consumer must support in
091         * order to successfully parse the iCalendar object
092         * @throws IllegalArgumentException if one of the versions strings are
093         * invalid
094         */
095        public Version(String minVersion, String maxVersion) {
096                this((minVersion == null) ? null : new VersionNumber(minVersion), (maxVersion == null) ? null : new VersionNumber(maxVersion));
097        }
098
099        private Version(VersionNumber minVersion, VersionNumber maxVersion) {
100                this.minVersion = minVersion;
101                this.maxVersion = maxVersion;
102        }
103
104        /**
105         * Creates a version property that is set to the older vCalendar version
106         * (1.0).
107         * @return the property instance
108         */
109        public static Version v1_0() {
110                return new Version(ICalVersion.V1_0);
111        }
112
113        /**
114         * Creates a version property that is set to the latest iCalendar version
115         * (2.0).
116         * @return the property instance
117         */
118        public static Version v2_0() {
119                return new Version(ICalVersion.V2_0);
120        }
121
122        /**
123         * Determines if this property is set to the older vCalendar version.
124         * @return true if the version is "1.0", false if not
125         */
126        public boolean isV1_0() {
127                return VCAL.equals(maxVersion);
128        }
129
130        /**
131         * Determines if this property is set to the latest iCalendar version.
132         * @return true if the version is "2.0", false if not
133         */
134        public boolean isV2_0() {
135                return ICAL.equals(maxVersion);
136        }
137
138        /**
139         * Gets the minimum version that a consumer must support in order to
140         * successfully parse the iCalendar object.
141         * @return the minimum version or null if not set
142         */
143        public VersionNumber getMinVersion() {
144                return minVersion;
145        }
146
147        /**
148         * Sets the minimum version that a consumer must support in order to
149         * successfully parse the iCalendar object.
150         * @param minVersion the minimum version or null to remove
151         */
152        public void setMinVersion(VersionNumber minVersion) {
153                this.minVersion = minVersion;
154        }
155
156        /**
157         * Gets the maximum version that a consumer must support in order to
158         * successfully parse the iCalendar object.
159         * @return the maximum version or null if not set
160         */
161        public VersionNumber getMaxVersion() {
162                return maxVersion;
163        }
164
165        /**
166         * Sets the maximum version that a consumer must support in order to
167         * successfully parse the iCalendar object.
168         * @param maxVersion the maximum version (this field is <b>required</b>)
169         */
170        public void setMaxVersion(VersionNumber maxVersion) {
171                this.maxVersion = maxVersion;
172        }
173
174        /**
175         * Converts this property's value to an {@link ICalVersion} enum.
176         * @return the {@link ICalVersion} enum or null if it couldn't be converted
177         */
178        public ICalVersion toICalVersion() {
179                if (minVersion == null && maxVersion != null) {
180                        return ICalVersion.get(maxVersion.toString());
181                }
182                return null;
183        }
184
185        @Override
186        protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) {
187                if (maxVersion == null) {
188                        warnings.add(Warning.validate(35));
189                }
190        }
191}