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

com.metreeca.tree.shapes.Clazz Maven / Gradle / Ivy

There is a newer version: 0.53.0
Show newest version
/*
 * Copyright © 2013-2019 Metreeca srl. All rights reserved.
 *
 * This file is part of Metreeca/Link.
 *
 * Metreeca/Link is free software: you can redistribute it and/or modify it under the terms
 * of the GNU Affero General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or(at your option) any later version.
 *
 * Metreeca/Link is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License along with Metreeca/Link.
 * If not, see .
 */

package com.metreeca.tree.shapes;

import com.metreeca.tree.Shape;
import com.metreeca.tree.probes.Inspector;

import java.util.*;

import static java.util.stream.Collectors.toSet;


/**
 * Class value constraint.
 *
 * 

States that each value in the focus set is a member of a given resource class or one of its superclasses.

*/ public final class Clazz implements Shape { public static Clazz clazz(final Object name) { return new Clazz(name); } public static Optional clazz(final Shape shape) { return shape == null ? Optional.empty() : Optional.ofNullable(shape.map(new ClazzProbe())); } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private final Object name; private Clazz(final Object name) { if ( name == null ) { throw new NullPointerException("null name"); } this.name=name; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// public Object getName() { return name; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// @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 Clazz && name.equals(((Clazz)object).name); } @Override public int hashCode() { return name.hashCode(); } @Override public String toString() { return "clazz("+name+")"; } //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// private static final class ClazzProbe extends Inspector { @Override public Object probe(final Clazz clazz) { return clazz.getName(); } @Override public Object probe(final And and) { return clazz(and.getShapes()); } @Override public Object probe(final Or or) { return clazz(or.getShapes()); } private Object clazz(final Collection shapes) { final Set names=shapes.stream() .map(shape -> shape.map(this)) .filter(Objects::nonNull) .collect(toSet()); return names.size() == 1 ? names.iterator().next() : null; } } }