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.base.Preconditions;
027import com.google.common.collect.Lists;
028
029
030/**
031 * A period of time.
032 */
033public class DateRange extends TemporalCoverage implements Serializable {
034
035  private static final long serialVersionUID = -1482059589547915674L;
036
037  private Date start;
038
039  private Date end;
040
041  public DateRange() {
042  }
043
044  public DateRange(Date start, Date end) {
045    Preconditions.checkArgument(start.before(end), "start date must be before end");
046    this.start = start;
047    this.end = end;
048  }
049
050  public Date getEnd() {
051    return end;
052  }
053
054  public void setEnd(Date end) {
055    this.end = end;
056  }
057
058  public Date getStart() {
059    return start;
060  }
061
062  public void setStart(Date start) {
063    this.start = start;
064  }
065
066  @Override
067  public Collection<String> toKeywords() {
068    StringBuilder sb = new StringBuilder();
069    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
070    if (start != null) {
071      sb.append(sdf.format(start));
072    }
073    if (end != null) {
074      if (sb.length() > 0) {
075        sb.append("-");
076      }
077      sb.append(sdf.format(end));
078    }
079    return Lists.newArrayList(sb.toString());
080  }
081
082  @Override
083  public boolean equals(Object obj) {
084    if (this == obj) {
085      return true;
086    }
087    if (!(obj instanceof org.gbif.api.model.registry.eml.temporal.DateRange)) {
088      return false;
089    }
090
091    org.gbif.api.model.registry.eml.temporal.DateRange that = (org.gbif.api.model.registry.eml.temporal.DateRange) obj;
092    return Objects.equal(this.start, that.start) && Objects.equal(this.end, that.end);
093  }
094
095  @Override
096  public int hashCode() {
097    return Objects.hashCode(super.hashCode(), start, end);
098  }
099
100  @Override
101  public String toString() {
102    return Objects.toStringHelper(this).add("super", super.toString()).add("start", start).add("end", end).toString();
103  }
104
105  @Override
106  public String acceptFormatter(TemporalCoverageFormatterVisitor formatter) {
107    return formatter.format(this);
108  }
109
110}