Skip to content

Commit

Permalink
Marked as Immutable; Method signature changes (as in Specification);
Browse files Browse the repository at this point in the history
Generics clean up
  • Loading branch information
vardlokkur committed Mar 24, 2013
1 parent e153c0a commit 93657fa
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package pl.ais.commons.domain.specification.mail;

import javax.annotation.concurrent.Immutable;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;

Expand All @@ -11,23 +12,28 @@
* @author Warlock, AIS.PL
* @since 1.0.1
*/
public final class ValidEmailSpecification implements Specification<String> {
@Immutable
public final class ValidEmailSpecification implements Specification<CharSequence> {

/**
* Defines singleton instance of {@link ValidEmailSpecification}.
*/
public static final Specification<String> INSTANCE = new ValidEmailSpecification();
@SuppressWarnings("rawtypes")
public static final Specification INSTANCE = new ValidEmailSpecification();

/**
* {@inheritDoc}
*/
@Override
public boolean isSatisfiedBy(final String candidate) {
boolean result = true;
try {
new InternetAddress(candidate).validate();
} catch (final AddressException exception) {
result = false;
public <T extends CharSequence> boolean isSatisfiedBy(final T candidate) {
boolean result = false;
if (null != candidate) {
try {
new InternetAddress(candidate.toString()).validate();
result = true;
} catch (final AddressException exception) {
// Ignore ...
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package pl.ais.commons.domain.specification.simple;

import javax.annotation.concurrent.Immutable;

import pl.ais.commons.domain.specification.Specification;

/**
Expand All @@ -9,28 +11,31 @@
* @author Warlock, AIS.PL
* @since 1.0.1
*/
@Immutable
public final class BlankSpecification implements Specification<CharSequence> {

/**
* Defines singleton instance of {@link BlankSpecification}.
*/
public static final Specification<CharSequence> INSTANCE = new BlankSpecification();
@SuppressWarnings("rawtypes")
public static final Specification INSTANCE = new BlankSpecification();

/**
* {@inheritDoc}
*/
@Override
public boolean isSatisfiedBy(final CharSequence candidate) {
public <T extends CharSequence> boolean isSatisfiedBy(final T candidate) {
final int length;
boolean result = true;
if ((null != candidate) && (0 < (length = candidate.length()))) {
for (int i = 0; i < length; i++) {
if (false == Character.isWhitespace(candidate.charAt(i))) {
if (!Character.isWhitespace(candidate.charAt(i))) {
result = false;
break;
}
}
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
import java.util.Collection;
import java.util.Map;

import javax.annotation.concurrent.Immutable;

import pl.ais.commons.domain.specification.Specification;

/**
Expand All @@ -13,11 +15,12 @@
* candidate having neither of the above types (including {@code null}) will never satisfy this specification.
* </p>
*
* @param <T> determines the type of candidate
* @param <C> determines the type of candidate
* @author Warlock, AIS.PL
* @since 1.0.1
*/
public final class EmptySpecification<T> implements Specification<T> {
@Immutable
public final class EmptySpecification<C> implements Specification<C> {

/**
* Defines singleton instance of {@link EmptySpecification}.
Expand All @@ -30,7 +33,7 @@ public final class EmptySpecification<T> implements Specification<T> {
*/
@SuppressWarnings("rawtypes")
@Override
public boolean isSatisfiedBy(final T candidate) {
public <T extends C> boolean isSatisfiedBy(final T candidate) {
boolean result = false;
processing: {
if (candidate instanceof Collection) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,26 +1,28 @@
package pl.ais.commons.domain.specification.simple;

import javax.annotation.Nullable;
import javax.annotation.concurrent.Immutable;

import pl.ais.commons.domain.specification.Specification;

/**
* Specification implementation satisfied by candidates equal to some predefined value.
*
* @param <T> determines the type of candidate
* @param <C> determines the type of candidate
* @author Warlock, AIS.PL
* @since 1.0.1
*/
public final class IsEqualSpecification<T> implements Specification<T> {
@Immutable
public final class IsEqualSpecification<C> implements Specification<C> {

private final T value;
private final C value;

/**
* Constructs new instance.
*
* @param value the value
*/
public IsEqualSpecification(@Nullable final T value) {
public IsEqualSpecification(@Nullable final C value) {
super();
this.value = value;
}
Expand All @@ -29,7 +31,7 @@ public IsEqualSpecification(@Nullable final T value) {
* {@inheritDoc}
*/
@Override
public boolean isSatisfiedBy(final T candidate) {
public <T extends C> boolean isSatisfiedBy(final T candidate) {
boolean result;
if (null == value) {
result = (null == candidate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.regex.Pattern;

import javax.annotation.concurrent.Immutable;

import pl.ais.commons.domain.specification.Specification;

/**
Expand All @@ -10,7 +12,8 @@
* @author Warlock, AIS.PL
* @since 1.0.1
*/
public class RegexSpecification implements Specification<CharSequence> {
@Immutable
public final class RegexSpecification implements Specification<CharSequence> {

private final Pattern pattern;

Expand All @@ -28,7 +31,7 @@ public RegexSpecification(final String regex) {
* {@inheritDoc}
*/
@Override
public boolean isSatisfiedBy(final CharSequence candidate) {
public <T extends CharSequence> boolean isSatisfiedBy(final T candidate) {
return (null != candidate) && pattern.matcher(candidate).matches();
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package pl.ais.commons.domain.specification.simple;

import javax.annotation.concurrent.Immutable;

import pl.ais.commons.domain.specification.Specification;

/**
* Specification implementation satisfied by any candidate.
*
* @param <T> determines the type of candidate
* @param <C> determines the type of candidate
* @author Warlock, AIS.PL
* @since 1.0.1
*/
public final class TrueSpecification<T> implements Specification<T> {
@Immutable
public final class TrueSpecification<C> implements Specification<C> {

/**
* Defines singleton instance of {@link TrueSpecification}.
Expand All @@ -21,7 +24,7 @@ public final class TrueSpecification<T> implements Specification<T> {
* {@inheritDoc}
*/
@Override
public boolean isSatisfiedBy(final T candidate) {
public <T extends C> boolean isSatisfiedBy(final T candidate) {
return true;
}

Expand Down

0 comments on commit 93657fa

Please sign in to comment.