001/* Copyright (C) 2014 konik.io 002 * 003 * This file is part of the Konik library. 004 * 005 * The Konik library is free software: you can redistribute it and/or modify 006 * it under the terms of the GNU Affero General Public License as 007 * published by the Free Software Foundation, either version 3 of the 008 * License, or (at your option) any later version. 009 * 010 * The Konik library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 013 * GNU Affero General Public License for more details. 014 * 015 * You should have received a copy of the GNU Affero General Public License 016 * along with the Konik library. If not, see <http://www.gnu.org/licenses/>. 017 */ 018package io.konik.validator; 019 020import io.konik.validator.annotation.NotBlank; 021 022import javax.validation.ConstraintValidator; 023import javax.validation.ConstraintValidatorContext; 024 025/** 026 * = The Not Blank Validator. 027 * 028 * Validator for the {@link NotBlank} annotation. 029 * 030 * @see {@link NotBlank} 031 */ 032public class NotBlankValidator implements ConstraintValidator<NotBlank, CharSequence> { 033 034 /** 035 * Initialize. 036 * 037 * @param annotation the annotation 038 */ 039 @Override 040 public void initialize(NotBlank annotation) { 041 //no special initialization needed 042 } 043 044 /** 045 * Checks that the trimmed string is not Blank. 046 * 047 * @param charSequence The character sequence to validate. 048 * @param constraintValidatorContext context in which the constraint is evaluated. 049 * 050 * @return Returns +true+ if the string is +null+ or the length of +charSequence+ is greater zero omitting leading 051 * and trailing whitespace. 052 * Otherwise it will return +false+. 053 */ 054 @Override 055 public boolean isValid(CharSequence charSequence, ConstraintValidatorContext constraintValidatorContext) { 056 if (charSequence == null) { return true; } 057 return charSequence.toString().trim().length() > 0; 058 } 059}