cz.muni.fi.editor.services.commons.Assert.java Source code

Java tutorial

Introduction

Here is the source code for cz.muni.fi.editor.services.commons.Assert.java

Source

/*
*Copyright  2016 Dominik Szalai (emptulik@gmail.com)
*
*Licensed under the Apache License, Version 2.0 (the "License");
*you may not use this file except in compliance with the License.
*You may obtain a copy of the License at
*
*    http://www.apache.org/licenses/LICENSE-2.0
*
*Unless required by applicable law or agreed to in writing, software
*distributed under the License is distributed on an "AS IS" BASIS,
*WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*See the License for the specific language governing permissions and
*limitations under the License.
*/
package cz.muni.fi.editor.services.commons;

import cz.muni.fi.editor.api.dto.DTO;
import cz.muni.fi.editor.api.exceptions.FieldException;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;

/**
 * Created by Dominik Szalai - emptulik at gmail.com on 14.11.2016.
 */
public class Assert {
    public static void isNull(Object object, String message) throws IllegalArgumentException {
        if (object == null) {
            throw new IllegalArgumentException(message);
        }
    }

    public static void hasID(DTO dto, String message) throws FieldException {
        if (dto.getId() == null) {
            throw new FieldException("id", message);
        }
    }

    public static void fieldIsEmpty(String fieldValue, String fieldName) throws FieldException {
        if (StringUtils.isEmpty(fieldValue)) {
            throw new FieldException(fieldName, MessageFormat.format("Field {0} is empty.", fieldName));
        }
    }
}