001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.metamodel.layout.memberorderfacet;
021
022import java.io.Serializable;
023import java.util.Comparator;
024
025import org.apache.isis.core.metamodel.layout.DeweyOrderSet;
026
027/**
028 * Compares by (simple) group name of each {@link OrderSet}.
029 * 
030 * <p>
031 * Note that it only makes sense to use this comparator for {@link OrderSet}s
032 * that are known to have the same parent {@link OrderSet}s.
033 */
034public class OrderSetGroupNameComparator implements Comparator<DeweyOrderSet>, Serializable {
035
036    private static final long serialVersionUID = 1L;
037
038    public OrderSetGroupNameComparator(final boolean ensureInSameGroupPath) {
039        this.ensureInSameGroupPath = ensureInSameGroupPath;
040    }
041
042    private final boolean ensureInSameGroupPath;
043
044    @Override
045    public int compare(final DeweyOrderSet o1, final DeweyOrderSet o2) {
046        if (ensureInSameGroupPath && !o1.getGroupPath().equals(o2.getGroupPath())) {
047            throw new IllegalArgumentException("OrderSets being compared do not have the same group path");
048        }
049
050        final String groupName1 = o1.getGroupName();
051        final String groupName2 = o2.getGroupName();
052
053        return groupName1.compareTo(groupName2);
054    }
055}