001/* 002 * Licensed to DuraSpace under one or more contributor license agreements. 003 * See the NOTICE file distributed with this work for additional information 004 * regarding copyright ownership. 005 * 006 * DuraSpace licenses this file to you under the Apache License, 007 * Version 2.0 (the "License"); you may not use this file except in 008 * compliance 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, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.fcrepo.http.commons.api.rdf; 019 020import java.util.Comparator; 021 022import org.apache.jena.graph.Triple; 023import org.apache.jena.shared.PrefixMapping; 024 025/** 026 * Comparator to sort a list of Quads by subject, predicate, and object 027 * to ensure a consistent order for human-readable output 028 * 029 * @author awoods 030 */ 031public class TripleOrdering implements Comparator<Triple> { 032 033 private final PrefixMapping prefixMapping; 034 035 /** 036 * When sorting predicates, take into account the given PrefixMapping 037 * @param prefixMapping the prefix mapping 038 */ 039 public TripleOrdering(final PrefixMapping prefixMapping) { 040 super(); 041 042 this.prefixMapping = prefixMapping; 043 } 044 045 @Override 046 public int compare(final Triple left, final Triple right) { 047 048 final int s = 049 left.getSubject().toString(prefixMapping, false).compareTo( 050 right.getSubject().toString(prefixMapping, false)); 051 052 if (s != 0) { 053 return s; 054 } 055 056 final int p = 057 left.getPredicate().toString(prefixMapping, false).compareTo( 058 right.getPredicate().toString(prefixMapping, false)); 059 060 if (p != 0) { 061 return p; 062 } 063 064 return left.getObject().toString(false).compareTo( 065 right.getObject().toString(false)); 066 067 } 068 069}