
org.codegeny.beans.model.SetModel Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of codegeny-beans Show documentation
Show all versions of codegeny-beans Show documentation
Library which provides meta-model, diffs, paths... for java beans
/*-
* #%L
* codegeny-beans
* %%
* Copyright (C) 2016 - 2018 Codegeny
* %%
* 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.
* #L%
*/
package org.codegeny.beans.model;
import java.util.Set;
import java.util.function.Function;
import static java.util.Collections.emptySet;
import static java.util.Objects.requireNonNull;
/**
* An implementation of {@link Model} for a set.
*
* @param The type of the set.
* @param The type of the element.
* @author Xavier DURY
*/
public final class SetModel implements Model {
private final Model elementModel;
private final Function super S, ? extends Set> extractor;
SetModel(Function super S, ? extends Set> extractor, Model elementModel) {
this.extractor = requireNonNull(extractor);
this.elementModel = requireNonNull(elementModel);
}
/**
* {@inheritDoc}
*/
@Override
public R accept(ModelVisitor visitor) {
return requireNonNull(visitor).visitSet(this);
}
/**
* Visit the element model.
*
* @param visitor The visitor.
* @param The result type.
* @return A result of type <R>.
*/
public R acceptElement(ModelVisitor visitor) {
return elementModel.accept(visitor);
}
/**
* Transform a set of type <S> to Set<E>
*
* @param values The values.
* @return A set of elements.
*/
public Set toSet(S values) {
return values == null ? emptySet() : extractor.apply(values);
}
/**
* Get the element model.
*
* @return The element model.
*/
public Model getElementModel() {
return elementModel;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy