com.sap.cds.impl.LazyRowImpl Maven / Gradle / Ivy
/************************************************************************
* © 2019-2022 SAP SE or an SAP affiliate company. All rights reserved. *
************************************************************************/
package com.sap.cds.impl;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import com.google.common.collect.ForwardingMap;
import com.google.common.collect.Maps;
import com.sap.cds.CdsData;
import com.sap.cds.CdsDataStore;
import com.sap.cds.NonUniqueResultException;
import com.sap.cds.Row;
import com.sap.cds.ql.StructuredType;
import com.sap.cds.ql.cqn.CqnSelect;
public class LazyRowImpl extends ForwardingMap implements Row, Lazy {
private final Supplier rowSupplier;
private boolean loaded;
private Row row;
public LazyRowImpl(Supplier rowSupplier) {
this.rowSupplier = rowSupplier;
}
public static LazyRowImpl lazyRow(CdsDataStore dataStore, CqnSelect query) {
return new LazyRowImpl(() -> {
List result = dataStore.execute(query).list();
if (result.size() == 1) {
return result.get(0);
}
if (result.size() > 1) {
throw new NonUniqueResultException("Query for single-valued association returned multiple rows");
}
return null;
});
}
private Row data() {
if (!loaded) {
load();
}
return row;
}
@Override
public String toJson() {
return row != null ? row.toJson() : null;
}
@Override
public String toString() {
return toJson();
}
@Override
protected Map delegate() {
return data();
}
@Override
public T as(Class type) {
return data().as(type);
}
@Override
public void load() {
row = rowSupplier.get();
loaded = true;
}
@Override
public boolean isLoaded() {
return loaded;
}
@Override
public Object loadData() {
Row data = data();
return (data != null) ? Maps.newHashMap(data) : null;
}
@Override
public StructuredType> ref() {
return data().ref();
}
@Override
public T getPath(String path) {
return data().getPath(path);
}
@Override
public T getPathOrDefault(String path, T defaultValue) {
return data().getPathOrDefault(path, defaultValue);
}
@Override
public T putPath(String path, T value) {
return data().putPath(path, value);
}
@Override
public T putPathIfAbsent(String path, T value) {
return data().putPathIfAbsent(path, value);
}
@Override
public boolean containsPath(String path) {
return data().containsPath(path);
}
@Override
public T removePath(String path) {
return data().removePath(path);
}
@Override
public T getMetadata(String key) {
return data().getMetadata(key);
}
@Override
public T putMetadata(String key, T value) {
return data().putMetadata(key, value);
}
@Override
public boolean isForRemoval() {
return false;
}
@Override
public CdsData forRemoval(boolean remove) {
throw new UnsupportedOperationException();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy