001/*
002 * Copyright 2002-2012 the original author or authors.
003 *
004 * Licensed under the Apache 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 *      https://www.apache.org/licenses/LICENSE-2.0
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
017package org.springframework.util.comparator;
018
019import java.io.Serializable;
020import java.util.Comparator;
021
022/**
023 * A Comparator for Boolean objects that can sort either true or false first.
024 *
025 * @author Keith Donald
026 * @since 1.2.2
027 */
028@SuppressWarnings("serial")
029public final class BooleanComparator implements Comparator<Boolean>, Serializable {
030
031        /**
032         * A shared default instance of this comparator, treating true lower
033         * than false.
034         */
035        public static final BooleanComparator TRUE_LOW = new BooleanComparator(true);
036
037        /**
038         * A shared default instance of this comparator, treating true higher
039         * than false.
040         */
041        public static final BooleanComparator TRUE_HIGH = new BooleanComparator(false);
042
043
044        private final boolean trueLow;
045
046
047        /**
048         * Create a BooleanComparator that sorts boolean values based on
049         * the provided flag.
050         * <p>Alternatively, you can use the default shared instances:
051         * {@code BooleanComparator.TRUE_LOW} and
052         * {@code BooleanComparator.TRUE_HIGH}.
053         * @param trueLow whether to treat true as lower or higher than false
054         * @see #TRUE_LOW
055         * @see #TRUE_HIGH
056         */
057        public BooleanComparator(boolean trueLow) {
058                this.trueLow = trueLow;
059        }
060
061
062        @Override
063        public int compare(Boolean v1, Boolean v2) {
064                return (v1 ^ v2) ? ((v1 ^ this.trueLow) ? 1 : -1) : 0;
065        }
066
067        @Override
068        public boolean equals(Object obj) {
069                if (this == obj) {
070                        return true;
071                }
072                if (!(obj instanceof BooleanComparator)) {
073                        return false;
074                }
075                return (this.trueLow == ((BooleanComparator) obj).trueLow);
076        }
077
078        @Override
079        public int hashCode() {
080                return (this.trueLow ? -1 : 1) * getClass().hashCode();
081        }
082
083        @Override
084        public String toString() {
085                return "BooleanComparator: " + (this.trueLow ? "true low" : "true high");
086        }
087
088}