001package io.ebean;
002
003import java.util.LinkedHashSet;
004import java.util.Set;
005
006/**
007 * Builds a MergeOptions which is immutable and thread safe.
008 */
009public class MergeOptionsBuilder {
010
011  private static final MOptions DEFAULT_OPTIONS = new MOptions();
012
013  private Set<String> paths = new LinkedHashSet<>();
014
015  private boolean clientGeneratedIds;
016
017  private boolean deletePermanent;
018
019  /**
020   * Return the default options.
021   */
022  public static MergeOptions defaultOptions() {
023    return DEFAULT_OPTIONS;
024  }
025
026  /**
027   * Add a path that will included in the merge.
028   *
029   * @param path The path relative to the root type.
030   * @return The builder to chain another addPath() or build().
031   */
032  public MergeOptionsBuilder addPath(String path) {
033    paths.add(path);
034    return this;
035  }
036
037  /**
038   * Set to true if Id values are supplied by the client.
039   * <p>
040   * This would be the case when for example a mobile creates data in it's own local database
041   * and then sync's. In this case often the id values are UUID.
042   */
043  public MergeOptionsBuilder setClientGeneratedIds() {
044    this.clientGeneratedIds = true;
045    return this;
046  }
047
048  /**
049   * Set that deletions should use delete permanent (rather than default which allows soft deletes).
050   */
051  public MergeOptionsBuilder setDeletePermanent() {
052    this.deletePermanent = true;
053    return this;
054  }
055
056  /**
057   * Build and return the MergeOptions instance.
058   */
059  public MergeOptions build() {
060    return new MOptions(paths, clientGeneratedIds, deletePermanent);
061  }
062
063  private static class MOptions implements MergeOptions {
064
065    private final boolean clientGeneratedIds;
066    private final boolean deletePermanent;
067    private final Set<String> paths;
068
069    private MOptions(){
070      this.clientGeneratedIds = false;
071      this.paths = new LinkedHashSet<>();
072      this.deletePermanent = false;
073    }
074
075    private MOptions(Set<String> paths, boolean clientGeneratedIds, boolean deletePermanent) {
076      this.paths = paths;
077      this.clientGeneratedIds = clientGeneratedIds;
078      this.deletePermanent = deletePermanent;
079    }
080
081    @Override
082    public Set<String> paths() {
083      return paths;
084    }
085
086    @Override
087    public boolean isClientGeneratedIds() {
088      return clientGeneratedIds;
089    }
090
091    @Override
092    public boolean isDeletePermanent() {
093      return deletePermanent;
094    }
095  }
096}