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