From a43be2cb8868a549c48c0a7f1292133140f68131 Mon Sep 17 00:00:00 2001 From: Robert von Burg Date: Wed, 18 Dec 2013 17:46:04 +0100 Subject: [PATCH] [New] Added DBC class for helping in assertions --- src/main/java/ch/eitchnet/utils/dbc/DBC.java | 56 ++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 src/main/java/ch/eitchnet/utils/dbc/DBC.java diff --git a/src/main/java/ch/eitchnet/utils/dbc/DBC.java b/src/main/java/ch/eitchnet/utils/dbc/DBC.java new file mode 100644 index 000000000..3ea0bb26e --- /dev/null +++ b/src/main/java/ch/eitchnet/utils/dbc/DBC.java @@ -0,0 +1,56 @@ +/* + * Copyright 2013 Robert von Burg + * + * 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 ch.eitchnet.utils.dbc; + +import java.text.MessageFormat; + +import ch.eitchnet.utils.helper.StringHelper; + +/** + * @author Robert von Burg + */ +public enum DBC { + + PRE { + public void assertNotEmpty(String msg, String value) { + if (StringHelper.isEmpty(value)) { + String ex = "Illegal empty value: {0}"; //$NON-NLS-1$ + ex = MessageFormat.format(ex, msg); + throw new DbcException(ex); + } + } + + @Override + public void assertNotNull(String msg, Object value) { + if (value == null) { + String ex = "Illegal null value: {0}"; //$NON-NLS-1$ + ex = MessageFormat.format(ex, msg); + throw new DbcException(ex); + } + } + }; + + public abstract void assertNotEmpty(String msg, String value); + public abstract void assertNotNull(String msg, Object value); + + public class DbcException extends RuntimeException { + private static final long serialVersionUID = 1L; + + public DbcException(String message) { + super(message); + } + } +}