com.metreeca.json.shapes.Link Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of metreeca-json Show documentation
Show all versions of metreeca-json Show documentation
A shape-based JSON modelling framework.
The newest version!
/*
* Copyright © 2013-2022 Metreeca srl
*
* 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 com.metreeca.json.shapes;
import com.metreeca.json.Shape;
import org.eclipse.rdf4j.model.IRI;
import java.util.Objects;
import static com.metreeca.json.Values.format;
import static com.metreeca.json.shapes.And.and;
import static com.metreeca.json.shapes.Or.or;
import static java.lang.String.format;
import static java.util.Arrays.stream;
/**
* Linking structural constraint.
*
* States that the derived focus set generated by virtually traversing a single step path linking a resource alias to
* its target resource is consistent with a given {@link Shape shape}.
*/
public final class Link extends Shape {
public static Shape link(final IRI iri, final Shape... shapes) {
if ( iri == null ) {
throw new NullPointerException("null iri");
}
if ( shapes == null || stream(shapes).anyMatch(Objects::isNull) ) {
throw new NullPointerException("null shape");
}
final Shape shape=and(shapes);
return shape.equals(or()) ? and() : new Link(iri, shape);
}
public static Shape link(final IRI iri, final Shape shape) {
if ( iri == null ) {
throw new NullPointerException("null iri");
}
if ( shape == null ) {
throw new NullPointerException("null shape");
}
return shape.equals(or()) ? and() : new Link(iri, shape);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
private final IRI iri;
private final Shape shape;
Link(final IRI iri, final Shape shape) {
this.iri=iri;
this.shape=shape;
}
public IRI iri() {
return iri;
}
public Shape shape() {
return shape;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override public V map(final Probe probe) {
if ( probe == null ) {
throw new NullPointerException("null probe");
}
return probe.probe(this);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
@Override public boolean equals(final Object object) {
return this == object || object instanceof Link
&& iri.equals(((Link)object).iri)
&& shape.equals(((Link)object).shape);
}
@Override public int hashCode() {
return iri.hashCode()^shape.hashCode();
}
@Override public String toString() {
return shape.equals(and())
? format("link(%s)", format(iri))
: format("link(%s, %s)", format(iri), shape);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy