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.registry.eml.temporal;
017
018import org.gbif.api.util.formatter.TemporalCoverageFormatterVisitor;
019
020import java.io.Serializable;
021import java.text.SimpleDateFormat;
022import java.util.ArrayList;
023import java.util.Collection;
024import java.util.Date;
025import java.util.Objects;
026import java.util.StringJoiner;
027
028/**
029 * A single instance in time.
030 */
031public class SingleDate extends TemporalCoverage implements Serializable {
032
033  private static final long serialVersionUID = 7528038100592298938L;
034
035  private Date date;
036
037
038  public SingleDate() {
039  }
040
041  public Date getDate() {
042    return date;
043  }
044
045  public void setDate(Date date) {
046    this.date = date;
047  }
048
049  @Override
050  public Collection<String> toKeywords() {
051    Collection<String> keywords = new ArrayList<>();
052    if (date != null) {
053      SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
054      keywords.add(sdf.format(date));
055    }
056    return keywords;
057  }
058
059  @Override
060  public boolean equals(Object o) {
061    if (this == o) {
062      return true;
063    }
064    if (o == null || getClass() != o.getClass()) {
065      return false;
066    }
067    SingleDate that = (SingleDate) o;
068    return Objects.equals(date, that.date);
069  }
070
071  @Override
072  public int hashCode() {
073    return Objects.hash(date);
074  }
075
076  @Override
077  public String toString() {
078    return new StringJoiner(", ", SingleDate.class.getSimpleName() + "[", "]")
079      .add("date=" + date)
080      .toString();
081  }
082
083  @Override
084  public String acceptFormatter(TemporalCoverageFormatterVisitor formatter) {
085    return formatter.format(this);
086  }
087}