Java tutorial
/** * Copyright 2011 Caleb Richardson * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.outerspacecat.icalendar; import java.io.Serializable; import java.util.ArrayDeque; import java.util.Deque; import javax.annotation.concurrent.Immutable; import javax.annotation.concurrent.ThreadSafe; import com.google.common.base.Function; import com.google.common.base.Objects; import com.google.common.base.Preconditions; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; /** * A representation of an iCalendar attendee property defined by <a * href="http://tools.ietf.org/html/rfc5545">RFC 5545</a>. * * @author Caleb Richardson */ @Immutable @ThreadSafe public final class Attendee implements HasProperty, Serializable { private final static long serialVersionUID = 1L; private final static ImmutableSet<String> RESTRICTED_PARAMETER_NAMES = ImmutableSet.of("CUTYPE", "MEMBER", "ROLE", "PARTSTAT", "RSVP", "DELEGATED-TO", "DELEGATED-FROM", "SENT-BY", "CN", "DIR", "LANGUAGE"); private final UriType address; private final ParameterValue calendarUserType; private final ImmutableSet<UriType> memberships; private final ParameterValue role; private final ParameterValue participationStatus; private final boolean rsvpExpected; private final ImmutableSet<UriType> delegatees; private final ImmutableSet<UriType> delegators; private final UriType sentBy; private final ParameterValue commonName; private final UriType directoryEntry; private final ParameterValue language; private final ImmutableMap<String, Parameter> extraParameters; /** * Creates a new attendee. * * @param address the address. Must be non {@code null}. * @param calendarUserType the calendar user type of the attendee. May be * {@code null}. * @param memberships the memberships of the attendee. May be {@code null} or * empty. * @param role the role of the attendee. May be {@code null}. * @param participationStatus the participation status of the attendee. May be * {@code null}. * @param rsvpExpected whether or not the attendee is expected to RSVP * @param delegatees the calendar users to whom the attendee has delegated * participation. May be {@code null} or empty. * @param delegators the calendar users who have delegated their participation * to the attendee. May be {@code null} or empty. * @param sentBy the calendar user acting on behalf of the attendee. May be * {@code null}. * @param commonName the name of the attendee. May be {@code null}. * @param directoryEntry the directory entry of the attendee. May be * {@code null}. * @param language the language of the attendee. May be {@code null}. * @param extraParameters the extra parameters of the attendee. Must be non * {@code null}, all elements must be non {@code null}, may be empty. */ public Attendee(final UriType address, final ParameterValue calendarUserType, final Iterable<UriType> memberships, final ParameterValue role, final ParameterValue participationStatus, final boolean rsvpExpected, final Iterable<UriType> delegatees, final Iterable<UriType> delegators, final UriType sentBy, final ParameterValue commonName, final UriType directoryEntry, final ParameterValue language, final Iterable<Parameter> extraParameters) { Preconditions.checkNotNull(address, "address required"); Preconditions.checkNotNull(extraParameters, "extraParameters required"); this.address = address; this.calendarUserType = calendarUserType; this.memberships = memberships != null ? ImmutableSet.copyOf(memberships) : ImmutableSet.<UriType>of(); this.role = role; this.participationStatus = participationStatus; this.rsvpExpected = rsvpExpected; this.delegatees = delegatees != null ? ImmutableSet.copyOf(delegatees) : ImmutableSet.<UriType>of(); this.delegators = delegators != null ? ImmutableSet.copyOf(delegators) : ImmutableSet.<UriType>of(); this.sentBy = sentBy; this.commonName = commonName; this.directoryEntry = directoryEntry; this.language = language; ImmutableMap.Builder<String, Parameter> extraParametersBuilder = ImmutableMap.builder(); for (Parameter param : extraParameters) extraParametersBuilder.put(param.getName(), param); this.extraParameters = extraParametersBuilder.build(); } /** * Parses an attendee property. * * @param property the property to parse. Must be non {@code null}. * @return an attendee. Never {@code null}. * @throws CalendarParseException if {@code property} cannot be parsed */ public static Attendee parse(final Property property) throws CalendarParseException { Preconditions.checkNotNull(property, "property required"); Preconditions.checkArgument(property.getName().getName().equals("ATTENDEE"), "property name must be ATTENDEE, saw: " + property.getName()); UriType address = property.asUri(); ParameterValue calendarUserType = property.getParameterValue("CUTYPE"); ImmutableSet.Builder<UriType> memberships = ImmutableSet.builder(); Parameter memberParam = property.getParameters().get("MEMBER"); if (memberParam != null) { for (ParameterValue value : memberParam.getValues()) memberships.add(UriType.parse(value.getValue())); } ParameterValue role = property.getParameterValue("ROLE"); ParameterValue participationStatus = property.getParameterValue("PARTSTAT"); ParameterValue rsvpParam = property.getParameterValue("RSVP"); boolean rsvpExpected = rsvpParam != null && rsvpParam.getValue().equalsIgnoreCase("TRUE"); ImmutableSet.Builder<UriType> delegatees = ImmutableSet.builder(); Parameter delegatedToParam = property.getParameters().get("DELEGATED-TO"); if (delegatedToParam != null) { for (ParameterValue value : delegatedToParam.getValues()) delegatees.add(UriType.parse(value.getValue())); } ImmutableSet.Builder<UriType> delegators = ImmutableSet.builder(); Parameter delegatedFromParam = property.getParameters().get("DELEGATED-FROM"); if (delegatedFromParam != null) { for (ParameterValue value : delegatedFromParam.getValues()) delegators.add(UriType.parse(value.getValue())); } ParameterValue sentByParam = property.getParameterValue("SENT-BY"); UriType sentBy = sentByParam != null ? UriType.parse(sentByParam.getValue()) : null; ParameterValue commonName = property.getParameterValue("CN"); ParameterValue dirParam = property.getParameterValue("DIR"); UriType directoryEntry = dirParam != null ? UriType.parse(dirParam.getValue()) : null; ParameterValue language = property.getParameterValue("LANGUAGE"); return new Attendee(address, calendarUserType, memberships.build(), role, participationStatus, rsvpExpected, delegatees.build(), delegators.build(), sentBy, commonName, directoryEntry, language, property.getParametersExcept(RESTRICTED_PARAMETER_NAMES).values()); } /** * Returns the address of this attendee. * * @return the address of this attendee. Never {@code null}. */ public UriType getAddress() { return address; } /** * Returns the calendar user type of this attendee. * * @return the calendar user type of this attendee. May be {@code null}. */ public ParameterValue getCalendarUserType() { return calendarUserType; } /** * Returns the memberships of this attendee. * * @return the memberships of this attendee. Never {@code null}, may be empty. */ public ImmutableSet<UriType> getMemberships() { return memberships; } /** * Returns the role of this attendee. * * @return the role of this attendee. May be {@code null}. */ public ParameterValue getRole() { return role; } /** * Returns the participation status of this attendee. * * @return the participation status of this attendee. May be {@code null}. */ public ParameterValue getParticipationStatus() { return participationStatus; } /** * Returns the whether or not this attendee is expected to RSVP. * * @return whether or not this attendee is expected to RSVP */ public boolean isRsvpExpected() { return rsvpExpected; } /** * Returns the calendar users to whom this attendee has delegated * participation. * * @return the calendar users to whom this attendee has delegated * participation. Never {@code null}, may be empty. */ public ImmutableSet<UriType> getDelegatees() { return delegatees; } /** * Returns the calendar users who have delegated their participation to this * attendee. * * @return the calendar users who have delegated their participation to this * attendeee. Never {@code null}, may be empty. */ public ImmutableSet<UriType> getDelegators() { return delegators; } /** * Returns the calendar user acting on behalf of this attendee. * * @return the calendar user acting on behalf of this attendee. May be * {@code null}. */ public UriType getSentBy() { return sentBy; } /** * Returns the name of this attendee. * * @return the name of this attendee. May be {@code null}. */ public ParameterValue getCommonName() { return commonName; } /** * Returns the directory entry of this attendee. * * @return the directory entry of this attendee. May be {@code null}. */ public UriType getDirectoryEntry() { return directoryEntry; } /** * Returns the language of this attendee. * * @return the language of this attendee. May be {@code null}. */ public ParameterValue getLanguage() { return language; } /** * Returns the extra parameters of this attendee. * * @return the extra parameters of this attendeee. Never {@code null}, may be * empty. */ public ImmutableMap<String, Parameter> getExtraParameters() { return extraParameters; } @Override public int hashCode() { return Objects.hashCode(getAddress()); } @Override public boolean equals(final Object obj) { return obj instanceof Attendee && Objects.equal(getAddress(), ((Attendee) obj).getAddress()); } @Override public String toString() { return toProperty(HasTimeZones.IGNORE).toICalendar(); } @Override public Property toProperty(final HasTimeZones timeZones) { Preconditions.checkNotNull(timeZones, "timeZones required"); Deque<Parameter> params = new ArrayDeque<Parameter>(); if (getCalendarUserType() != null) params.add(new Parameter("CUTYPE", ImmutableSet.of(getCalendarUserType()))); Function<UriType, ParameterValue> uriMapper = new Function<UriType, ParameterValue>() { @Override public ParameterValue apply(final UriType uri) { return new ParameterValue(uri.toICalendar()); } }; if (!getMemberships().isEmpty()) params.add(new Parameter("MEMBER", Iterables.transform(getMemberships(), uriMapper))); if (getRole() != null) params.add(new Parameter("ROLE", ImmutableSet.of(getRole()))); if (getParticipationStatus() != null) params.add(new Parameter("PARTSTAT", ImmutableSet.of(getParticipationStatus()))); params.add(new Parameter("RSVP", ImmutableSet.of(new ParameterValue(isRsvpExpected() ? "TRUE" : "FALSE")))); if (!getDelegatees().isEmpty()) params.add(new Parameter("DELEGATED-TO", Iterables.transform(getDelegatees(), uriMapper))); if (!getDelegators().isEmpty()) params.add(new Parameter("DELEGATED-FROM", Iterables.transform(getDelegators(), uriMapper))); if (getSentBy() != null) params.add(new Parameter("SENT-BY", ImmutableSet.of(new ParameterValue(getSentBy().toICalendar())))); if (getCommonName() != null) params.add(new Parameter("CN", ImmutableSet.of(getCommonName()))); if (getDirectoryEntry() != null) params.add( new Parameter("DIR", ImmutableSet.of(new ParameterValue(getDirectoryEntry().toICalendar())))); if (getLanguage() != null) params.add(new Parameter("LANGUAGE", ImmutableSet.of(getLanguage()))); params.addAll(getExtraParameters().values()); return new Property(new PropertyName("ATTENDEE"), getAddress().toICalendar(), params); } }