001 /*
002 * Copyright 2011 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 */
016 package org.kuali.kfs.gl;
017
018 /**
019 * This class is used to compare objects with one another
020 */
021 public class ObjectHelper {
022 protected ObjectHelper() {
023 }
024
025 /**
026 * Returns true if object on left side is equal to object on right side
027 *
028 * @param lhs object on left side of equation
029 * @param rhs object on right side of equation
030 * @return true if both lhs and rhs are null or if lhs.equals(rhs)
031 */
032 static public boolean isEqual(Object lhs, Object rhs) {
033 return (null == lhs && null == rhs) || (null != lhs && lhs.equals(rhs));
034 }
035
036 /**
037 * Return true if object on left side is one of the items in array of objects
038 *
039 * @param lhs object on left side of equation
040 * @param rhs object on right side of equation
041 * @return false if rhs is null. true if isEqual(lhs, rhs[i]) for any ith element of rhs.
042 */
043 static public boolean isOneOf(Object lhs, Object[] rhs) {
044 if (rhs == null)
045 return false;
046
047 // simple linear search. Arrays.binarySearch isn't appropriate
048 // because the elements of rhs aren't in natural order.
049 for (int i = 0; i < rhs.length; i++) {
050 if (isEqual(lhs, rhs[i])) {
051 return true;
052 }
053 }
054
055 return false;
056 }
057 }