| 1 | package ezvcard.io.chain; |
| 2 | |
| 3 | import java.util.Collection; |
| 4 | |
| 5 | import ezvcard.VCard; |
| 6 | import ezvcard.io.scribe.ScribeIndex; |
| 7 | import ezvcard.io.scribe.VCardPropertyScribe; |
| 8 | import ezvcard.property.ProductId; |
| 9 | import ezvcard.property.VCardProperty; |
| 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 | |
| 36 | /** |
| 37 | * Parent class for all chaining writers. This class is package-private in order |
| 38 | * to hide it from the generated Javadocs. |
| 39 | * @author Michael Angstadt |
| 40 | * @param <T> the object instance's type (for method chaining) |
| 41 | */ |
| 42 | class ChainingWriter<T extends ChainingWriter<?>> { |
| 43 | final Collection<VCard> vcards; |
| 44 | ScribeIndex index; |
| 45 | boolean prodId = true; |
| 46 | boolean versionStrict = true; |
| 47 | |
| 48 | @SuppressWarnings("unchecked") |
| 49 | private final T this_ = (T) this; |
| 50 | |
| 51 | /** |
| 52 | * @param vcards the vCards to write |
| 53 | */ |
| 54 | ChainingWriter(Collection<VCard> vcards) { |
| 55 | this.vcards = vcards; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Sets whether to exclude properties that do not support the target version |
| 60 | * from the written vCard. |
| 61 | * @param versionStrict true to exclude such properties, false not to |
| 62 | * (defaults to true) |
| 63 | * @return this |
| 64 | */ |
| 65 | T versionStrict(boolean versionStrict) { |
| 66 | this.versionStrict = versionStrict; |
| 67 | return this_; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Sets whether to add a {@link ProductId} property to each vCard that marks |
| 72 | * it as having been generated by this library. For 2.1 vCards, the extended |
| 73 | * property "X-PRODID" will be added, since {@link ProductId} is not |
| 74 | * supported by that version. |
| 75 | * @param include true to add the property, false not to (defaults to true) |
| 76 | * @return this |
| 77 | */ |
| 78 | T prodId(boolean include) { |
| 79 | this.prodId = include; |
| 80 | return this_; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Registers a property scribe. |
| 85 | * @param scribe the scribe to register |
| 86 | * @return this |
| 87 | */ |
| 88 | T register(VCardPropertyScribe<? extends VCardProperty> scribe) { |
| 89 | if (index == null) { |
| 90 | index = new ScribeIndex(); |
| 91 | } |
| 92 | index.register(scribe); |
| 93 | return this_; |
| 94 | } |
| 95 | } |