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