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; 017 018import java.io.Serializable; 019import java.util.Collection; 020import java.util.HashSet; 021import java.util.Objects; 022import java.util.Set; 023import java.util.StringJoiner; 024 025/** 026 * Allows a collection of keywords which may declare a thesaurus from which they come from. 027 */ 028public class KeywordCollection implements Serializable, Keywords { 029 030 private static final long serialVersionUID = -4749948152622713533L; 031 032 private String thesaurus; 033 034 private Set<String> keywords = new HashSet<>(); 035 036 public KeywordCollection() { 037 } 038 039 public KeywordCollection(String thesaurus, Set<String> keywords) { 040 this.thesaurus = thesaurus; 041 this.keywords = keywords; 042 } 043 044 public Set<String> getKeywords() { 045 return keywords; 046 } 047 048 public void setKeywords(Set<String> keywords) { 049 this.keywords = keywords; 050 } 051 052 public String getThesaurus() { 053 return thesaurus; 054 } 055 056 public void setThesaurus(String thesaurus) { 057 this.thesaurus = thesaurus; 058 } 059 060 @Override 061 public Collection<String> toKeywords() { 062 return keywords; 063 } 064 065 /** 066 * Add keyword to keywords Set. 067 */ 068 public void addKeyword(String keyword) { 069 this.keywords.add(keyword); 070 } 071 072 @Override 073 public boolean equals(Object o) { 074 if (this == o) { 075 return true; 076 } 077 if (o == null || getClass() != o.getClass()) { 078 return false; 079 } 080 KeywordCollection that = (KeywordCollection) o; 081 return Objects.equals(thesaurus, that.thesaurus) && 082 Objects.equals(keywords, that.keywords); 083 } 084 085 @Override 086 public int hashCode() { 087 return Objects.hash(thesaurus, keywords); 088 } 089 090 @Override 091 public String toString() { 092 return new StringJoiner(", ", KeywordCollection.class.getSimpleName() + "[", "]") 093 .add("thesaurus='" + thesaurus + "'") 094 .add("keywords=" + keywords) 095 .toString(); 096 } 097}