source: RELcat/trunk/mod-openanzo/nk/src/StatementComparator.java @ 2029

Last change on this file since 2029 was 2029, checked in by mwindhouwer, 12 years ago

Initial import of all the *cats, i.e., ISOcat, RELcat and SCHEMAcat.

File size: 2.9 KB
Line 
1/*******************************************************************************
2 * Copyright (c) 2008 Cambridge Semantics Incorporated.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 *     Cambridge Semantics Incorporated
10 *******************************************************************************/
11package org.isocat.relcat.openanzo;
12
13import java.util.ArrayList;
14import java.util.Collection;
15import java.util.Collections;
16import java.util.Comparator;
17
18import org.apache.commons.lang.builder.CompareToBuilder;
19import org.openanzo.rdf.Statement;
20import org.openanzo.rdf.URI;
21import org.openanzo.rdf.vocabulary.RDF;
22
23/**
24 * Sorts statements by named graph, then subject, pred, obj. This sorting results in well framed serialized rdf.
25 *
26 * @author Joe Betz <jpbetz@cambridgesemantics.com>
27 *
28 */
29class StatementComparator implements Comparator<Statement> {
30
31    private static StatementComparator statementComparator = new StatementComparator();
32
33    private static PredicateComparator predicateComparator = new PredicateComparator();
34
35    public static Collection<Statement> sort(Collection<Statement> stmt) {
36        ArrayList<Statement> list = new ArrayList<Statement>(stmt);
37        Collections.sort(list, statementComparator);
38        return list;
39    }
40
41    public int compare(Statement o1, Statement o2) {
42        CompareToBuilder builder = new CompareToBuilder();
43        builder.append(o1.getNamedGraphUri() != null ? o1.getNamedGraphUri().toString() : null, o2.getNamedGraphUri() != null ? o2.getNamedGraphUri().toString() : null);
44        builder.append(o1.getSubject().toString(), o2.getSubject().toString());
45        builder.append(o1.getPredicate(), o2.getPredicate(), predicateComparator);
46        builder.append(o1.getObject().toString(), o2.getObject().toString());
47        return builder.toComparison();
48    }
49
50    /**
51     * This sort URIs in lexical order except for the rdf:type uri, which it always sorts before any other URI. This makes the serialization of RDF simpler to
52     * read since it's typically easier to read when the type assertions for a resource come first, before other properties.
53     *
54     * The rdf:type URI is specifically <code>http://www.w3.org/1999/02/22-rdf-syntax-ns#type</code>.
55     *
56     * @author Jordi Albornoz Mulligan <jordi@cambridgesemantics.com>
57     */
58    public static class PredicateComparator implements Comparator<URI> {
59        public int compare(URI o1, URI o2) {
60            int ret;
61            if (o1.equals(o2)) {
62                ret = 0;
63            } else if (o1.equals(RDF.TYPE)) {
64                ret = -1;
65            } else if (o2.equals(RDF.TYPE)) {
66                ret = 1;
67            } else {
68                ret = o1.compareTo(o2);
69            }
70            return ret;
71        }
72    }
73}
Note: See TracBrowser for help on using the repository browser.