io.micronaut.data.model.PersistentPropertyPath Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of micronaut-data-model Show documentation
Show all versions of micronaut-data-model Show documentation
Data Repository Support for Micronaut
The newest version!
/*
* Copyright 2017-2021 original authors
*
* 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
*
* https://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 io.micronaut.data.model;
import io.micronaut.core.annotation.NonNull;
import io.micronaut.core.annotation.Nullable;
import io.micronaut.core.beans.BeanProperty;
import io.micronaut.data.model.naming.NamingStrategy;
import io.micronaut.data.model.runtime.RuntimePersistentProperty;
import java.util.ArrayList;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import java.util.Optional;
import java.util.StringJoiner;
/**
* The property path representation.
*
* @author Denis Stepanov
* @since 2.4.0
*/
public class PersistentPropertyPath {
private final List associations;
private final PersistentProperty property;
private String path;
/**
* Default constructor.
*
* @param property The property
*/
public PersistentPropertyPath(@NonNull PersistentProperty property) {
this(List.of(), property, null);
}
/**
* Default constructor.
*
* @param associations The associations
* @param property The property
*/
public PersistentPropertyPath(@NonNull List associations, @NonNull PersistentProperty property) {
this(associations, property, null);
}
/**
* Default constructor.
*
* @param associations The associations
* @param property The property
* @param path The path
*/
public PersistentPropertyPath(@NonNull List associations, @NonNull PersistentProperty property, @Nullable String path) {
Objects.requireNonNull(associations);
Objects.requireNonNull(property);
this.associations = associations;
this.property = property;
this.path = path;
}
/**
* Creates {@link PersistentPropertyPath} or {@link PersistentAssociationPath}.
*
* @param associations The associations
* @param property The property
* @return new instance of {@link PersistentPropertyPath} or {@link PersistentAssociationPath}
*/
public static PersistentPropertyPath of(List associations, @NonNull PersistentProperty property) {
return of(associations, property, null);
}
/**
* Creates {@link PersistentPropertyPath} or {@link PersistentAssociationPath}.
*
* @param associations The associations
* @param property The property
* @param path The path
* @return new instance of {@link PersistentPropertyPath} or {@link PersistentAssociationPath}
*/
public static PersistentPropertyPath of(List associations, @NonNull PersistentProperty property, @Nullable String path) {
if (property instanceof Association association) {
return new PersistentAssociationPath(associations, association, path);
}
return new PersistentPropertyPath(associations, property, path);
}
/**
* Sets property path value.
* (Only possible for runtime properties)
*
* @param bean The root bean
* @param value The value
* @return The root bean - possibly modified
*/
public Object setPropertyValue(Object bean, Object value) {
if (!(property instanceof RuntimePersistentProperty> runtimeProperty)) {
throw new IllegalStateException("Expected runtime property!");
}
return setProperty(associations, runtimeProperty, bean, value);
}
private Object setProperty(List associations, RuntimePersistentProperty> property, Object bean, Object value) {
if (associations.isEmpty()) {
BeanProperty beanProperty = property.getProperty();
return setProperty(beanProperty, bean, value);
}
Association association = associations.iterator().next();
RuntimePersistentProperty> p = (RuntimePersistentProperty) association;
BeanProperty beanProperty = p.getProperty();
Object prevBean = beanProperty.get(bean);
Object newBean = setProperty(associations.subList(1, associations.size()), property, prevBean, value);
if (prevBean != newBean) {
return setProperty(beanProperty, bean, newBean);
}
return bean;
}
private X setProperty(BeanProperty beanProperty, X x, Y y) {
if (beanProperty.isReadOnly()) {
return beanProperty.withValue(x, y);
}
beanProperty.set(x, y);
return x;
}
/**
* Gets property path value.
* (Only possible for runtime properties)
*
* @param bean The root bean
* @return The value
*/
public Object getPropertyValue(Object bean) {
if (!(property instanceof RuntimePersistentProperty p)) {
throw new IllegalStateException("Expected runtime property!");
}
Object value = bean;
for (Association association : associations) {
RuntimePersistentProperty
© 2015 - 2025 Weber Informatics LLC | Privacy Policy