Edevat

Edevat includes utility classes,io classes, data structures implementations and much much more.

License

License

GroupId

GroupId

com.github.enesusta
ArtifactId

ArtifactId

edevat
Last Version

Last Version

1.0.4
Release Date

Release Date

Type

Type

jar
Description

Description

Edevat
Edevat includes utility classes,io classes, data structures implementations and much much more.
Project URL

Project URL

https://github.com/enesusta/edevat
Source Code Management

Source Code Management

http://github.com/enesusta/tasdik

Download edevat

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.enesusta/edevat/ -->
<dependency>
    <groupId>com.github.enesusta</groupId>
    <artifactId>edevat</artifactId>
    <version>1.0.4</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.enesusta/edevat/
implementation 'com.github.enesusta:edevat:1.0.4'
// https://jarcasting.com/artifacts/com.github.enesusta/edevat/
implementation ("com.github.enesusta:edevat:1.0.4")
'com.github.enesusta:edevat:jar:1.0.4'
<dependency org="com.github.enesusta" name="edevat" rev="1.0.4">
  <artifact name="edevat" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.enesusta', module='edevat', version='1.0.4')
)
libraryDependencies += "com.github.enesusta" % "edevat" % "1.0.4"
[com.github.enesusta/edevat "1.0.4"]

Dependencies

compile (1)

Group / Artifact Type Version
commons-io : commons-io jar 2.7

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter-engine jar 5.6.2

Project Modules

There are no modules declared in this project.

tasdik

Travis CI w/ Logo Issues Maven Central javadoc License

What is it?

Tasdik is bean (JavaBean, POJO) validation library that uses Java reflection API.

Quick Installation

If you are using Maven you can add the repository by adding the following XML to your project pom.xml file.

<dependency>
  <groupId>com.github.enesusta</groupId>
  <artifactId>tasdik</artifactId>
  <version>1.7.7</version>
</dependency>

How could you use it?

Tasdik has 10 annotation to validate bean which uses in general purposes.

Those are:

  • @False
  • @True
  • @Max
  • @Min
  • @Negative
  • @Positive
  • @NonNull
  • @Size
  • @Regex
  • @Email

Lets assume that we have a bean which we want to validate.

import com.github.enesusta.tasdik.fals.False;
import com.github.enesusta.tasdik.max.Max;
import com.github.enesusta.tasdik.min.Min;
import com.github.enesusta.tasdik.negative.Negative;
import com.github.enesusta.tasdik.nonnull.NonNull;
import com.github.enesusta.tasdik.positive.Positive;
import com.github.enesusta.tasdik.regex.Regex;
import com.github.enesusta.tasdik.size.Size;
import com.github.enesusta.tasdik.tru.True;
import com.github.enesusta.tasdik.validator.DefaultValidator;
import com.github.enesusta.tasdik.validator.Validator;

public class Example {

    public static class Bean {

        @False
        private boolean mustBeFalse;

        @True
        private boolean mustBeTrue;

        @Max(value = 10)
        private int mustBeAMaximumOf10;

        @Min(value = 5)
        private int mustBeAMin5;

        @Negative
        private int mustBeNegative;

        @Positive
        private int mustBePositive;

        @NonNull
        private Object mustBeNonNull;

        @Size(min = 5, max = 10)
        private String numberOfCharactersMustBeBetween5and10;

        @Regex(pattern = "\\d+")
        private String mustBeDigit;

        @Email
        private String mustBeEmail;

        /**
         * setter/getter/constructor and other stuffs
         */
    }

    public static void main(String[] args) throws IllegalAccessException {

        Bean bean = new Bean();
        /**
         * setter / getter etc on your bean
         */

        Validator validator = DefaultValidator.getInstance();
        boolean isValid = validator.isValid(bean);
    }

}

How tasdik works?

Tasdik is based on two interface.

  • Validator
  • FieldValidator
Validator

There are 1 concrete class of Validator.

  • DefaultValidator
public interface Validator {
    boolean isValid(Object object) throws IllegalAccessException;

    default boolean hasAny(boolean[] arr) {
        boolean valid = true;
        for (boolean b : arr) {
            if (!b) {
                valid = false;
                break;
            }
        }
        return valid;
    }
}
FieldValidator

FieldValidator interface is heart of the instrastructe of Tasdik. There are 10 concrete class of FieldValidator.

import java.lang.reflect.Field;

public interface FieldValidator {
    boolean isFieldValid(Field field) throws IllegalAccessException;
}

Those are:

  • FalseFieldValidator
  • TrueFieldValidator
  • MaxFieldValidator
  • MinFieldValidator
  • NegativeFieldValidator
  • PositiveFieldValidator
  • NonNullFieldValidator
  • SizeFieldValidator
  • RegexFieldValidator
  • EmailFieldValidator

As mentioned above Validator interface has one concrete class. In this manner DefaultValidator class uses a specific class which name is FieldContext that uses Strategy pattern.

Implementation of FieldContext
public class FieldContext {

    private static FieldContext instance = null;
    private Map<Class<? extends Annotation>, FieldValidator> contextMap = new HashMap<>(9);

    private FieldContext(final Object o) {
        initialize(o);
    }

    private void initialize(final Object object) {
        contextMap.put(Email.class, new EmailFieldValidator(object));
        contextMap.put(Min.class, new MinFieldValidator(object));
        contextMap.put(Max.class, new MaxFieldValidator(object));
        contextMap.put(Negative.class, new NegativeFieldValidator(object));
        contextMap.put(Positive.class, new PositiveFieldValidator(object));
        contextMap.put(Regex.class, new RegexFieldValidator(object));
        contextMap.put(Size.class, new SizeFieldValidator(object));
        contextMap.put(True.class, new TrueFieldValidator(object));
        contextMap.put(False.class, new FalseFieldValidator(object));
    }

    public static FieldContext getInstance(final Object o) {
        if (instance == null)
            instance = new FieldContext(o);
        return instance;
    }

    public boolean isValid(final Field field) throws IllegalAccessException {
        boolean isValid = true;
        for (Annotation annotation : field.getAnnotations())
            if (!contextMap.get(annotation.annotationType()).isFieldValid(field))
                isValid = false;
        return isValid;
    }

}

Visualisation Of Implementation

Versions

Version
1.0.4
1.0.3
1.0.2
1.0.1
1.0.0