001package ezvcard.parameter; 002 003import java.lang.reflect.Field; 004import java.lang.reflect.Modifier; 005 006import ezvcard.SupportedVersions; 007import ezvcard.VCardVersion; 008 009/* 010 Copyright (c) 2012-2023, Michael Angstadt 011 All rights reserved. 012 013 Redistribution and use in source and binary forms, with or without 014 modification, are permitted provided that the following conditions are met: 015 016 1. Redistributions of source code must retain the above copyright notice, this 017 list of conditions and the following disclaimer. 018 2. Redistributions in binary form must reproduce the above copyright notice, 019 this list of conditions and the following disclaimer in the documentation 020 and/or other materials provided with the distribution. 021 022 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 023 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 024 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 025 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 026 ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 027 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 028 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 029 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 030 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 031 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 032 033 The views and conclusions contained in the software and documentation are those 034 of the authors and should not be interpreted as representing official policies, 035 either expressed or implied, of the FreeBSD Project. 036 */ 037 038/** 039 * Represents a vCard parameter (aka "sub type") whose values are pre-defined. 040 * @author Michael Angstadt 041 */ 042public class VCardParameter { 043 /** 044 * The value (for example, "home"). 045 */ 046 protected final String value; 047 048 /** 049 * Creates a new parameter. 050 * @param value the value 051 */ 052 public VCardParameter(String value) { 053 this(value, false); 054 } 055 056 /** 057 * Creates a new parameter. 058 * @param value the value 059 * @param preserveCase true to preserve the case of the value, false convert 060 * it to lower-case 061 */ 062 protected VCardParameter(String value, boolean preserveCase) { 063 this.value = (value == null || preserveCase) ? value : value.toLowerCase(); 064 } 065 066 /** 067 * Gets the value of the parameter. 068 * @return the value of the parameter (e.g. "home") 069 */ 070 public String getValue() { 071 return value; 072 } 073 074 /** 075 * <p> 076 * Gets the vCard versions that support this parameter value. 077 * </p> 078 * <p> 079 * The supported versions are defined by assigning a 080 * {@link SupportedVersions} annotation to the parameter value's static 081 * field (for example, {@link AddressType#DOM}). Dynamically-created 082 * parameter values (i.e. non-standard values) are considered to be 083 * supported by all versions. 084 * </p> 085 * @return the vCard versions that support this parameter. 086 */ 087 public VCardVersion[] getSupportedVersions() { 088 for (Field field : getClass().getFields()) { 089 if (!Modifier.isStatic(field.getModifiers())) { 090 continue; 091 } 092 093 Object fieldValue; 094 try { 095 fieldValue = field.get(null); 096 } catch (IllegalArgumentException e) { 097 //should never be thrown because we check for the static modified 098 continue; 099 } catch (IllegalAccessException e) { 100 continue; 101 } 102 103 if (fieldValue == this) { 104 SupportedVersions supportedVersionsAnnotation = field.getAnnotation(SupportedVersions.class); 105 return (supportedVersionsAnnotation == null) ? VCardVersion.values() : supportedVersionsAnnotation.value(); 106 } 107 } 108 109 return VCardVersion.values(); 110 } 111 112 /** 113 * <p> 114 * Determines if this parameter value is supported by the given vCard 115 * version. 116 * </p> 117 * <p> 118 * The supported versions are defined by assigning a 119 * {@link SupportedVersions} annotation to the parameter value's static 120 * field (for example, {@link AddressType#DOM}). Dynamically-created 121 * parameter values (i.e. non-standard values) are considered to be 122 * supported by all versions. 123 * </p> 124 * @param version the vCard version 125 * @return true if it is supported, false if not 126 */ 127 public boolean isSupportedBy(VCardVersion version) { 128 for (VCardVersion supportedVersion : getSupportedVersions()) { 129 if (supportedVersion == version) { 130 return true; 131 } 132 } 133 return false; 134 } 135 136 @Override 137 public String toString() { 138 return value; 139 } 140 141 @Override 142 public int hashCode() { 143 final int prime = 31; 144 int result = 1; 145 result = prime * result + ((value == null) ? 0 : value.hashCode()); 146 return result; 147 } 148 149 @Override 150 public boolean equals(Object obj) { 151 if (this == obj) return true; 152 if (obj == null) return false; 153 if (getClass() != obj.getClass()) return false; 154 VCardParameter other = (VCardParameter) obj; 155 if (value == null) { 156 if (other.value != null) return false; 157 } else if (!value.equals(other.value)) return false; 158 return true; 159 } 160}