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 java.util.Objects;
019import java.util.StringJoiner;
020
021/**
022 * A typed dimension to a cube, which is constructed with a unique key. This key defines the HTTP parameter name during
023 * serialization, and the type is respected by the {@link ReadBuilder} during lookups.
024 *
025 * @param <T> The value type supported for the dimension
026 */
027public class Dimension<T> {
028  // not final to enable easy Jackson serialization
029  private String key;
030  private Class<T> type;
031
032  /**
033   * Force the provision of the key in construction, which should be unique for the cube.
034   *
035   * @param key Which is used in the HTTP parameters
036   * @param type For the dimension, stored to enable runtime checking where required
037   */
038  public Dimension(String key, Class<T> type) {
039    this.key = key;
040    this.type = type;
041  }
042
043  /**
044   * Prefer use of the constructor taking the parameters over this method.
045   * This is provided to allow easy Jackson serialization.
046   */
047  public Dimension() {
048  }
049
050  public String getKey() {
051    return key;
052  }
053
054  public Class<T> getType() {
055    return type;
056  }
057
058
059  public void setKey(String key) {
060    this.key = key;
061  }
062
063  public void setType(Class<T> type) {
064    this.type = type;
065  }
066
067  @Override
068  public boolean equals(Object o) {
069    if (this == o) {
070      return true;
071    }
072    if (o == null || getClass() != o.getClass()) {
073      return false;
074    }
075    Dimension<?> dimension = (Dimension<?>) o;
076    return Objects.equals(key, dimension.key) &&
077      Objects.equals(type, dimension.type);
078  }
079
080  @Override
081  public int hashCode() {
082    return Objects.hash(key, type);
083  }
084
085  @Override
086  public String toString() {
087    return new StringJoiner(", ", Dimension.class.getSimpleName() + "[", "]")
088      .add("key='" + key + "'")
089      .add("type=" + type)
090      .toString();
091  }
092}