com.openpojo.business.annotation.BusinessKey Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of openpojo Show documentation
Show all versions of openpojo Show documentation
This project was born out of a need to validate all POJOs (Plain Old Java Object) are behaving correctly.
This project has two main aspects to it:
* Make Testing as easy as possible.
* Simplifying identity management (hashCode / equals) using annotation.
/*
* Copyright (c) 2010-2013 Osman Shoukry
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License or any
* later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see .
*/
package com.openpojo.business.annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
/**
* This annotation is used to indicate a field is part of the business identity.
* Fields tagged with BusinessKey annotation will be part of the equals / hashcode.
* Key fields are:
* 1. caseSensitive (default is true)
* 2. required (default is true)
* 3. composite (default is false)
*
* caseSensitive usage:
* If a field tagged with caseSensitive=false (i.e. not case sensitive) AND the field is of type
* Character or CharacterSequence, the field must be normalized and then hashcode/equality is performed.
*
* Example: {@link BusinessKey}(caseSensitive = false)
* String one = "hello world"
* must have the same hashcode & equality as:
* one = "HELLO WORLD"
* required usage:
* If a field is tagged with required = false, then the field will only be used in equality only if its populated.
* Two null fields tagged with required = false are equal.
*
* composite usage:
* If a field is tagged with composite = true, then two things happen, first, required configuration is ignored,
* and this field is treated as optional part of a group. This also means that one of the fields tagged with composite
* MUST have a value.
* Example:
* If you had a POJO called Person, that had a FirstName & LastName fields, and you want to set a rule that is for this
* POJO to be complete, one of the names MUST be populated, but its not important which one.
* This is typical composite key usage.
*
* @author oshoukry
*/
@Retention(RUNTIME)
@Target(FIELD)
public @interface BusinessKey {
/**
* Set to true if the field is of type Character or CharSequence to ignore case when comparing.
*/
boolean caseSensitive() default true;
/**
* Set to True to indicate field required to be populated.
*/
boolean required() default true;
/**
* Set to true if this key is part of a group where any one of the group needs to be not null.
* Setting Composite to true, shadows the "required" above, since composite is OR-ing against any that are
* populated. With at least one of the composite group being non-null.
*/
boolean composite() default false;
}