CidBuilder.java
package fr.sii.ogham.email.builder;
import fr.sii.ogham.core.builder.Builder;
import fr.sii.ogham.core.builder.context.BuildContext;
import fr.sii.ogham.core.fluent.AbstractParent;
import fr.sii.ogham.core.id.generator.IdGenerator;
import fr.sii.ogham.core.id.generator.SequentialIdGenerator;
import fr.sii.ogham.email.message.Email;
/**
* Configures how images are attached to {@link Email}s.
*
* Image defined in a html must be referenced by a
* <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID (or
* CID)</a> if the image is attached to the email. You can define how CIDs are
* generated.
*
*
* @author Aurélien Baudet
*
*/
public class CidBuilder extends AbstractParent<AttachImageBuilder> implements Builder<IdGenerator> {
private final BuildContext buildContext;
private IdGenerator idGenerator;
private boolean sequential;
/**
* Initializes with the parent (used when calling {@link #and()} method for
* fluent chaining).
*
* @param parent
* the parent builder
* @param buildContext
* for registering instances and property evaluation
*/
public CidBuilder(AttachImageBuilder parent, BuildContext buildContext) {
super(parent);
this.buildContext = buildContext;
}
/**
* Image defined in a html must be referenced by a
* <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID
* (or CID)</a> if the image is attached to the email. You can define how
* CIDs are generated by defining an {@link IdGenerator}.
*
* <p>
* Use this method to provide custom CID generation strategy.
*
* <p>
* Any call to this method preempts any other configuration.
*
* <p>
* If this method is called several times, only the last provided value is
* used.
*
* <p>
* If {@code null} value is provided, it disables custom generator.
*
* @param generator
* the cid generator
* @return this instance for fluent chaining
*/
public CidBuilder generator(IdGenerator generator) {
idGenerator = generator;
return this;
}
/**
* Image defined in a html must be referenced by a
* <a href="https://tools.ietf.org/html/rfc4021#section-2.2.2">Content-ID
* (or CID)</a> if the image is attached to the email. You can define how
* CIDs are generated by defining an {@link IdGenerator}.
*
* <p>
* Enables basic sequential CID generation: 0, 1, 2, 3...
*
*
* @return this instance for fluent chaining
*/
public CidBuilder sequential() {
this.sequential = true;
return this;
}
@Override
public IdGenerator build() {
if (idGenerator != null) {
return idGenerator;
}
if (sequential) {
return buildContext.register(new SequentialIdGenerator());
}
return null;
}
}