| 1 | package ezvcard.property; |
| 2 | |
| 3 | import java.util.LinkedHashMap; |
| 4 | import java.util.List; |
| 5 | import java.util.Map; |
| 6 | |
| 7 | import ezvcard.VCard; |
| 8 | import ezvcard.VCardVersion; |
| 9 | import ezvcard.ValidationWarning; |
| 10 | |
| 11 | /* |
| 12 | Copyright (c) 2012-2023, Michael Angstadt |
| 13 | All rights reserved. |
| 14 | |
| 15 | Redistribution and use in source and binary forms, with or without |
| 16 | modification, are permitted provided that the following conditions are met: |
| 17 | |
| 18 | 1. Redistributions of source code must retain the above copyright notice, this |
| 19 | list of conditions and the following disclaimer. |
| 20 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 21 | this list of conditions and the following disclaimer in the documentation |
| 22 | and/or other materials provided with the distribution. |
| 23 | |
| 24 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 25 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 26 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 27 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 28 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 29 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 30 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 31 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 32 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 33 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 34 | |
| 35 | The views and conclusions contained in the software and documentation are those |
| 36 | of the authors and should not be interpreted as representing official policies, |
| 37 | either expressed or implied, of the FreeBSD Project. |
| 38 | */ |
| 39 | |
| 40 | /** |
| 41 | * Represents a property whose data model consists of a single Java object. |
| 42 | * @param <T> the class of the property's value |
| 43 | * @author Michael Angstadt |
| 44 | */ |
| 45 | public class SimpleProperty<T> extends VCardProperty { |
| 46 | protected T value; |
| 47 | |
| 48 | /** |
| 49 | * Creates a valued property |
| 50 | * @param value the value |
| 51 | */ |
| 52 | public SimpleProperty(T value) { |
| 53 | this.value = value; |
| 54 | } |
| 55 | |
| 56 | /** |
| 57 | * Copy constructor. |
| 58 | * @param original the property to make a copy of |
| 59 | */ |
| 60 | public SimpleProperty(SimpleProperty<T> original) { |
| 61 | super(original); |
| 62 | value = original.value; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Gets the value of this property. |
| 67 | * @return the value or null if not set |
| 68 | */ |
| 69 | public T getValue() { |
| 70 | return value; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Sets the value of this property. |
| 75 | * @param value the value |
| 76 | */ |
| 77 | public void setValue(T value) { |
| 78 | this.value = value; |
| 79 | } |
| 80 | |
| 81 | @Override |
| 82 | protected void _validate(List<ValidationWarning> warnings, VCardVersion version, VCard vcard) { |
| 83 | if (value == null) { |
| 84 | warnings.add(new ValidationWarning(8)); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | protected Map<String, Object> toStringValues() { |
| 90 | Map<String, Object> values = new LinkedHashMap<>(); |
| 91 | values.put("value", value); |
| 92 | return values; |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public int hashCode() { |
| 97 | final int prime = 31; |
| 98 | int result = super.hashCode(); |
| 99 | 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 | } |