org.ocpsoft.rewrite.param.ParameterBuilder Maven / Gradle / Ivy
/*
* Copyright 2011 Lincoln Baxter, III
*
* 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 org.ocpsoft.rewrite.param;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import org.ocpsoft.rewrite.bind.Binding;
/**
* An base implementation of {@link Parameter}
*
* @author Lincoln Baxter, III
*/
public abstract class ParameterBuilder> implements
ConfigurableParameter
{
private final List bindings = new ArrayList();
private final List> transpositions = new ArrayList>();
private final List> constraints = new ArrayList>();
private Converter> converter = null;
private Validator> validator = null;
private String name;
/**
* Create a new {@link ParameterBuilder} instance with the given name.
*/
protected ParameterBuilder(String name)
{
this.name = name;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE configuredBy(ParameterConfigurator configurator)
{
if (configurator instanceof Binding)
bindsTo((Binding) configurator);
if (configurator instanceof Constraint>)
constrainedBy((Constraint) configurator);
if (configurator instanceof Converter>)
convertedBy((Converter>) configurator);
if (configurator instanceof Validator>)
validatedBy((Validator>) configurator);
if (configurator instanceof Transposition>)
transposedBy((Transposition) configurator);
return (IMPLTYPE) this;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE bindsTo(final Binding binding)
{
addOrReplaceBinding(binding);
return (IMPLTYPE) this;
}
/*
* Bindings must be added to the front of the list, since we want the
* ability to override the default binding if necessary.
*/
private void addOrReplaceBinding(final Binding binding)
{
ListIterator iterator = this.bindings.listIterator();
while(iterator.hasNext())
{
Binding current = iterator.next();
if(current.toString().equals(binding.toString())) // binding does not implements equals/hashCode
{
iterator.remove();
iterator.add(binding);
return;
}
}
this.bindings.add(0, binding);
}
@Override
public List getBindings()
{
return bindings;
}
@Override
public String getName()
{
return name;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE convertedBy(Converter> converter)
{
this.converter = converter;
return (IMPLTYPE) this;
}
@Override
public Converter> getConverter()
{
return converter;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE validatedBy(Validator> validator)
{
this.validator = validator;
return (IMPLTYPE) this;
}
@Override
public Validator> getValidator()
{
return validator;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE constrainedBy(Constraint constraint)
{
this.constraints.add(constraint);
return (IMPLTYPE) this;
}
@Override
public List> getConstraints()
{
return constraints;
}
@Override
@SuppressWarnings("unchecked")
public IMPLTYPE transposedBy(Transposition transform)
{
this.transpositions.add(transform);
return (IMPLTYPE) this;
}
@Override
public List> getTranspositions()
{
return transpositions;
}
@Override
public String toString()
{
return "ParameterBuilder [name=\"" + name + "\" -> transpositions=" + transpositions + ", constraints="
+ constraints
+ ", bindings=" + getBindings() + ", converter=" + converter + ", validator=" + validator + "]";
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy