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

com.metreeca.json.shapes.Range Maven / Gradle / Ivy

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 com.metreeca.json.Values;

import org.eclipse.rdf4j.model.Value;

import java.util.*;

import static com.metreeca.json.Values.format;
import static com.metreeca.json.shapes.And.and;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static java.util.Collections.unmodifiableSet;
import static java.util.stream.Collectors.joining;


/**
 * Range value constraint.
 *
 * 

States that each value in the focus set is a member of the given set of target values.

*/ public final class Range extends Shape { public static Shape range(final Value... values) { if ( values == null || stream(values).anyMatch(Objects::isNull) ) { throw new NullPointerException("null values"); } return range(asList(values)); } public static Shape range(final Collection values) { if ( values == null || values.stream().anyMatch(Objects::isNull) ) { throw new NullPointerException("null values"); } return values.isEmpty() ? and() : new Range(values); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private final Set values; private Range(final Collection values) { this.values=new LinkedHashSet<>(values); } public Set values() { return unmodifiableSet(values); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @Override public T 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 Range && values.equals(((Range)object).values); } @Override public int hashCode() { return values.hashCode(); } @Override public String toString() { return "range("+values.stream() .map(v -> Values.indent(format(v))) .collect(joining(",\n\t", "\n\t", "\n")) +")"; } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy