001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.model.common;
015
016import org.gbif.api.util.IdentifierUtils;
017import org.gbif.api.vocabulary.IdentifierType;
018
019import java.util.Objects;
020import java.util.StringJoiner;
021
022import javax.annotation.Nullable;
023import javax.validation.constraints.NotNull;
024
025import io.swagger.v3.oas.annotations.media.Schema;
026
027/**
028 * Identifier Model Object represents an alternative identifier for an occurrence or name usage.
029 *
030 * @see <a href="http://rs.gbif.org/extension/gbif/1.0/identifier.xml">Identifier Definition</a>
031 */
032public class Identifier {
033
034  private String identifier;
035  private String title;
036  private IdentifierType type;
037
038  /**
039   * Other known identifier used for the same taxon. Can be a URL pointing to a webpage, an XML or RDF document, a DOI,
040   * UUID or any other identifer.
041   * <blockquote>
042   * <p>
043   * <i>Example:</i> urn:lsid:ipni.org:names:692570-1:1.4
044   * </p>
045   * </blockquote>
046   *
047   * @return the identifier.
048   */
049  @Schema(description = "Other known identifier used for the same taxon. Can be a URL pointing to a webpage, an XML or " +
050    "RDF document, a DOI UUID or any other identifer.")
051  @NotNull
052  public String getIdentifier() {
053    return identifier;
054  }
055
056  /**
057   * @param identifier the identifier to set
058   */
059  public void setIdentifier(String identifier) {
060    this.identifier = identifier;
061  }
062
063  /**
064   * The optional title of an identifier, mostly for linking.
065   *
066   * @return the title
067   */
068  @Schema(description = "The optional title of an identifier, mostly for linking.")
069  @Nullable
070  public String getTitle() {
071    return title;
072  }
073
074  /**
075   * @param title the identifier title to set
076   */
077  public void setTitle(String title) {
078    this.title = title;
079  }
080
081  /**
082   * Type of identifier.
083   *
084   * @return the type
085   *
086   * @see IdentifierType
087   */
088  @Schema(description = "Type of identifier.")
089  @NotNull
090  public IdentifierType getType() {
091    return type;
092  }
093
094  /**
095   * @param type the type to set
096   */
097  public void setType(IdentifierType type) {
098    this.type = type;
099  }
100
101  /**
102   * Creates an HTTP link for an identifier if possible by passing it to some known resolvers for the specific id type.
103   * If no link can be constructed, null is returned.
104   *
105   * @return the url or null if it cannot be created
106   *
107   * @see org.gbif.api.util.IdentifierUtils#getIdentifierLink(String, org.gbif.api.vocabulary.IdentifierType)
108   */
109  @Schema(description = "An HTTP link for an identifier, if a suitable, known resolver exists for the identifier type.")
110  @Nullable
111  public String getIdentifierLink() {
112    return IdentifierUtils.getIdentifierLink(identifier, type);
113  }
114
115  @Override
116  public boolean equals(Object o) {
117    if (this == o) {
118      return true;
119    }
120    if (o == null || getClass() != o.getClass()) {
121      return false;
122    }
123    Identifier that = (Identifier) o;
124    return Objects.equals(identifier, that.identifier) &&
125      Objects.equals(title, that.title) &&
126      type == that.type;
127  }
128
129  @Override
130  public int hashCode() {
131    return Objects.hash(identifier, title, type);
132  }
133
134  @Override
135  public String toString() {
136    return new StringJoiner(", ", Identifier.class.getSimpleName() + "[", "]")
137      .add("identifier='" + identifier + "'")
138      .add("title='" + title + "'")
139      .add("type=" + type)
140      .toString();
141  }
142}