001package biweekly.property;
002
003import java.util.Arrays;
004import java.util.Collection;
005import java.util.Collections;
006import java.util.List;
007
008import biweekly.ICalVersion;
009import biweekly.Warning;
010import biweekly.component.ICalComponent;
011
012/*
013 Copyright (c) 2013-2015, Michael Angstadt
014 All rights reserved.
015
016 Redistribution and use in source and binary forms, with or without
017 modification, are permitted provided that the following conditions are met: 
018
019 1. Redistributions of source code must retain the above copyright notice, this
020 list of conditions and the following disclaimer. 
021 2. Redistributions in binary form must reproduce the above copyright notice,
022 this list of conditions and the following disclaimer in the documentation
023 and/or other materials provided with the distribution. 
024
025 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
026 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
027 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
028 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
029 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
030 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
031 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
032 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
033 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
034 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
035 */
036
037/**
038 * Represents a property that has a defined set of acceptable values (for
039 * example, the {@link Action} property).
040 * @author Michael Angstadt
041 */
042public abstract class EnumProperty extends TextProperty {
043        /**
044         * Creates an enum property.
045         * @param value the property value
046         */
047        public EnumProperty(String value) {
048                super(value);
049        }
050
051        /**
052         * Compares the property's value with a given string (case-insensitive).
053         * @param value the string
054         * @return true if it's equal, false if not
055         */
056        protected boolean is(String value) {
057                return value.equalsIgnoreCase(this.value);
058        }
059
060        /**
061         * Gets the list of acceptable values for this property.
062         * @param version the version
063         * @return the list of acceptable values
064         */
065        protected abstract Collection<String> getStandardValues(ICalVersion version);
066
067        /**
068         * Gets the iCalendar versions that this property's value is supported in.
069         * @return the supported versions
070         */
071        protected Collection<ICalVersion> getValueSupportedVersions() {
072                if (value == null) {
073                        return Collections.emptyList();
074                }
075                return Arrays.asList(ICalVersion.values());
076        }
077
078        @Override
079        protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) {
080                super.validate(components, version, warnings);
081                if (value == null) {
082                        return;
083                }
084
085                Collection<ICalVersion> supportedVersions = getValueSupportedVersions();
086                if (supportedVersions.isEmpty()) {
087                        //it's a non-standard value
088                        warnings.add(Warning.validate(28, value, getStandardValues(version)));
089                        return;
090                }
091
092                boolean supported = supportedVersions.contains(version);
093                if (!supported) {
094                        warnings.add(Warning.validate(46, value, supportedVersions));
095                }
096        }
097}