Skip to content

Commit

Permalink
#160: added sub-packages and sorted new validators into sub-packages …
Browse files Browse the repository at this point in the history
…to prevent chaos

#168: implemented builders
  • Loading branch information
hohwille committed Jan 5, 2016
1 parent f5dbabb commit 6b72775
Show file tree
Hide file tree
Showing 28 changed files with 988 additions and 32 deletions.
24 changes: 24 additions & 0 deletions mmm-util-core/src/main/java/net/sf/mmm/util/lang/api/Builder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.lang.api;

/**
* This is the generic interface for a <em>builder</em>. A builder is an object following the builder-pattern in order
* to {@link #build() build} some object using a simple and fluent API.
*
* @param <T> the type of the object to build.
*
* @author hohwille
* @since 7.1.0
*/
public interface Builder<T> {

/**
* Creates a new instance of the object to build. If the {@link Builder} is reused, any additional changes to the
* {@link Builder} shall NOT have any effect on instances previously returned by this method.
*
* @return the object to build.
*/
T build();

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

/**
* This is the abstract base implementation of {@link ValidatorBuilder}.
*
*
* @author Joerg Hohwiller (hohwille at users.sourceforge.net)
* @since 4.0.0
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
* @author hohwille
* @since 7.1.0
*/
public class AbstractValidatorRange<V, R extends Comparable<R>> extends AbstractValueValidator<V> {
@SuppressWarnings("rawtypes")
public class AbstractValidatorRange<V, R extends Comparable> extends AbstractValueValidator<V> {

/** @see #validateNotNull(Comparable) */
private final Range<R> range;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.validation.base;

import java.util.Collection;

import net.sf.mmm.util.validation.base.collection.ValidatorCollectionSize;
import net.sf.mmm.util.value.api.Range;

/**
* The {@link ObjectValidatorBuilder builder} of {@link AbstractValidator} for {@link Comparable} values.
*
* @param <V> the generic type of the value to {@link AbstractValidator#validate(Object) validate}.
* @param <PARENT> the generic type of the {@link #and() parent builder}.
* @param <SELF> the generic type of this builder itself (this).
*
* @author hohwille
* @since 7.1.0
*/
@SuppressWarnings("rawtypes")
public abstract class CompareableValidatorBuilder<V extends Comparable, PARENT, SELF extends CompareableValidatorBuilder<V, PARENT, SELF>>
extends ObjectValidatorBuilder<V, PARENT, SELF> {

/**
* The constructor.
*
* @param parent the {@link #and() parent} builder.
*/
public CompareableValidatorBuilder(PARENT parent) {
super(parent);
}

/**
* @see ValidatorCollectionSize
*
* @param range the {@link Range} to limit the {@link Collection#size() size} of the {@link Collection}.
* @return this build instance for fluent API calls.
*/
public SELF size(Range<V> range) {

return add(new ValidatorRange<>(range));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.validation.base;

import java.util.ArrayList;
import java.util.List;

import net.sf.mmm.util.pojo.path.api.TypedProperty;
import net.sf.mmm.util.validation.api.ValidationFailure;
import net.sf.mmm.util.validation.api.ValueValidator;
Expand All @@ -20,7 +17,7 @@
* @author Joerg Hohwiller (hohwille at users.sourceforge.net)
* @since 3.1.0
*/
public class ComposedValidator<V> extends AbstractValidator<V> {
public class ComposedValidator<V> extends AbstractValidator<V> implements ComposedValueValidator<V> {

/** The child validators. */
private final AbstractValidator<? super V>[] validators;
Expand Down Expand Up @@ -52,26 +49,12 @@ protected String getCode() {
@Override
public ValidationFailure validate(V value, Object valueSource) {

ValidationFailure result = null;
List<ValidationFailure> failureList = null;
for (ValueValidator<? super V> validator : this.validators) {
ValidationFailure failure = validator.validate(value, valueSource);
if (failure != null) {
if (failureList == null) {
failureList = new ArrayList<>();
}
failureList.add(failure);
}
}
if (failureList != null) {
if (failureList.size() == 1) {
result = failureList.get(0);
} else {
result = new ComposedValidationFailure(valueSource,
failureList.toArray(new ValidationFailure[failureList.size()]));
}
ValidationFailureComposer composer = new ValidationFailureComposer();
for (ValueValidator<? super V> v : this.validators) {
ValidationFailure failure = v.validate(value, valueSource);
composer.add(failure);
}
return result;
return composer.get(valueSource);
}

/**
Expand All @@ -93,7 +76,7 @@ public int getValidatorCount() {
* @param index is the index of the {@link ValueValidator} to get.
* @return the requested {@link ValueValidator}.
*/
public ValueValidator<? super V> getValidator(int index) {
public AbstractValidator<? super V> getValidator(int index) {

return this.validators[index];
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.validation.base;

import net.sf.mmm.util.validation.api.ValueValidator;

/**
* This is the interface for a {@link ValueValidator} {@link #getValidator(int) composed} out of other validators.
*
* @param <V> the generic type of the value to {@link #validate(Object) validate}.
*
* @author hohwille
* @since 7.1.0
*/
public interface ComposedValueValidator<V> extends ValueValidator<V> {

/**
* @see #getValidator(int)
* @see java.util.Collection#size()
*
* @return the number of {@link #getValidator(int) validators}.
*/
int getValidatorCount();

/**
* Gets the {@link ValueValidator} at the given <code>index</code>.
*
* @see java.util.List#get(int)
*
* @param index is the index of the {@link ValueValidator} to get.
* @return the requested {@link ValueValidator}.
*/
AbstractValidator<?> getValidator(int index);

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.validation.base;

import java.util.ArrayList;
import java.util.List;

import net.sf.mmm.util.lang.api.Builder;

/**
* This is the base class to create instances of {@link AbstractValidator} using the builder pattern.
*
* @param <V> the generic type of the value to {@link AbstractValidator#validate(Object) validate}.
* @param <PARENT> the generic type of the {@link #and() parent builder}.
* @param <SELF> the generic type of this builder itself (this).
*
* @author hohwille
* @since 7.1.0
*/
public abstract class ObjectValidatorBuilder<V, PARENT, SELF extends ObjectValidatorBuilder<V, PARENT, SELF>>
implements Builder<AbstractValidator<? super V>> {

private final PARENT parent;

private final List<AbstractValidator<? super V>> validators;

/**
* The constructor.
*
* @param parent the {@link #and() parent} builder.
*/
public ObjectValidatorBuilder(PARENT parent) {
super();
this.validators = new ArrayList<>();
this.parent = parent;
}

/**
* @param <T> the generic type of the value to {@link AbstractValidator#validate(Object) validate}.
* @param builder the {@link ObjectValidatorBuilder}.
* @return the {@link List} of validators.
*/
protected <T> List<AbstractValidator<? super T>> getValidators(ObjectValidatorBuilder<T, ?, ?> builder) {

return builder.validators;
}

/**
* @return the parent {@link Builder} or <code>null</code> if {@literal <PARENT>} is void.
*/
public PARENT and() {

return this.parent;
}

/**
* @param validator the {@link AbstractValidator} to add to this builder.
* @return this build instance for fluent API calls.
*/
@SuppressWarnings("unchecked")
public SELF add(AbstractValidator<? super V> validator) {

this.validators.add(validator);
return (SELF) this;
}

/**
* Value is {@link ValidatorMandatory mandatory}.
*
* @return this build instance for fluent API calls.
*/
public SELF mandatory() {

return add(ValidatorMandatory.getInstance());
}

/**
* @return the {@link AbstractValidator}
*/
public AbstractValidator<? super V> build() {

int size = this.validators.size();
if (size == 0) {
return ValidatorNone.getInstance();
} else if (size == 1) {
return this.validators.get(0);
} else {
AbstractValidator<? super V>[] array = this.validators.toArray(new AbstractValidator[size]);
return new ComposedValidator<>(array);
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* Copyright (c) The m-m-m Team, Licensed under the Apache License, Version 2.0
* http://www.apache.org/licenses/LICENSE-2.0 */
package net.sf.mmm.util.validation.base;

import net.sf.mmm.util.validation.base.text.ValidatorBuilderCharSequence;
import net.sf.mmm.util.validation.base.text.ValidatorBuilderString;

/**
* TODO: this class ...
*
* @author hohwille
* @since 7.1.0
*/
public abstract class ObjectValidatorBuilderFactory<PARENT> {

private static final BuilderFactory INSTANCE = new BuilderFactory();

private final PARENT parent;

/**
* The constructor.
*/
public ObjectValidatorBuilderFactory(PARENT parent) {
super();
this.parent = parent;
}

/**
* @return the singleton instance of {@link BuilderFactory}.
*/
public static BuilderFactory getInstance() {

return INSTANCE;
}

/**
* @return the parent builder or <code>null</code> if <code>Void</code>.
*/
protected PARENT getParent() {

return this.parent;
}

public ValidatorBuilderCharSequence<PARENT> create(CharSequence v) {

return new ValidatorBuilderCharSequence<>(getParent());
}

public ValidatorBuilderString<PARENT> create(String v) {

return new ValidatorBuilderString<>(getParent());
}

// public <E, SELF extends CollectionValidatorBuilder<E, PARENT, SELF, SUB>, SUB extends ObjectValidatorBuilder<E,
// SELF, ?>> CollectionValidatorBuilder<E, PARENT, SELF, SUB> create(
// Collection<? extends E> v) {
//
// return new CollectionValidatorBuilder<>(getParent());
// }

public static final class BuilderFactory extends ObjectValidatorBuilderFactory<Void> {

/**
* The constructor.
*/
BuilderFactory() {
super(null);
}

}

}
Loading

0 comments on commit 6b72775

Please sign in to comment.