001/*
002 * Copyright 2014 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.common;
017
018import org.gbif.api.util.IdentifierUtils;
019import org.gbif.api.vocabulary.IdentifierType;
020
021import javax.annotation.Nullable;
022import javax.validation.constraints.NotNull;
023
024import com.google.common.base.Objects;
025
026/**
027 * Identifier Model Object represents an alternative identifier for an occurrence or name usage.
028 *
029 * @see <a href="http://rs.gbif.org/extension/gbif/1.0/identifier.xml">Identifier Definition</a>
030 */
031public class Identifier {
032
033  private String identifier;
034  private String title;
035  private IdentifierType type;
036
037  /**
038   * Other known identifier used for the same taxon. Can be a URL pointing to a webpage, an xml or rdf document, a DOI,
039   * UUID or any other identifer.
040   * <blockquote>
041   * <p>
042   * <i>Example:</i> urn:lsid:ipni.org:names:692570-1:1.4
043   * </p>
044   * </blockquote>
045   *
046   * @return the identifier.
047   */
048  @NotNull
049  public String getIdentifier() {
050    return identifier;
051  }
052
053  /**
054   * @param identifier the identifier to set
055   */
056  public void setIdentifier(String identifier) {
057    this.identifier = identifier;
058  }
059
060  /**
061   * The optional title of an identifier, mostly for linking.
062   *
063   * @return the title
064   */
065  @Nullable
066  public String getTitle() {
067    return title;
068  }
069
070  /**
071   * @param title the identifier title to set
072   */
073  public void setTitle(String title) {
074    this.title = title;
075  }
076
077  /**
078   * Type of identifier.
079   *
080   * @return the type
081   *
082   * @see IdentifierType
083   */
084  @NotNull
085  public IdentifierType getType() {
086    return type;
087  }
088
089  /**
090   * @param type the type to set
091   */
092  public void setType(IdentifierType type) {
093    this.type = type;
094  }
095
096  /**
097   * Creates a http link for an identifier if possible by passing it to some known resolvers for the specific id type.
098   * If no link can be constructed, null is returned.
099   *
100   * @return the url or null if it cannot be created
101   *
102   * @see org.gbif.api.util.IdentifierUtils#getIdentifierLink(String, org.gbif.api.vocabulary.IdentifierType)
103   */
104  @Nullable
105  public String getIdentifierLink() {
106    return IdentifierUtils.getIdentifierLink(identifier, type);
107  }
108
109  @Override
110  public boolean equals(Object obj) {
111    if (this == obj) {
112      return true;
113    }
114    if (!(obj instanceof Identifier)) {
115      return false;
116    }
117
118    Identifier that = (Identifier) obj;
119    return Objects.equal(this.identifier, that.identifier)
120           && Objects.equal(this.type, that.type)
121           && Objects.equal(this.title, that.title);
122  }
123
124  @Override
125  public int hashCode() {
126    return Objects.hashCode(identifier, type, title);
127  }
128
129  @Override
130  public String toString() {
131    return Objects.toStringHelper(this)
132      .add("identifier", identifier)
133      .add("type", type)
134      .add("title", title)
135      .toString();
136  }
137
138}