001/*
002 * Copyright 2020 Global Biodiversity Information Facility (GBIF)
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package org.gbif.api.model.metrics.cube;
017
018import org.gbif.api.vocabulary.Country;
019import org.gbif.api.vocabulary.Language;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.Objects;
024import java.util.UUID;
025
026import javax.annotation.concurrent.NotThreadSafe;
027
028/**
029 * Provides building of addresses for reading the cube. This class ensures the type safety of
030 * dimensions as they are added to the builder.
031 */
032@NotThreadSafe
033public class ReadBuilder {
034
035  private final Map<Dimension<?>, String> address = new HashMap<>();
036
037  /**
038   * Adds an country type dimension to the address.
039   */
040  public ReadBuilder at(Dimension<Country> dim, Country value) {
041    Objects.requireNonNull(value, "Dimension cannot be null");
042    address.put(dim, value.getIso2LetterCode());
043    return this;
044  }
045
046  /**
047   * Adds an language type dimension to the address.
048   */
049  public ReadBuilder at(Dimension<Language> dim, Language value) {
050    Objects.requireNonNull(value, "Dimension cannot be null");
051    address.put(dim, value.getIso2LetterCode());
052    return this;
053  }
054
055  /**
056   * Adds an enumerated type dimension to the address.
057   */
058  public ReadBuilder at(Dimension<? extends Enum<?>> dim, Enum<?> value) {
059    Objects.requireNonNull(value, "Dimension cannot be null");
060    address.put(dim, value.name());
061    return this;
062  }
063
064  /**
065   * Adds a boolen typed dimension to the address.
066   */
067  public ReadBuilder at(Dimension<Boolean> dim, boolean value) {
068    address.put(dim, String.valueOf(value));
069    return this;
070  }
071
072  /**
073   * Adds a double typed dimension to the address.
074   */
075  public ReadBuilder at(Dimension<Double> dim, double value) {
076    address.put(dim, String.valueOf(value));
077    return this;
078  }
079
080  /**
081   * Adds a float typed dimension to the address.
082   */
083  public ReadBuilder at(Dimension<Float> dim, float value) {
084    address.put(dim, String.valueOf(value));
085    return this;
086  }
087
088  /**
089   * Adds an integer typed dimension to the address.
090   */
091  public ReadBuilder at(Dimension<Integer> dim, int value) {
092    address.put(dim, String.valueOf(value));
093    return this;
094  }
095
096  /**
097   * Adds an String typed dimension to the address.
098   */
099  public ReadBuilder at(Dimension<String> dim, String value) {
100    Objects.requireNonNull(value, "Dimension cannot be null");
101    address.put(dim, value);
102    return this;
103  }
104
105  /**
106   * Adds a UUID typed dimension to the address.
107   */
108  public ReadBuilder at(Dimension<UUID> dim, UUID value) {
109    Objects.requireNonNull(value, "Dimension cannot be null");
110    address.put(dim, value.toString());
111    return this;
112  }
113
114  /**
115   * @return The built address.
116   */
117  public Map<Dimension<?>, String> build() {
118    return address;
119  }
120}