001/*
002 * Copyright 2013 Global Biodiversity Information Facility (GBIF)
003 * Licensed under the Apache License, Version 2.0 (the "License");
004 * you may not use this file except in compliance with the License.
005 * You may obtain a copy of the License at
006 * http://www.apache.org/licenses/LICENSE-2.0
007 * Unless required by applicable law or agreed to in writing, software
008 * distributed under the License is distributed on an "AS IS" BASIS,
009 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
010 * See the License for the specific language governing permissions and
011 * limitations under the License.
012 */
013package org.gbif.api.model.registry;
014
015import java.util.Date;
016import javax.validation.constraints.Min;
017import javax.validation.constraints.NotNull;
018import javax.validation.constraints.Null;
019import javax.validation.constraints.Size;
020
021import com.google.common.base.Objects;
022
023public class Tag implements LenientEquals<Tag> {
024
025  private Integer key;
026  private String value;
027  private String createdBy;
028  private Date created;
029
030  public Tag() {
031  }
032
033  public Tag(String value) {
034    this.value = value;
035  }
036
037  public Tag(String value, String createdBy) {
038    this.value = value;
039    this.createdBy = createdBy;
040  }
041
042  @Null(groups = {PrePersist.class})
043  @NotNull(groups = {PostPersist.class})
044  @Min(1)
045  public Integer getKey() {
046    return key;
047  }
048
049  public void setKey(Integer key) {
050    this.key = key;
051  }
052
053  @NotNull
054  @Size(min = 1)
055  public String getValue() {
056    return value;
057  }
058
059  public void setValue(String value) {
060    this.value = value;
061  }
062
063  @Size(min = 3)
064  public String getCreatedBy() {
065    return createdBy;
066  }
067
068  public void setCreatedBy(String createdBy) {
069    this.createdBy = createdBy;
070  }
071
072  @Null(groups = {PrePersist.class})
073  @NotNull(groups = {PostPersist.class})
074  public Date getCreated() {
075    return created;
076  }
077
078  public void setCreated(Date created) {
079    this.created = created;
080  }
081
082  @Override
083  public int hashCode() {
084    return Objects.hashCode(key, value, createdBy, created);
085  }
086
087  @Override
088  public boolean equals(Object object) {
089    if (object instanceof Tag) {
090      Tag that = (Tag) object;
091      return Objects.equal(this.key, that.key)
092        && Objects.equal(this.value, that.value)
093        && Objects.equal(this.createdBy, that.createdBy)
094        && Objects.equal(this.created, that.created);
095    }
096    return false;
097  }
098
099  @Override
100  public String toString() {
101    return Objects.toStringHelper(this)
102      .add("key", key)
103      .add("value", value)
104      .add("createdBy", createdBy)
105      .add("created", created)
106      .toString();
107  }
108
109  /**
110   * A lenient test that returns true if they are the same object or have the same value.
111   */
112  @Override
113  public boolean lenientEquals(Tag other) {
114    if (this == other) {
115      return true;
116    }
117    return Objects.equal(this.value, other.value);
118  }
119
120}