001 package org.fest.util;
002
003 /*
004 * Created on Sep 17, 2010
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
012 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
013 * specific language governing permissions and limitations under the License.
014 *
015 * Copyright @2010-2011 the original author or authors.
016 */
017
018 import static java.lang.reflect.Array.getLength;
019
020 import static org.fest.util.Collections.isEmpty;
021
022 import java.lang.reflect.Array;
023 import java.util.HashSet;
024 import java.util.Set;
025
026 /**
027 * Base implementation of {@link ComparisonStrategy} contract.
028 *
029 * @author Joel Costigliola
030 */
031 public abstract class AbstractComparisonStrategy implements ComparisonStrategy {
032
033 public Iterable<?> duplicatesFrom(Iterable<?> iterable) {
034 Set<Object> duplicates = new HashSet<Object>();
035 if (isEmpty(iterable)) return duplicates;
036 Set<Object> noDuplicates = new HashSet<Object>();
037 for (Object element : iterable) {
038 if (iterableContains(noDuplicates, element)) {
039 duplicates.add(element);
040 continue;
041 }
042 noDuplicates.add(element);
043 }
044 return duplicates;
045 }
046
047 public boolean arrayContains(Object array, Object value) {
048 for (int i = 0; i < getLength(array); i++) {
049 Object element = Array.get(array, i);
050 if (areEqual(element, value)) return true;
051 }
052 return false;
053 }
054
055 public boolean isLessThan(Object actual, Object other) {
056 if (areEqual(actual, other)) return false;
057 return !isGreaterThan(actual, other);
058 }
059
060 public boolean isLessThanOrEqualTo(Object actual, Object other) {
061 if (areEqual(actual, other)) return true;
062 return isLessThan(actual, other);
063 }
064
065 public boolean isGreaterThanOrEqualTo(Object actual, Object other) {
066 if (areEqual(actual, other)) return true;
067 return isGreaterThan(actual, other);
068 }
069
070 }