Fork me on GitHub

Usage

Requirements

This library uses JSR-305 annotations to identify software defects easily.

This means that if the Quality-Check library is used also the JSR-305 library must exist in your classpath.

A simple use case

To use the library it must be added to your classpath. With Apache Maven you can start with this guide quickly.

Then you should have access to the classes of the library. For example, you might want to build an immutable object as the following example.

With Quality-Check

package net.example.app.model;

import java.util.regex.Pattern;
import javax.annotation.*;
import net.sf.qualitycheck.*;
import net.sf.qualitycheck.exception.*;

public class User {

  // example email pattern
  private static final Pattern EMAIL_PATTERN = Pattern.compile(".+@.+\\.[a-z]+");
  private final String name;
  private final String email;

  @ArgumentsChecked
  @Throws({
    IllegalNullArgumentException.class,
    IllegalEmptyArgumentException.class,
    IllegalPatternArgumentException.class })
  public User(@Nonnull final String name, @Nonnull final String email) {
    this.name = Check.notEmpty(name);
    this.email = Check.matchesPattern(EMAIL_PATTERN, email);
  }

  @Nonnull
  public String getEmail() {
    return email;
  }

  @Nonnull
  public String getName() {
    return name;
  }

}

Look how many lines of code must be written without Quality-Check.

Without Quality-Check

package net.example.app.model;

import java.util.regex.Pattern;

public class User {

  // example email pattern
  private static final Pattern EMAIL_PATTERN = Pattern.compile(".+@.+\\.[a-z]+");
  private final String name;
  private final String email;

  public User(final String name, final String email) {
    if (name == null) {
      throw new IllegalArgumentException("Argument 'name' must not be null.");
    }
    if (name.isEmpty()) {
      throw new IllegalArgumentException("Argument 'name' must not be empty.");
    }
    if (email == null) {
      throw new IllegalArgumentException("Argument 'email' must not be null.");
    }
    if (!EMAIL_PATTERN.matcher(email).matches()) {
      throw new IllegalArgumentException("Argument 'email' must be a valid email address.");
    }

    this.name = name;
    this.email = email;
  }

  public String getEmail() {
    return email;
  }

  public String getName() {
    return name;
  }

}