001package ezvcard.property;
002
003import java.util.LinkedHashMap;
004import java.util.List;
005import java.util.Map;
006import java.util.Objects;
007
008import ezvcard.VCard;
009import ezvcard.VCardVersion;
010import ezvcard.ValidationWarning;
011
012/*
013 Copyright (c) 2012-2026, 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 The views and conclusions contained in the software and documentation are those
037 of the authors and should not be interpreted as representing official policies, 
038 either expressed or implied, of the FreeBSD Project.
039 */
040
041/**
042 * Represents a property whose data model consists of a single Java object.
043 * @param <T> the class of the property's value
044 * @author Michael Angstadt
045 */
046public class SimpleProperty<T> extends VCardProperty {
047        protected T value;
048
049        /**
050         * Creates a valued property
051         * @param value the value
052         */
053        public SimpleProperty(T value) {
054                this.value = value;
055        }
056
057        /**
058         * Copy constructor.
059         * @param original the property to make a copy of
060         */
061        public SimpleProperty(SimpleProperty<T> original) {
062                super(original);
063                value = original.value;
064        }
065
066        /**
067         * Gets the value of this property.
068         * @return the value or null if not set
069         */
070        public T getValue() {
071                return value;
072        }
073
074        /**
075         * Sets the value of this property.
076         * @param value the value
077         */
078        public void setValue(T value) {
079                this.value = value;
080        }
081
082        @Override
083        protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) {
084                if (value == null) {
085                        warnings.add(new ValidationWarning(8));
086                }
087        }
088
089        @Override
090        protected Map<String, Object> toStringValues() {
091                Map<String, Object> values = new LinkedHashMap<>();
092                values.put("value", value);
093                return values;
094        }
095
096        @Override
097        public int hashCode() {
098                final int prime = 31;
099                int result = super.hashCode();
100                result = prime * result + Objects.hash(value);
101                return result;
102        }
103
104        @Override
105        public boolean equals(Object obj) {
106                if (this == obj) return true;
107                if (!super.equals(obj)) return false;
108                if (getClass() != obj.getClass()) return false;
109                SimpleProperty<?> other = (SimpleProperty<?>) obj;
110                return Objects.equals(value, other.value);
111        }
112}