001package org.galaxyproject.gxformat2.v19_09.utils;
002
003import java.util.HashMap;
004import java.util.List;
005import java.util.Map;
006import java.util.Optional;
007
008public class LoadingOptionsBuilder {
009  private Optional<Fetcher> fetcher = Optional.empty();
010  private Optional<String> fileUri = Optional.empty();
011  private Optional<Map<String, String>> namespaces = Optional.empty();
012  private Optional<List<String>> schemas = Optional.empty();
013  private Optional<LoadingOptions> copyFrom = Optional.empty();
014
015  public LoadingOptionsBuilder() {}
016
017  public LoadingOptionsBuilder setFetcher(final Fetcher fetcher) {
018    this.fetcher = Optional.of(fetcher);
019    return this;
020  }
021
022  public LoadingOptionsBuilder copiedFrom(final LoadingOptions copyFrom) {
023    this.copyFrom = Optional.of(copyFrom);
024    return this;
025  }
026
027  public LoadingOptionsBuilder setFileUri(final String fileUri) {
028    this.fileUri = Optional.of(fileUri);
029    return this;
030  }
031
032  public LoadingOptionsBuilder setNamespaces(final Map<String, String> namespaces) {
033    this.namespaces = Optional.of(namespaces);
034    return this;
035  }
036
037  public LoadingOptions build() {
038    Fetcher fetcher = this.fetcher.orElse(null);
039    String fileUri = this.fileUri.orElse(null);
040    List<String> schemas = this.schemas.orElse(null);
041    Map<String, String> namespaces = this.namespaces.orElse(null);
042    Map<String, Map<String, Object>> idx = new HashMap<String, Map<String, Object>>();
043    if (this.copyFrom.isPresent()) {
044      final LoadingOptions copyFrom = this.copyFrom.get();
045      idx = copyFrom.idx;
046      if (fetcher == null) {
047        fetcher = copyFrom.fetcher;
048      }
049      if (fileUri == null) {
050        fileUri = copyFrom.fileUri;
051      }
052      if (namespaces == null) {
053        namespaces = copyFrom.namespaces;
054        schemas = copyFrom.schemas; // Bug in Python codegen?
055      }
056    }
057    if (fetcher == null) {
058      fetcher = new DefaultFetcher();
059    }
060    return new LoadingOptions(fetcher, fileUri, namespaces, schemas, idx);
061  }
062}