Search in sources :

Example 1 with ListMultimap

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);
}
Also used : ArrayList(java.util.ArrayList) AbstractList(java.util.AbstractList) List(java.util.List) ArrayList(java.util.ArrayList) SortString(ezvcard.property.SortString) ListMultimap(ezvcard.util.ListMultimap) Map(java.util.Map) ClientPidMap(ezvcard.property.ClientPidMap)

Example 2 with ListMultimap

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);
    }
}
Also used : AttributesImpl(org.xml.sax.helpers.AttributesImpl) VCardProperty(ezvcard.property.VCardProperty) NodeList(org.w3c.dom.NodeList) List(java.util.List) IOException(java.io.IOException) ListMultimap(ezvcard.util.ListMultimap) Map(java.util.Map) NamedNodeMap(org.w3c.dom.NamedNodeMap) SAXException(org.xml.sax.SAXException)

Aggregations

ListMultimap (ezvcard.util.ListMultimap)2 List (java.util.List)2 Map (java.util.Map)2 ClientPidMap (ezvcard.property.ClientPidMap)1 SortString (ezvcard.property.SortString)1 VCardProperty (ezvcard.property.VCardProperty)1 IOException (java.io.IOException)1 AbstractList (java.util.AbstractList)1 ArrayList (java.util.ArrayList)1 NamedNodeMap (org.w3c.dom.NamedNodeMap)1 NodeList (org.w3c.dom.NodeList)1 SAXException (org.xml.sax.SAXException)1 AttributesImpl (org.xml.sax.helpers.AttributesImpl)1