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