View Javadoc

1   /**
2    * Copyright 2010-2012 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.common.util;
17  
18  import java.util.ArrayList;
19  import java.util.List;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  
24  public class ModeUtils {
25  
26  	private static final Logger logger = LoggerFactory.getLogger(ModeUtils.class);
27  
28  	public static final void validate(Mode mode, String msg) {
29  		validate(mode, msg, msg);
30  	}
31  
32  	public static final void validate(Mode mode, String msg, String errMsg) {
33  		validate(mode, msg, null, errMsg);
34  	}
35  
36  	public static final void validate(Mode mode, String msg, Object arg, String errMsg) {
37  		validate(mode, msg, arg, null, errMsg);
38  	}
39  
40  	public static final void validate(Mode mode, String msg, Object arg1, Object arg2, String errMsg) {
41  		validate(mode, msg, getArgs(arg1, arg2), errMsg);
42  	}
43  
44  	public static final void validate(Mode mode, String msg, Object[] args, String errMsg) {
45  		switch (mode) {
46  		case IGNORE:
47  			return;
48  		case DEBUG:
49  			logger.debug(msg, args);
50  			return;
51  		case INFORM:
52  			logger.info(msg, args);
53  			return;
54  		case WARN:
55  			logger.warn(msg, args);
56  			return;
57  		case ERROR:
58  			logger.error(msg, args);
59  			throw new IllegalStateException(errMsg);
60  		default:
61  			throw new IllegalArgumentException("Mode '" + mode + "' is unknown");
62  		}
63  	}
64  
65  	protected static final Object[] getArgs(Object arg1, Object arg2) {
66  		if (arg1 == null && arg2 == null) {
67  			return null;
68  		}
69  		List<Object> args = new ArrayList<Object>();
70  		if (arg1 != null) {
71  			args.add(arg1);
72  		}
73  		if (arg2 != null) {
74  			args.add(arg2);
75  		}
76  		return CollectionUtils.toArray(args);
77  	}
78  
79  }