net.binis.codegen.collection.EmbeddedCodeCollectionImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of code-generator-core Show documentation
Show all versions of code-generator-core Show documentation
Binis Code Generation Core Classes
package net.binis.codegen.collection;
/*-
* #%L
* code-generator-core
* %%
* Copyright (C) 2021 - 2024 Binis Belev
* %%
* 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%
*/
import net.binis.codegen.factory.CodeFactory;
import net.binis.codegen.modifier.Modifier;
import java.util.Collection;
import java.util.List;
import java.util.Optional;
import java.util.function.Consumer;
import java.util.function.Predicate;
import java.util.function.UnaryOperator;
import java.util.stream.Stream;
import static java.util.Objects.nonNull;
public abstract class EmbeddedCodeCollectionImpl implements EmbeddedCodeCollection, Modifier {
protected final Collection collection;
protected final Consumer validator;
protected R parent;
protected final Class cls;
protected EmbeddedCodeCollectionImpl(R parent, Collection collection, Class cls) {
this.parent = parent;
this.collection = collection;
this.cls = cls;
this.validator = null;
}
protected EmbeddedCodeCollectionImpl(R parent, Collection collection, Class cls, Consumer validator) {
this.parent = parent;
this.collection = collection;
this.cls = cls;
this.validator = validator;
}
@Override
public EmbeddedCodeCollection _add(T value) {
validate(value);
collection.add(value);
return this;
}
@Override
public EmbeddedCodeCollection _add$(UnaryOperator init) {
T value = CodeFactory.create(cls);
validate(value);
collection.add(value);
init.apply(CodeFactory.modify(this, value, cls));
return this;
}
@Override
public EmbeddedCodeCollection _remove(T value) {
collection.remove(value);
return this;
}
@Override
public EmbeddedCodeCollection _clear() {
collection.clear();
return this;
}
@Override
public EmbeddedCodeCollection _each(Consumer doWhat) {
collection.forEach(e -> doWhat.accept(CodeFactory.modify(this, e, cls)));
return this;
}
@Override
public EmbeddedCodeCollection _ifEmpty(Consumer> doWhat) {
if (collection.isEmpty()) {
doWhat.accept(this);
}
return this;
}
@Override
public EmbeddedCodeCollection _ifNotEmpty(Consumer> doWhat) {
if (!collection.isEmpty()) {
doWhat.accept(this);
}
return this;
}
@Override
public EmbeddedCodeCollection _ifContains(T value, Consumer> doWhat) {
if (collection.contains(value)) {
doWhat.accept(this);
}
return this;
}
@Override
public EmbeddedCodeCollection _ifContains(Predicate predicate, Consumer> doWhat) {
if (collection.stream().anyMatch(predicate)) {
doWhat.accept(this);
}
return this;
}
@Override
public EmbeddedCodeCollection _ifNotContains(T value, Consumer> doWhat) {
if (!collection.contains(value)) {
doWhat.accept(this);
}
return this;
}
@Override
public EmbeddedCodeCollection _ifNotContains(Predicate predicate, Consumer> doWhat) {
if (collection.stream().noneMatch(predicate)) {
doWhat.accept(this);
}
return this;
}
@Override
public M _add() {
T value = CodeFactory.create(cls);
collection.add(value);
return CodeFactory.modify(this, value, cls);
}
@SuppressWarnings("unchecked")
@Override
public Optional _find(Predicate predicate) {
return collection.stream().filter(predicate).map(e -> (M) CodeFactory.modify(this, e, cls)).findFirst();
}
@SuppressWarnings("unchecked")
@Override
public List _findAll(Predicate predicate) {
return collection.stream().filter(predicate).map(e -> (M) CodeFactory.modify(this, e, cls)).toList();
}
@Override
public R done() {
return parent;
}
public Stream _stream() {
return collection.stream();
}
@Override
public R getObject() {
return parent;
}
@Override
public void setObject(R object) {
parent = object;
}
protected void validate(T value) {
if (nonNull(validator)) {
validator.accept(value);
}
}
}