All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.linkedopenactors.rdfpub.domain.RdfRevisionContainerDefault Maven / Gradle / Ivy

The newest version!
package org.linkedopenactors.rdfpub.domain;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;
import java.util.stream.Collectors;

import org.eclipse.rdf4j.model.Literal;
import org.eclipse.rdf4j.model.Model;

import de.naturzukunft.rdf4j.vocabulary.SCHEMA_ORG;
import lombok.ToString;

@ToString
class RdfRevisionContainerDefault implements RdfRevisionContainer {

	private List rdfObjectRevisions = new ArrayList<>();
	
	private String identifier;
	private Model model;

	private DomainObjectFactory domainObjectFactory;

	private Separator seperator;
	
	public RdfRevisionContainerDefault(Separator seperator, Model model) {
		this.model = model;
		Set identifiers = extractIdentifiers(model);		
		if(identifiers.size()!=1) {
			throw new IllegalStateException("exact one identifier expected, but is: " + identifiers);
		}
		this.identifier = identifiers.stream().findFirst().orElseThrow();

		Map> objectsByVersion = seperator.seperate(model, identifier);
		
		for (Entry> entry : objectsByVersion.entrySet()) {
			rdfObjectRevisions.add(new RdfObjectRevisionDefault(identifier, entry.getKey()/*version*/, entry.getValue()/*RdfObject*/));	
		}		
	}
	
	private Set extractIdentifiers(Model model) {
		return model.filter(null, SCHEMA_ORG.identifier, null).stream()
				.map(stmt-> stmt.getObject())
				.filter(object->object.isLiteral())
				.map(Literal.class::cast)
				.map(literal->literal.stringValue())
				.collect(Collectors.toSet());
	}

	@Override
	public String getIdentifier() {
		return identifier;
	}
	
	@Override
	public RdfObjectRevision latest() {
		return rdfObjectRevisions.stream()
			.max(Comparator.comparing(RdfObjectRevision::getVersion))
			.orElseThrow(()->new IllegalStateException("no latest RdfObjectRevision: " + rdfObjectRevisions));
	}

	@Override
	public Optional byVersion(long version) {		
		List rdfObjectRevisionList = rdfObjectRevisions.stream()
				.filter(r->r.getVersion().equals(version))
				.collect(Collectors.toList());
		if(rdfObjectRevisionList.size() > 1) {
			throw new IllegalStateException("more than one RdfObjectRevision for version "+version + " : " + rdfObjectRevisions);
		}		
		return Optional.ofNullable(rdfObjectRevisionList.get(0));
	}

	@Override
	public List getRdfObjectRevisions() {
		return rdfObjectRevisions;
	}

	@Override
	public Model getModel() {
		return model;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy