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