001package biweekly.property;
002
003import java.util.ArrayList;
004import java.util.Arrays;
005import java.util.LinkedHashMap;
006import java.util.List;
007import java.util.Map;
008
009import biweekly.ICalVersion;
010import biweekly.Messages;
011import biweekly.Warning;
012import biweekly.component.ICalComponent;
013
014/*
015 Copyright (c) 2013-2016, 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 * Represents a property whose value is a list of values.
041 * @author Michael Angstadt
042 * @param <T> the value type
043 */
044public class ListProperty<T> extends ICalProperty {
045        protected final List<T> values;
046
047        /**
048         * Creates a new list property.
049         */
050        public ListProperty() {
051                values = new ArrayList<T>();
052        }
053
054        /**
055         * Creates a new list property.
056         * @param values the values to initialize the property with
057         */
058        public ListProperty(T... values) {
059                this.values = new ArrayList<T>(Arrays.asList(values));
060        }
061
062        /**
063         * Creates a new list property.
064         * @param values the values to initialize the property with (cannot be null)
065         */
066        public ListProperty(List<T> values) {
067                if (values == null) {
068                        throw new NullPointerException(Messages.INSTANCE.getExceptionMessage(18));
069                }
070                this.values = values;
071        }
072
073        /**
074         * Copy constructor.
075         * @param original the property to make a copy of
076         */
077        public ListProperty(ListProperty<T> original) {
078                super(original);
079                values = new ArrayList<T>(original.values);
080        }
081
082        /**
083         * Gets the list that holds the values of this property.
084         * @return the values list (this list is mutable)
085         */
086        public List<T> getValues() {
087                return values;
088        }
089
090        @Override
091        protected void validate(List<ICalComponent> components, ICalVersion version, List<Warning> warnings) {
092                if (values.isEmpty()) {
093                        warnings.add(Warning.validate(26));
094                }
095        }
096
097        @Override
098        protected Map<String, Object> toStringValues() {
099                Map<String, Object> values = new LinkedHashMap<String, Object>();
100                values.put("values", this.values);
101                return values;
102        }
103
104        @Override
105        public int hashCode() {
106                final int prime = 31;
107                int result = super.hashCode();
108                result = prime * result + values.hashCode();
109                return result;
110        }
111
112        @Override
113        public boolean equals(Object obj) {
114                if (this == obj) return true;
115                if (!super.equals(obj)) return false;
116                ListProperty<?> other = (ListProperty<?>) obj;
117                if (!values.equals(other.values)) return false;
118                return true;
119        }
120}