-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#160: added sub-packages and sorted new validators into sub-packages …
…to prevent chaos #168: implemented builders
- Loading branch information
Showing
28 changed files
with
988 additions
and
32 deletions.
There are no files selected for viewing
24 changes: 24 additions & 0 deletions
24
mmm-util-core/src/main/java/net/sf/mmm/util/lang/api/Builder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
mmm-util-core/src/main/java/net/sf/mmm/util/validation/base/CompareableValidatorBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
mmm-util-core/src/main/java/net/sf/mmm/util/validation/base/ComposedValueValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
|
||
} |
93 changes: 93 additions & 0 deletions
93
mmm-util-core/src/main/java/net/sf/mmm/util/validation/base/ObjectValidatorBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
72 changes: 72 additions & 0 deletions
72
...til-core/src/main/java/net/sf/mmm/util/validation/base/ObjectValidatorBuilderFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
|
||
} | ||
|
||
} |
Oops, something went wrong.