| 1 | package ezvcard.parameter; |
| 2 | |
| 3 | import java.lang.reflect.Constructor; |
| 4 | |
| 5 | import ezvcard.VCardVersion; |
| 6 | import ezvcard.util.CaseClasses; |
| 7 | |
| 8 | /* |
| 9 | Copyright (c) 2012-2023, Michael Angstadt |
| 10 | All rights reserved. |
| 11 | |
| 12 | Redistribution and use in source and binary forms, with or without |
| 13 | modification, are permitted provided that the following conditions are met: |
| 14 | |
| 15 | 1. Redistributions of source code must retain the above copyright notice, this |
| 16 | list of conditions and the following disclaimer. |
| 17 | 2. Redistributions in binary form must reproduce the above copyright notice, |
| 18 | this list of conditions and the following disclaimer in the documentation |
| 19 | and/or other materials provided with the distribution. |
| 20 | |
| 21 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 22 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 23 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 24 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 25 | ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 26 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 27 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 28 | ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 | (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 30 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 | */ |
| 32 | |
| 33 | /** |
| 34 | * Manages the list of pre-defined values for a parameter that consists of a |
| 35 | * single String value. |
| 36 | * @author Michael Angstadt |
| 37 | * @param <T> the parameter class |
| 38 | */ |
| 39 | public class VCardParameterCaseClasses<T extends VCardParameter> extends CaseClasses<T, String> { |
| 40 | public VCardParameterCaseClasses(Class<T> clazz) { |
| 41 | super(clazz); |
| 42 | } |
| 43 | |
| 44 | @Override |
| 45 | protected T create(String value) { |
| 46 | //reflection: return new ClassName(value); |
| 47 | try { |
| 48 | //try (String) constructor |
| 49 | Constructor<T> constructor = clazz.getDeclaredConstructor(String.class); |
| 50 | constructor.setAccessible(true); |
| 51 | return constructor.newInstance(value); |
| 52 | } catch (Exception e) { |
| 53 | try { |
| 54 | //try (String, VCardVersion...) constructor |
| 55 | Constructor<T> constructor = clazz.getDeclaredConstructor(String.class, VCardVersion[].class); |
| 56 | constructor.setAccessible(true); |
| 57 | return constructor.newInstance(value, new VCardVersion[] {}); |
| 58 | } catch (Exception e2) { |
| 59 | throw new RuntimeException(e2); |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | @Override |
| 65 | protected boolean matches(T object, String value) { |
| 66 | return object.getValue().equalsIgnoreCase(value); |
| 67 | } |
| 68 | } |