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.checklistbank;
017
018import org.gbif.api.vocabulary.Rank;
019
020import javax.annotation.Nullable;
021import javax.validation.constraints.Min;
022import javax.validation.constraints.NotNull;
023
024import com.google.common.base.Objects;
025
026/**
027 * Basic metrics for a single name usage. These metrics have been embedded in the NameUsage class before
028 * but are rarely being used. They are therefore split out into a separate class to be retrieved only when needed.
029 */
030public class NameUsageMetrics {
031
032  private Integer key;
033  private int numPhylum;
034  private int numClass;
035  private int numOrder;
036  private int numFamily;
037  private int numGenus;
038  private int numSubgenus;
039  private int numSpecies;
040  private int numChildren;
041  private int numDescendants;
042  private int numSynonyms;
043
044
045  /**
046   * Return the key that uniquely identifies this name usage.
047   *
048   * @return the key
049   */
050  @NotNull
051  public Integer getKey() {
052    return key;
053  }
054
055  /**
056   * @param key the key to set
057   */
058  public void setKey(Integer key) {
059    this.key = key;
060  }
061
062
063  /**
064   * Returns the number of direct taxonomic elements under this usage. "Direct" means that children should have
065   * this usageKey as their parentKey.
066   *
067   * @return the numChildren
068   */
069  @Min(0)
070  public int getNumChildren() {
071    return numChildren;
072  }
073
074  /**
075   * @param numChildren the numChildren to set
076   */
077  public void setNumChildren(int numChildren) {
078    this.numChildren = numChildren;
079  }
080
081  /**
082   * Returns the number of classes that are below this taxon. Typically this would mean
083   * this usage is of something above the "class" taxonomic level.
084   *
085   * @return the numClass
086   */
087  @Min(0)
088  public int getNumClass() {
089    return numClass;
090  }
091
092  /**
093   * @param numClass the numClass to set
094   */
095  public void setNumClass(int numClass) {
096    this.numClass = numClass;
097  }
098
099  /**
100   * The number of all accepted taxonomic elements under this usage.
101   *
102   * @return the number of descendants
103   */
104  public int getNumDescendants() {
105    return numDescendants;
106  }
107
108  /**
109   * @param numDescendants the n umber of descendants to set
110   */
111  public void setNumDescendants(int numDescendants) {
112    this.numDescendants = numDescendants;
113  }
114
115  /**
116   * Returns the number of families that are below this taxon. Typically this would mean
117   * this usage is of something above the "family" taxonomic level.
118   *
119   * @return the numFamily
120   */
121  @Min(0)
122  public int getNumFamily() {
123    return numFamily;
124  }
125
126  /**
127   * @param numFamily the numFamily to set
128   */
129  public void setNumFamily(int numFamily) {
130    this.numFamily = numFamily;
131  }
132
133  /**
134   * Returns the number of genus that are below this taxon. Typically this would mean
135   * this usage is of something above the "genus" taxonomic level.
136   *
137   * @return the numGenus
138   */
139  @Min(0)
140  public int getNumGenus() {
141    return numGenus;
142  }
143
144  /**
145   * @param numGenus the numGenus to set
146   */
147  public void setNumGenus(int numGenus) {
148    this.numGenus = numGenus;
149  }
150
151  /**
152   * Returns the number of orders that are below this taxon. Typically this would mean
153   * this usage is of something above the "order" taxonomic level.
154   *
155   * @return the numOrder
156   */
157  @Min(0)
158  public int getNumOrder() {
159    return numOrder;
160  }
161
162  /**
163   * @param numOrder the numOrder to set
164   */
165  public void setNumOrder(int numOrder) {
166    this.numOrder = numOrder;
167  }
168
169  /**
170   * Returns the number of phyla that are below this taxon. Typically this would mean
171   * this usage is of something above the "phylum" taxonomic level.
172   *
173   * @return the numPhylum
174   */
175  @Min(0)
176  public int getNumPhylum() {
177    return numPhylum;
178  }
179
180  /**
181   * @param numPhylum the numPhylum to set
182   */
183  public void setNumPhylum(int numPhylum) {
184    this.numPhylum = numPhylum;
185  }
186
187  /**
188   * Returns the number of species that are below this taxon. Typically this would mean
189   * this usage is of something above the "species" taxonomic level.
190   *
191   * @return the numSpecies.
192   */
193  @Min(0)
194  public int getNumSpecies() {
195    return numSpecies;
196  }
197
198  /**
199   * @param numSpecies the numSpecies to set
200   */
201  public void setNumSpecies(int numSpecies) {
202    this.numSpecies = numSpecies;
203  }
204
205  /**
206   * Returns the number of distinct subgenera that are below this taxon. Typically this would mean
207   * this usage is of something above the "subgenus" taxonomic level.
208   *
209   * @return the numSubgenus
210   */
211  @Min(0)
212  public int getNumSubgenus() {
213    return numSubgenus;
214  }
215
216  /**
217   * @param numSubgenus the number of distinct subgenera
218   */
219  public void setNumSubgenus(int numSubgenus) {
220    this.numSubgenus = numSubgenus;
221  }
222
223  /**
224   * Returns the number of related synonyms this usage possses.
225   *
226   * @return the numSynonyms.
227   */
228  @Min(0)
229  public int getNumSynonyms() {
230    return numSynonyms;
231  }
232
233  /**
234   * @param numSynonyms the numSynonyms to set
235   */
236  public void setNumSynonyms(int numSynonyms) {
237    this.numSynonyms = numSynonyms;
238  }
239
240  /**
241   * Gets the count of usages by a rank.
242   * Only Linnean ranks are supported.
243   *
244   * @param rank the linnean rank to retrieve counts for
245   *
246   * @return the count or null if rank doesnt exist
247   */
248  @Nullable
249  public Integer getNumByRank(Rank rank) {
250    if (rank == Rank.PHYLUM) {
251      return numPhylum;
252    }
253    if (rank == Rank.CLASS) {
254      return numClass;
255    }
256    if (rank == Rank.ORDER) {
257      return numOrder;
258    }
259    if (rank == Rank.FAMILY) {
260      return numFamily;
261    }
262    if (rank == Rank.GENUS) {
263      return numGenus;
264    }
265    if (rank == Rank.SUBGENUS) {
266      return numSubgenus;
267    }
268    if (rank == Rank.SPECIES) {
269      return numSpecies;
270    }
271    return null;
272  }
273
274
275  @Override
276  public int hashCode() {
277    return Objects
278      .hashCode(
279        key,
280        numPhylum,
281        numClass,
282        numOrder,
283        numFamily,
284        numGenus,
285        numSubgenus,
286        numSpecies,
287        numChildren,
288        numDescendants,
289        numSynonyms);
290  }
291
292  @Override
293  public boolean equals(Object obj) {
294    if (this == obj) {
295      return true;
296    }
297    if (obj == null) {
298      return false;
299    }
300    if (getClass() != obj.getClass()) {
301      return false;
302    }
303    final NameUsageMetrics other = (NameUsageMetrics) obj;
304    return Objects.equal(this.key, other.key)
305           && Objects.equal(this.numPhylum, other.numPhylum)
306           && Objects.equal(this.numClass, other.numClass)
307           && Objects.equal(this.numOrder, other.numOrder)
308           && Objects.equal(this.numFamily, other.numFamily)
309           && Objects.equal(this.numGenus, other.numGenus)
310           && Objects.equal(this.numSubgenus, other.numSubgenus)
311           && Objects.equal(this.numSpecies, other.numSpecies)
312           && Objects.equal(this.numChildren, other.numChildren)
313           && Objects.equal(this.numDescendants, other.numDescendants)
314           && Objects.equal(this.numSynonyms, other.numSynonyms);
315  }
316
317  @Override
318  public String toString() {
319    return Objects.toStringHelper(this)
320      .add("key", key)
321      .add("numPhylum", numPhylum)
322      .add("numClass", numClass)
323      .add("numOrder", numOrder)
324      .add("numFamily", numFamily)
325      .add("numGenus", numGenus)
326      .add("numSubgenus", numSubgenus)
327      .add("numSpecies", numSpecies)
328      .add("numChildren", numChildren)
329      .add("numDescendants", numDescendants)
330      .add("numSynonyms", numSynonyms)
331      .toString();
332  }
333}