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