Skip to content

Commit

Permalink
Merge pull request #29 from ical4j/develop
Browse files Browse the repository at this point in the history
Added vcard factory to content builder
  • Loading branch information
benfortuna authored Nov 1, 2024
2 parents dd3a6c9 + 8edde25 commit b61ee2e
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class ContentBuilder extends FactoryBuilderSupport {
}

def registerVCard() {
registerFactory('vcard', new VCardFactory())
registerFactory('entity', new EntityFactory())
}

Expand Down
58 changes: 58 additions & 0 deletions src/main/groovy/net/fortuna/ical4j/vcard/VCardFactory.groovy
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/**
* Copyright (c) 2012, Ben Fortuna
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* o Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* o Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* o Neither the name of Ben Fortuna nor the names of any other contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.fortuna.ical4j.vcard
/**
* $Id$
*
* Created on: 03/08/2009
*
* @author fortuna
*
*/
class VCardFactory extends groovy.util.AbstractFactory {

Object newInstance(FactoryBuilderSupport builder, name, value, Map attributes) throws InstantiationException,
IllegalAccessException {
VCard card
if (FactoryBuilderSupport.checkValueIsType(value, name, VCard)) {
card = value
} else {
card = []
}
return card
}

void setChild(FactoryBuilderSupport build, Object parent, Object child) {
parent.add(child)
}
}

18 changes: 18 additions & 0 deletions src/main/java/net/fortuna/ical4j/vcard/EntityContainer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package net.fortuna.ical4j.vcard;

import java.util.List;
import java.util.function.BiFunction;

/**
* Provides mutator methods for {@link Entity} collections.
Expand Down Expand Up @@ -36,4 +37,21 @@ default EntityContainer remove(Entity entity) {
default List<Entity> getEntities() {
return getEntityList().getAll();
}

/**
* A functional method used to apply an entity to a container in an undefined way.
* <p>
* For example, a null check can be introduced as follows:
* <p>
* container.with((container, entity) -> if (entity != null) container.add(entity); return container;)
*
* @param f
* @param entity
* @return
*/
default <T extends EntityContainer> T with(BiFunction<T, List<Entity>, T> f, List<Entity> entity) {
//noinspection unchecked
return f.apply((T) this, entity);
}

}
14 changes: 14 additions & 0 deletions src/main/java/net/fortuna/ical4j/vcard/EntityList.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
Expand Down Expand Up @@ -48,4 +49,17 @@ public List<Entity> getAll() {
public String toString() {
return entities.stream().map(Entity::toString).collect(Collectors.joining(Strings.LINE_SEPARATOR));
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
EntityList that = (EntityList) o;
return Objects.equals(entities, that.entities);
}

@Override
public int hashCode() {
return Objects.hashCode(entities);
}
}
74 changes: 74 additions & 0 deletions src/main/java/net/fortuna/ical4j/vcard/VCard.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,18 @@
*/
package net.fortuna.ical4j.vcard;

import net.fortuna.ical4j.model.ConstraintViolationException;
import net.fortuna.ical4j.model.Property;
import net.fortuna.ical4j.model.Prototype;
import net.fortuna.ical4j.validate.ValidationException;
import net.fortuna.ical4j.validate.ValidationResult;
import net.fortuna.ical4j.vcard.property.Uid;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;

import java.io.Serializable;
import java.util.*;
import java.util.function.BiFunction;
import java.util.stream.Collectors;

/**
Expand All @@ -53,6 +58,15 @@ public class VCard implements Serializable, Prototype<VCard>, EntityContainer {

private EntityList entities;

public static final BiFunction<VCard, List<Entity>, VCard> MERGE_ENTITIES = (c, list) -> {
list.forEach(entity -> {
if (!c.getEntities().contains(entity)) {
c.add(entity.copy());
}
});
return c;
};

/**
* Default constructor.
*/
Expand Down Expand Up @@ -98,6 +112,66 @@ public VCard copy() {
.map(Entity::copy).collect(Collectors.toList())));
}

/**
* Merge all entities from the specified vCard with this instance.
*
* @param c2 the second card to merge
* @return a vCard object containing all entities from both of the cards
*/
public VCard merge(VCard c2) {
VCard copy = this.copy();
copy.with(MERGE_ENTITIES, c2.getEntities());
return copy;
}

/**
* Splits a vCard object into distinct vCard objects for unique identifiers (UID).
*
* @return an array of vCard objects
*/
public VCard[] split() {
if (getEntities().size() <= 1) {
return new VCard[]{this};
}

final Map<Uid, VCard> cards = new HashMap<Uid, VCard>();
for (final var c : getEntities()) {
final Optional<Uid> uid = c.getUid();
if (uid.isPresent()) {
var uidCal = cards.get(uid.get());
if (uidCal == null) {
cards.put(uid.get(), new VCard(new EntityList(Collections.singletonList(c))));
} else {
uidCal.add(c);
}
}
}
return cards.values().toArray(VCard[]::new);
}

/**
* Returns a unique identifier as specified by components in the vCard object.
*
* @return the UID property
* @throws ConstraintViolationException if zero or more than one unique identifier(s) is
* found in the specified vCard
*/
public Uid getUid() throws ConstraintViolationException {
Uid uid = null;
for (final var c : entities.getAll()) {
for (final var foundUid : c.getProperties(Property.UID)) {
if (uid != null && !uid.equals(foundUid)) {
throw new ConstraintViolationException("More than one UID found in card");
}
uid = (Uid) foundUid;
}
}
if (uid == null) {
throw new ConstraintViolationException("Card must specify a single unique identifier (UID)");
}
return uid;
}

/**
* @return a vCard-compliant string representation of the vCard object
*/
Expand Down
20 changes: 11 additions & 9 deletions src/test/groovy/net/fortuna/ical4j/vcard/ContentBuilderTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -42,19 +42,21 @@ class ContentBuilderTest extends GroovyTestCase {

void testBuildCard() {
def builder = new ContentBuilder()
def entity = builder.entity {
version '4.0'
fn 'test'
n('example') {
value 'text'
def card = builder.vcard {
entity {
version '4.0'
fn 'test'
n('example') {
value 'text'
}
photo(value: 'http://example.com', parameters: [value('uri')])
}
photo(value: 'http://example.com', parameters: [value('uri')])
}
entity.validate()
card.validate()

assert entity.getPropertyList().all.size() == 4
assert card.entities[0].getPropertyList().all.size() == 4

println entity
println card
}

void testBuildEmail() {
Expand Down

0 comments on commit b61ee2e

Please sign in to comment.