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

org.molgenis.data.Fetch Maven / Gradle / Ivy

There is a newer version: 8.4.5
Show newest version
package org.molgenis.data;

import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.molgenis.data.meta.model.Attribute;

/**
 * {@link Fetch} that defines which entity attributes to retrieve. For attributes referring to
 * entities a Fetch can be supplied that defines which entity attributes to retrieve for the
 * referred entity.
 *
 * 

A null Fetch means that all attributes should be retrieved. */ public class Fetch implements Iterable> { private final Map attrFetchMap; private final boolean validated; /** Creates an empty Fetch. */ public Fetch() { this(false); } /** * Creates an empty valid Fetch. Can be used to improve performance in case of system entity type * fetches defined by the system. * * @param validated whether or not the fetch has been validated */ public Fetch(boolean validated) { this.attrFetchMap = new LinkedHashMap<>(); this.validated = validated; } /** * Updates this Fetch, adding a single field. If the field is a reference, the reference will be * fetched with a null Fetch, which means that all attributes will be fetched. * * @param field the name of the field to fetch * @return this Fetch, updated */ public Fetch field(String field) { return field(field, null); } /** * Updates this fetch, adding a single field. If the field is a reference, the reference will be * fetched with the Fetch that is provided. * * @param field the name of the field to fetch * @param fetch the fetch to use for this field, if the field is a reference * @return this Fetch, updated */ public Fetch field(String field, Fetch fetch) { attrFetchMap.put(field, fetch); return this; } /** * Retrieves the Fetch for a particular field * * @param field the field for which the Fetch is retrieved * @return the Fetch for this field, or null if none was provided */ @Nullable @CheckForNull public Fetch getFetch(String field) { return attrFetchMap.get(field); } /** Retrieves the Fetch for an attribute */ @Nullable @CheckForNull public Fetch getFetch(Attribute attr) { return getFetch(attr.getName()); } /** * Indicates if a field is included in this Fetch. * * @param field the field that is queried * @return true if the field is included in this Fetch, otherwise false */ public boolean hasField(String field) { return attrFetchMap.containsKey(field); } /** * Indicates if a field is included in this Fetch * * @param attr {@link Attribute} for the field * @return true if the field is included in this Fetch, otherwise false */ public boolean hasField(Attribute attr) { return hasField(attr.getName()); } /** * Retrieves the fields included in this Fetch * * @return {@link Set} containing the names of all fields included in this Fetch */ public Set getFields() { return Collections.unmodifiableMap(attrFetchMap).keySet(); } public boolean isValidated() { return validated; } /** * Iterates over all fields in this fetch. The key is the field name, the value is the {@link * Fetch} for that field, or null if no Fetch is provided for that field. * * @return {@link Iterator} over all {@link Entry}s in this fetch. */ @Override public Iterator> iterator() { return Collections.unmodifiableMap(attrFetchMap).entrySet().iterator(); } @Override public boolean equals(Object o) { if (this == o) { return true; } if (o == null || getClass() != o.getClass()) { return false; } Fetch fetch = (Fetch) o; return validated == fetch.validated && attrFetchMap.equals(fetch.attrFetchMap); } @Override public int hashCode() { return Objects.hash(attrFetchMap, validated); } @Override public String toString() { StringBuilder builder = new StringBuilder(); toStringRec(builder, this); return builder.toString(); } private void toStringRec(StringBuilder builder, Fetch fetch) { builder.append('('); for (Iterator> it = fetch.iterator(); it.hasNext(); ) { Entry entry = it.next(); builder.append(entry.getKey()); Fetch subFetch = entry.getValue(); if (subFetch != null) { toStringRec(builder, subFetch); } if (it.hasNext()) { builder.append(','); } } builder.append(')'); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy