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;
017
018import io.swagger.v3.oas.annotations.media.Schema;
019
020import java.io.Serializable;
021import java.util.Date;
022import java.util.Objects;
023import java.util.StringJoiner;
024
025import javax.validation.constraints.Min;
026import javax.validation.constraints.NotNull;
027import javax.validation.constraints.Null;
028import javax.validation.constraints.Size;
029
030public class Tag implements Serializable, LenientEquals<Tag> {
031
032  @Schema(
033    description = "Identifier for the tag",
034    accessMode = Schema.AccessMode.READ_ONLY
035  )
036  private Integer key;
037
038  @Schema(
039    description = "Text value of the tag"
040  )
041  private String value;
042
043  @Schema(
044    description = "The GBIF username of the creator of the tag",
045    accessMode = Schema.AccessMode.READ_ONLY
046  )
047  private String createdBy;
048
049  @Schema(
050    description = "Timestamp of when the tag was created",
051    accessMode = Schema.AccessMode.READ_ONLY
052  )
053  private Date created;
054
055  public Tag() {}
056
057  public Tag(String value) {
058    this.value = value;
059  }
060
061  public Tag(String value, String createdBy) {
062    this.value = value;
063    this.createdBy = createdBy;
064  }
065
066  @Null(groups = {PrePersist.class})
067  @NotNull(groups = {PostPersist.class})
068  @Min(1)
069  public Integer getKey() {
070    return key;
071  }
072
073  public void setKey(Integer key) {
074    this.key = key;
075  }
076
077  @NotNull
078  @Size(min = 1)
079  public String getValue() {
080    return value;
081  }
082
083  public void setValue(String value) {
084    this.value = value;
085  }
086
087  @Size(min = 3)
088  public String getCreatedBy() {
089    return createdBy;
090  }
091
092  public void setCreatedBy(String createdBy) {
093    this.createdBy = createdBy;
094  }
095
096  @Null(groups = {PrePersist.class})
097  @NotNull(groups = {PostPersist.class})
098  public Date getCreated() {
099    return created;
100  }
101
102  public void setCreated(Date created) {
103    this.created = created;
104  }
105
106  @Override
107  public boolean equals(Object o) {
108    if (this == o) {
109      return true;
110    }
111    if (o == null || getClass() != o.getClass()) {
112      return false;
113    }
114    Tag tag = (Tag) o;
115    return Objects.equals(key, tag.key)
116        && Objects.equals(value, tag.value)
117        && Objects.equals(createdBy, tag.createdBy)
118        && Objects.equals(created, tag.created);
119  }
120
121  @Override
122  public int hashCode() {
123    return Objects.hash(key, value, createdBy, created);
124  }
125
126  @Override
127  public String toString() {
128    return new StringJoiner(", ", Tag.class.getSimpleName() + "[", "]")
129        .add("key=" + key)
130        .add("value='" + value + "'")
131        .add("createdBy='" + createdBy + "'")
132        .add("created=" + created)
133        .toString();
134  }
135
136  /**
137   * A lenient test that returns true if they are the same object or have the same value.
138   */
139  @Override
140  public boolean lenientEquals(Tag other) {
141    if (this == other) {
142      return true;
143    }
144    return Objects.equals(this.value, other.value);
145  }
146}