/*
* Copyright 2011 Revelytix Inc.
*
* 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 spark.spi;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import spark.api.Command;
import spark.api.Solutions;
import spark.api.exception.SparqlException;
import spark.api.rdf.RDFNode;
public class SolutionSet extends BaseSolutions implements Solutions {
private final List<Map<String,RDFNode>> data;
private static final int BEFORE_FIRST = -1;
private static final int FIRST = 0;
private volatile int cursor = BEFORE_FIRST;
public SolutionSet(Command command, List<String> vars, List<Map<String,RDFNode>> data) {
super(command, vars);
this.data = data;
}
@Override
public Map<String, RDFNode> getResult() {
return data.get(cursor);
}
@Override
public boolean isAfterLast() {
return cursor >= data.size();
}
@Override
public boolean isBeforeFirst() {
return cursor == BEFORE_FIRST;
}
@Override
public boolean isFirst() {
return cursor == FIRST;
}
@Override
public boolean isLast() {
return data.size() > 0 && cursor == (data.size() - 1);
}
@Override
public boolean next() {
cursor++;
return cursor < data.size();
}
@Override
public int getRow() {
return cursor+1;
}
@Override
public Iterator<Map<String, RDFNode>> iterator() {
return new SolutionIterator();
}
private class SolutionIterator implements Iterator<Map<String,RDFNode>> {
int iterCursor = -1;
@Override
public boolean hasNext() {
return (iterCursor + 1) < data.size();
}
@Override
public Map<String,RDFNode> next() {
iterCursor++;
if (data.size() > 0 && iterCursor < data.size()) {
return data.get(iterCursor);
} else {
return null;
}
}
@Override
public void remove() {
throw new SparqlException("remove not supported on Solutions");
}
}
}