use of ezvcard.util.ListMultimap in project ez-vcard by mangstadt.
the class VCard method getPropertiesAlt.
/**
* Gets all properties of a given class, grouping the alternative
* representations of each property together (see:
* {@link VCardParameters#getAltId description of ALTID})
* @param clazz the property class
* @param <T> the property class
* @return the properties (this list is immutable)
*/
public <T extends VCardProperty & HasAltId> List<List<T>> getPropertiesAlt(Class<T> clazz) {
List<T> propertiesWithoutAltIds = new ArrayList<T>();
ListMultimap<String, T> propertiesWithAltIds = new ListMultimap<String, T>();
for (T property : getProperties(clazz)) {
String altId = property.getAltId();
if (altId == null) {
propertiesWithoutAltIds.add(property);
} else {
propertiesWithAltIds.put(altId, property);
}
}
int size = propertiesWithoutAltIds.size() + propertiesWithAltIds.size();
List<List<T>> listToReturn = new ArrayList<List<T>>(size);
for (Map.Entry<String, List<T>> entry : propertiesWithAltIds) {
listToReturn.add(Collections.unmodifiableList(entry.getValue()));
}
// put properties without ALTIDs at the end
for (T property : propertiesWithoutAltIds) {
List<T> list = new ArrayList<T>(1);
list.add(property);
listToReturn.add(Collections.unmodifiableList(list));
}
return Collections.unmodifiableList(listToReturn);
}
use of ezvcard.util.ListMultimap in project ez-vcard by mangstadt.
the class XCardWriter method _write.
@Override
protected void _write(VCard vcard, List<VCardProperty> properties) throws IOException {
try {
if (!started) {
handler.startDocument();
if (!vcardsElementExists) {
// don't output a <vcards> element if the parent is a <vcards> element
start(VCARDS);
}
started = true;
}
// group the types by group name (null = no group name)
ListMultimap<String, VCardProperty> propertiesByGroup = new ListMultimap<String, VCardProperty>();
for (VCardProperty property : properties) {
propertiesByGroup.put(property.getGroup(), property);
}
start(VCARD);
for (Map.Entry<String, List<VCardProperty>> entry : propertiesByGroup) {
String groupName = entry.getKey();
if (groupName != null) {
AttributesImpl attr = new AttributesImpl();
attr.addAttribute(XCardQNames.NAMESPACE, "", "name", "", groupName);
start(GROUP, attr);
}
for (VCardProperty property : entry.getValue()) {
write(property, vcard);
}
if (groupName != null) {
end(GROUP);
}
}
end(VCARD);
} catch (SAXException e) {
throw new IOException(e);
}
}
Aggregations