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.util.Comparator;
023
024import org.apache.isis.core.metamodel.facetapi.FacetHolder;
025import org.apache.isis.core.metamodel.facetapi.IdentifiedHolder;
026import org.apache.isis.core.metamodel.facets.FacetedMethod;
027import org.apache.isis.core.metamodel.facets.members.order.MemberOrderFacet;
028import org.apache.isis.core.metamodel.layout.DeweyOrderSet;
029
030/**
031 * Compares by {@link MemberOrderFacet} obtained from each {@link FacetedMethod}
032 * ).
033 * 
034 * <p>
035 * Will also compare {@link OrderSet}s; these are put after any
036 * {@link FacetedMethod}s. If there is more than one OrderSet then these are
037 * compared by an {@link OrderSetGroupNameComparator}.
038 * 
039 * <p>
040 * If there is no annotation on either member, then will compare the members by
041 * name instead.
042 * 
043 * <p>
044 * Can specify if requires that members are in the same (group) name.
045 */
046public class MemberOrderComparator implements Comparator<Object> {
047
048    private final MemberOrderFacetComparator memberOrderFacetComparator;
049    private final MemberIdentifierComparator memberIdentifierComparator = new MemberIdentifierComparator();
050    private final OrderSetGroupNameComparator orderSetComparator = new OrderSetGroupNameComparator(true);
051    
052    public MemberOrderComparator(final boolean ensureGroupIsSame) {
053        memberOrderFacetComparator = new MemberOrderFacetComparator(ensureGroupIsSame);
054    }
055
056
057    @Override
058    public int compare(final Object o1, final Object o2) {
059        if (o1 instanceof IdentifiedHolder && o2 instanceof IdentifiedHolder) {
060            return compare((IdentifiedHolder) o1, (IdentifiedHolder) o2);
061        }
062        if (o1 instanceof DeweyOrderSet && o2 instanceof DeweyOrderSet) {
063            return orderSetComparator.compare((DeweyOrderSet) o1, (DeweyOrderSet) o2);
064        }
065        if (o1 instanceof IdentifiedHolder && o2 instanceof DeweyOrderSet) {
066            return -1; // members before OrderSets.
067        }
068        if (o1 instanceof DeweyOrderSet && o2 instanceof IdentifiedHolder) {
069            return +1; // members before OrderSets.
070        }
071        throw new IllegalArgumentException("can only compare IdentifiedHolders and DeweyOrderSets");
072    }
073
074    public int compare(final IdentifiedHolder o1, final IdentifiedHolder o2) {
075        final MemberOrderFacet m1 = getMemberOrder(o1);
076        final MemberOrderFacet m2 = getMemberOrder(o2);
077
078        final int memberOrderComparison = memberOrderFacetComparator.compare(m1, m2);
079        if(memberOrderComparison != 0) {
080            return memberOrderComparison;
081        } 
082        return memberIdentifierComparator.compare(o1, o2);
083    }
084
085
086    private MemberOrderFacet getMemberOrder(final FacetHolder facetHolder) {
087        return facetHolder.getFacet(MemberOrderFacet.class);
088    }
089
090}