| 1 | package ezvcard.parameter; |
| 2 | |
| 3 | import ezvcard.property.ClientPidMap; |
| 4 | |
| 5 | /* |
| 6 | Copyright (c) 2012-2023, Michael Angstadt |
| 7 | All rights reserved. |
| 8 | |
| 9 | Redistribution and use in source and binary forms, with or without |
| 10 | modification, are permitted provided that the following conditions are met: |
| 11 | |
| 12 | 1. Redistributions of source code must retain the above copyright notice, this |
| 13 | list of conditions and the following disclaimer. |
| 14 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 15 | this list of conditions and the following disclaimer in the documentation |
| 16 | and/or other materials provided with the distribution. |
| 17 | |
| 18 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 19 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 20 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 21 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 22 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 23 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 24 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 25 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 26 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 28 | |
| 29 | The views and conclusions contained in the software and documentation are those |
| 30 | of the authors and should not be interpreted as representing official policies, |
| 31 | either expressed or implied, of the FreeBSD Project. |
| 32 | */ |
| 33 | |
| 34 | /** |
| 35 | * Represents a PID parameter value. A PID uniquely identifies a property. They |
| 36 | * are used when two different versions of the same vCard have to be merged |
| 37 | * together (called "synchronizing"). |
| 38 | * @author Michael Angstadt |
| 39 | * @see <a href="http://tools.ietf.org/html/rfc6350#section-5.5">RFC 6350 |
| 40 | * Section 5.5</a> |
| 41 | */ |
| 42 | public class Pid { |
| 43 | private final Integer localId, clientPidMapReference; |
| 44 | |
| 45 | /** |
| 46 | * Creates a new PID. |
| 47 | * @param localId the local ID (cannot be null, must be positive) |
| 48 | * @param clientPidMapReference an integer that references the property's |
| 49 | * globally unique ID (optional, must be positive). It must match the first |
| 50 | * value in an existing {@link ClientPidMap} property |
| 51 | * @throws NullPointerException if local ID is null |
| 52 | */ |
| 53 | public Pid(Integer localId, Integer clientPidMapReference) { |
| 54 | if (localId == null) { |
| 55 | throw new NullPointerException("Local ID must not be null."); |
| 56 | } |
| 57 | this.localId = localId; |
| 58 | this.clientPidMapReference = clientPidMapReference; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Creates a new PID. |
| 63 | * @param localId the local ID (cannot be null, must be positive) |
| 64 | * @throws NullPointerException if local ID is null |
| 65 | */ |
| 66 | public Pid(Integer localId) { |
| 67 | this(localId, null); |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Gets the local ID |
| 72 | * @return the local ID |
| 73 | */ |
| 74 | public Integer getLocalId() { |
| 75 | return localId; |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Gets the reference to the property's globally unique ID (stored in a |
| 80 | * {@link ClientPidMap} property). |
| 81 | * @return the reference ID or null if not set |
| 82 | */ |
| 83 | public Integer getClientPidMapReference() { |
| 84 | return clientPidMapReference; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Parses a string into a {@link Pid} object. |
| 89 | * @param value the string value (e.g. "1.2") |
| 90 | * @return the {@link Pid} object |
| 91 | * @throws NumberFormatException if there's a problem parsing the string |
| 92 | */ |
| 93 | public static Pid valueOf(String value) { |
| 94 | int dot = value.indexOf('.'); |
| 95 | String localIdStr, clientPidMapReferenceStr; |
| 96 | if (dot < 0) { |
| 97 | localIdStr = value; |
| 98 | clientPidMapReferenceStr = null; |
| 99 | } else { |
| 100 | localIdStr = value.substring(0, dot); |
| 101 | clientPidMapReferenceStr = (dot == value.length() - 1) ? null : value.substring(dot + 1); |
| 102 | } |
| 103 | |
| 104 | Integer localId = Integer.valueOf(localIdStr); |
| 105 | Integer clientPidMapReference = (clientPidMapReferenceStr == null) ? null : Integer.valueOf(clientPidMapReferenceStr); |
| 106 | return new Pid(localId, clientPidMapReference); |
| 107 | } |
| 108 | |
| 109 | @Override |
| 110 | public String toString() { |
| 111 | return (clientPidMapReference == null) ? Integer.toString(localId) : localId + "." + clientPidMapReference; |
| 112 | } |
| 113 | |
| 114 | @Override |
| 115 | public int hashCode() { |
| 116 | final int prime = 31; |
| 117 | int result = 1; |
| 118 | result = prime * result + ((clientPidMapReference == null) ? 0 : clientPidMapReference.hashCode()); |
| 119 | result = prime * result + localId.hashCode(); |
| 120 | return result; |
| 121 | } |
| 122 | |
| 123 | @Override |
| 124 | public boolean equals(Object obj) { |
| 125 | if (this == obj) return true; |
| 126 | if (obj == null) return false; |
| 127 | if (getClass() != obj.getClass()) return false; |
| 128 | Pid other = (Pid) obj; |
| 129 | if (clientPidMapReference == null) { |
| 130 | if (other.clientPidMapReference != null) return false; |
| 131 | } else if (!clientPidMapReference.equals(other.clientPidMapReference)) return false; |
| 132 | if (!localId.equals(other.localId)) return false; |
| 133 | return true; |
| 134 | } |
| 135 | } |