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