at.newmedialab.ldpath.model.functions.SortFunction Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of ldpath-core-bundle Show documentation
Show all versions of ldpath-core-bundle Show documentation
Core Implementation of LD Path a simple path-based query language similar to XPath or SPARQL Property Paths that is particularly well-suited for querying and retrieving resources from the Linked Data Cloud by following RDF links between resources and servers.
/*
* Copyright (c) 2011 Salzburg Research.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package at.newmedialab.ldpath.model.functions;
import at.newmedialab.ldpath.api.backend.RDFBackend;
import at.newmedialab.ldpath.api.functions.SelectorFunction;
import at.newmedialab.ldpath.model.transformers.DateTimeTransformer;
import at.newmedialab.ldpath.model.transformers.StringTransformer;
import java.text.Collator;
import java.util.*;
/**
* Allow sorting of the selection passed as first argument. Usage:
*
*
* - fn:sort(path-expression): sorts the results according to ascending string order
* - fn:sort(path-expression, order): sorts the results according to the given order in
* ascending direction; order can be one of "string", "number" or "date"
* - fn:sort(path-expression, order, direction): sorts the results according to the
* given order in the specified direction; order can be one of "string", "number" or "date";
* direction can be one of "asc" or "desc"
*
*
*
* Author: Sebastian Schaffert
*/
public class SortFunction implements SelectorFunction {
private StringTransformer transformer;
private DateTimeTransformer dateTransformer;
public SortFunction() {
transformer = new StringTransformer();
dateTransformer = new DateTimeTransformer();
}
/**
* Apply the function to the list of nodes passed as arguments and return the result as type T.
* Throws IllegalArgumentException if the function cannot be applied to the nodes passed as argument
* or the number of arguments is not correct.
*
* @param args a nested list of KiWiNodes
* @return
*/
@Override
public Collection apply(final RDFBackend nodeRDFBackend, Collection... args) throws IllegalArgumentException {
String order = "string";
String direction = "asc";
// parse arguments
if(args.length > 1) {
order = transformer.transform(nodeRDFBackend,args[1].iterator().next());
}
if(args.length > 2) {
direction = transformer.transform(nodeRDFBackend,args[2].iterator().next());
}
Comparator comparator = null;
// some local classes for carrying out the comparison
if("string".equalsIgnoreCase(order)) {
comparator = new Comparator() {
Collator stringCollator = Collator.getInstance(Locale.getDefault());
@Override
public int compare(Node o1, Node o2) {
return stringCollator.compare(transformer.transform(nodeRDFBackend,o1),
transformer.transform(nodeRDFBackend,o2));
}
};
} else if("number".equalsIgnoreCase(order)) {
comparator = new Comparator() {
@Override
public int compare(Node o1, Node o2) {
return (int)Math.signum(nodeRDFBackend.doubleValue(o2) - nodeRDFBackend.doubleValue(o1));
}
};
} else if("date".equalsIgnoreCase(order)) {
comparator = new Comparator() {
@Override
public int compare(Node o1, Node o2) {
return (int) (dateTransformer.transform(nodeRDFBackend,o2).getTime() -
dateTransformer.transform(nodeRDFBackend,o1).getTime());
}
};
}
if("desc".equalsIgnoreCase(direction) && comparator != null) {
final Comparator comparator2 = comparator;
comparator = new Comparator() {
@Override
public int compare(Node o1, Node o2) {
return comparator2.compare(o2,o1);
}
};
}
List result = new ArrayList(args[0]);
if(comparator != null) {
java.util.Collections.sort(result,comparator);
}
return result;
}
/**
* Return the representation of the NodeFunction or NodeSelector in the RDF Path Language
*
* @param nodeRDFBackend
* @return
*/
@Override
public String getPathExpression(RDFBackend nodeRDFBackend) {
return "sort";
}
}