001package ezvcard.property;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006
007import ezvcard.VCard;
008import ezvcard.VCardVersion;
009import ezvcard.ValidationWarning;
010
011/*
012 Copyright (c) 2012-2023, Michael Angstadt
013 All rights reserved.
014
015 Redistribution and use in source and binary forms, with or without
016 modification, are permitted provided that the following conditions are met: 
017
018 1. Redistributions of source code must retain the above copyright notice, this
019 list of conditions and the following disclaimer. 
020 2. Redistributions in binary form must reproduce the above copyright notice,
021 this list of conditions and the following disclaimer in the documentation
022 and/or other materials provided with the distribution. 
023
024 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
025 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
026 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
027 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
028 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
029 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
030 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
031 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
032 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
033 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
034
035 The views and conclusions contained in the software and documentation are those
036 of the authors and should not be interpreted as representing official policies, 
037 either expressed or implied, of the FreeBSD Project.
038 */
039
040/**
041 * Represents a property whose data model consists of a single Java object.
042 * @param <T> the class of the property's value
043 * @author Michael Angstadt
044 */
045public class SimpleProperty<T> extends VCardProperty {
046        protected T value;
047
048        /**
049         * Creates a valued property
050         * @param value the value
051         */
052        public SimpleProperty(T value) {
053                this.value = value;
054        }
055
056        /**
057         * Copy constructor.
058         * @param original the property to make a copy of
059         */
060        public SimpleProperty(SimpleProperty<T> original) {
061                super(original);
062                value = original.value;
063        }
064
065        /**
066         * Gets the value of this property.
067         * @return the value or null if not set
068         */
069        public T getValue() {
070                return value;
071        }
072
073        /**
074         * Sets the value of this property.
075         * @param value the value
076         */
077        public void setValue(T value) {
078                this.value = value;
079        }
080
081        @Override
082        protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
083                if (value == null) {
084                        warnings.add(new ValidationWarning(8));
085                }
086        }
087
088        @Override
089        protected Map<String, Object> toStringValues() {
090                Map<String, Object> values = new LinkedHashMap<>();
091                values.put("value", value);
092                return values;
093        }
094
095        @Override
096        public int hashCode() {
097                final int prime = 31;
098                int result = super.hashCode();
099                result = prime * result + ((value == null) ? 0 : value.hashCode());
100                return result;
101        }
102
103        @Override
104        public boolean equals(Object obj) {
105                if (this == obj) return true;
106                if (!super.equals(obj)) return false;
107                SimpleProperty<?> other = (SimpleProperty<?>) obj;
108                if (value == null) {
109                        if (other.value != null) return false;
110                } else if (!value.equals(other.value)) return false;
111                return true;
112        }
113}