All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dk.eobjects.metamodel.util.BooleanComparator Maven / Gradle / Ivy

/**
 *  This file is part of MetaModel.
 *
 *  MetaModel is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  MetaModel is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with MetaModel.  If not, see .
 */
package dk.eobjects.metamodel.util;

import java.util.Comparator;

import org.apache.commons.math.MathException;
import org.apache.commons.math.util.DefaultTransformer;

public class BooleanComparator implements Comparator {

	private static BooleanComparator _instance = new BooleanComparator();

	private BooleanComparator() {
	}

	public static Comparator getComparator() {
		return _instance;
	}

	public static Comparable getComparable(Object object) {
		final Boolean b = toBoolean(object);
		return new Comparable() {

			public int compareTo(Object o) {
				return _instance.compare(b, o);
			}

			@Override
			public String toString() {
				return "BooleanComparable[boolean=" + b + "]";
			}
		};
	}

	public int compare(Object o1, Object o2) {
		Boolean b1 = toBoolean(o1);
		Boolean b2 = toBoolean(o2);
		return b1.compareTo(b2);
	}

	private static Boolean toBoolean(Object o) {
		if (o != null) {
			if (o instanceof Boolean) {
				return (Boolean) o;
			}
			if (o instanceof String) {
				try {
					return parseBoolean((String) o);
				} catch (IllegalArgumentException e) {
					return false;
				}
			}
			if (o instanceof Number) {
				try {
					double number = new DefaultTransformer().transform(o);
					if (number >= 1.0) {
						return true;
					}
				} catch (MathException e) {
				}
			}
		}
		return false;
	}

	/**
	 * Parses a string and returns a boolean representation of it. To parse the
	 * string the following values will be accepted, irrespective of case.
	 * 
    *
  • true
  • *
  • false
  • *
  • 1
  • *
  • 0
  • *
  • yes
  • *
  • no
  • *
  • y
  • *
  • n
  • *
* * @param string * the string to parse * @return a boolean * @throws IllegalArgumentException * if the string provided cannot be parsed as a boolean */ public static boolean parseBoolean(String string) throws IllegalArgumentException { string = string.trim(); if ("true".equalsIgnoreCase(string) || "1".equals(string) || "y".equalsIgnoreCase(string) || "yes".equalsIgnoreCase(string)) { return true; } else if ("false".equalsIgnoreCase(string) || "0".equals(string) || "n".equalsIgnoreCase(string) || "no".equalsIgnoreCase(string)) { return false; } else { throw new IllegalArgumentException( "Could not get boolean value of string: " + string); } } }