com.enonic.xp.resource.ResourceProcessor Maven / Gradle / Ivy
package com.enonic.xp.resource;
import java.util.function.Function;
import com.google.common.base.Preconditions;
public final class ResourceProcessor
{
private final K key;
private final String segment;
private final Function keyTranslator;
private final Function processor;
@SuppressWarnings("unchecked")
private ResourceProcessor( final Builder builder )
{
this.key = (K) builder.key;
this.segment = builder.segment;
this.keyTranslator = builder.keyTranslator;
this.processor = builder.processor;
}
public K getKey()
{
return this.key;
}
public String getSegment()
{
return this.segment;
}
public ResourceKey toResourceKey()
{
return this.keyTranslator.apply( this.key );
}
public V process( final Resource resource )
{
if ( !resource.exists() )
{
return null;
}
return this.processor.apply( resource );
}
public static final class Builder
{
private K key;
private String segment;
private Function keyTranslator;
private Function processor;
public Builder key( final K key )
{
this.key = key;
return this;
}
public Builder segment( final String segment )
{
this.segment = segment;
return this;
}
public Builder keyTranslator( final Function keyTranslator )
{
this.keyTranslator = keyTranslator;
return this;
}
public Builder processor( final Function processor )
{
this.processor = processor;
return this;
}
public ResourceProcessor build()
{
Preconditions.checkNotNull( this.key, "key is required" );
Preconditions.checkNotNull( this.segment, "segment is required" );
Preconditions.checkNotNull( this.keyTranslator, "keyTranslator is required" );
Preconditions.checkNotNull( this.processor, "processor is required" );
return new ResourceProcessor<>( this );
}
}
}