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