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.common;
017
018import java.net.URI;
019import java.util.Objects;
020import java.util.StringJoiner;
021
022import javax.annotation.Nullable;
023import javax.validation.constraints.NotNull;
024
025import com.fasterxml.jackson.annotation.JsonCreator;
026import com.fasterxml.jackson.annotation.JsonProperty;
027
028/**
029 * Data about a DOI with a target URI and a status enumeration.
030 */
031public class DoiData {
032  private final DoiStatus status;
033  private final URI target;
034
035  @JsonCreator
036  public DoiData(@JsonProperty("status") DoiStatus status, @JsonProperty("target") URI target) {
037    this.status = Objects.requireNonNull(status, "DOI status is required");
038    this.target = target;
039  }
040
041  public DoiData(String ezidStatus, URI target) {
042    this.status = Objects.requireNonNull(DoiStatus.fromString(ezidStatus));
043    this.target = target;
044  }
045
046  public DoiData(DoiStatus status) {
047    this.status = Objects.requireNonNull(status, "DOI status is required");
048    this.target = null;
049  }
050
051  @NotNull
052  public DoiStatus getStatus() {
053    return status;
054  }
055
056  @Nullable
057  public URI getTarget() {
058    return target;
059  }
060
061  @Override
062  public boolean equals(Object o) {
063    if (this == o) {
064      return true;
065    }
066    if (o == null || getClass() != o.getClass()) {
067      return false;
068    }
069    DoiData doiData = (DoiData) o;
070    return status == doiData.status &&
071      Objects.equals(target, doiData.target);
072  }
073
074  @Override
075  public int hashCode() {
076    return Objects.hash(status, target);
077  }
078
079  @Override
080  public String toString() {
081    return new StringJoiner(", ", DoiData.class.getSimpleName() + "[", "]")
082      .add("status=" + status)
083      .add("target=" + target)
084      .toString();
085  }
086}