001 /*
002 * Copyright 2002-2005 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 * http://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 package org.springframework.binding.validation;
017
018 import java.util.Comparator;
019
020 import org.springframework.util.comparator.NullSafeComparator;
021
022 /**
023 * Comparator that compares ValidationMessages. Comparison is done by timestamp
024 * (desc) then property name then severity then message.
025 *
026 * @author Oliver Hutchison
027 */
028 public class ValidationMessageComparator implements Comparator {
029
030 /**
031 * A shared default instance of this comparator.
032 */
033 public static Comparator INSTANCE = new NullSafeComparator(new ValidationMessageComparator(), true);
034
035 protected ValidationMessageComparator() {
036 }
037
038 public int compare(Object o1, Object o2) {
039 ValidationMessage m1 = (ValidationMessage) o1;
040 ValidationMessage m2 = (ValidationMessage) o2;
041 int c;
042 if (m1.getTimestamp() == m2.getTimestamp()) {
043 c = NullSafeComparator.NULLS_HIGH.compare(m1.getProperty(), m2.getProperty());
044 if (c == 0) {
045 c = m1.getSeverity().compareTo(m2.getSeverity());
046 if (c == 0) {
047 c = m1.getMessage().compareTo(m2.getMessage());
048 }
049 }
050 }
051 else {
052 c = (m1.getTimestamp() > m2.getTimestamp()) ? -1 : 1;
053 }
054 return c;
055 }
056
057 }