001/** 002 * Copyright 2010-2014 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.kuali.common.util; 017 018import java.util.ArrayList; 019import java.util.List; 020 021import org.slf4j.Logger; 022import org.slf4j.LoggerFactory; 023 024public class ModeUtils { 025 026 private static final Logger logger = LoggerFactory.getLogger(ModeUtils.class); 027 028 public static final void validate(Mode mode, String msg) { 029 validate(mode, msg, msg); 030 } 031 032 public static final void validate(Mode mode, String msg, String errMsg) { 033 validate(mode, msg, null, errMsg); 034 } 035 036 public static final void validate(Mode mode, String msg, Object arg, String errMsg) { 037 validate(mode, msg, arg, null, errMsg); 038 } 039 040 public static final void validate(Mode mode, String msg, Object arg1, Object arg2, String errMsg) { 041 validate(mode, msg, getArgs(arg1, arg2), errMsg); 042 } 043 044 public static final void validate(Mode mode, String msg, Object[] args, String errMsg) { 045 switch (mode) { 046 case IGNORE: 047 return; 048 case DEBUG: 049 logger.debug(msg, args); 050 return; 051 case INFORM: 052 logger.info(msg, args); 053 return; 054 case WARN: 055 logger.warn(msg, args); 056 return; 057 case ERROR: 058 logger.error(msg, args); 059 throw new IllegalStateException(errMsg); 060 default: 061 throw new IllegalArgumentException("Mode '" + mode + "' is unknown"); 062 } 063 } 064 065 protected static final Object[] getArgs(Object arg1, Object arg2) { 066 if (arg1 == null && arg2 == null) { 067 return null; 068 } 069 List<Object> args = new ArrayList<Object>(); 070 if (arg1 != null) { 071 args.add(arg1); 072 } 073 if (arg2 != null) { 074 args.add(arg2); 075 } 076 return CollectionUtils.toObjectArray(args); 077 } 078 079}