001package org.gbif.api.util;
002
003import org.gbif.api.model.registry.MachineTag;
004import org.gbif.api.model.registry.MachineTaggable;
005import org.gbif.api.vocabulary.TagName;
006
007import java.util.List;
008import javax.annotation.Nullable;
009
010import com.google.common.base.Predicate;
011import com.google.common.collect.Iterables;
012import com.google.common.collect.Lists;
013
014/**
015 * Utility class to simplify your life when dealing with machine tags.
016 */
017public class MachineTagUtils {
018
019  private MachineTagUtils() {
020  }
021
022  /**
023   * Returns a copy of the original list of machine taggable entities, that have the machine tag.
024   */
025  public static <T extends MachineTaggable> List<T> filter(List<T> source, @Nullable final String namespace, @Nullable final String name, @Nullable final String value) {
026    return Lists.newArrayList(Iterables.filter(source, new Predicate<T>() {
027
028      @Override
029      public boolean apply(@Nullable T e) {
030        for (MachineTag t : e.getMachineTags()) {
031          if ((namespace == null || t.getNamespace().equals(namespace)) &&
032              (name == null || t.getName().equals(name)) &&
033              (value == null || t.getValue().equals(value)))
034          {
035            return true;
036          }
037        }
038        return false;
039      }
040    }));
041  }
042
043
044  /**
045   * @return the int value for the given machine tag or zero if its no valid integer or null
046   */
047  public static Integer tagValueAsInteger(MachineTag tag) {
048    if (tag != null) {
049      try {
050        return Integer.parseInt(tag.getValue());
051      } catch (NumberFormatException e) {
052        // quietly swallow
053      }
054    }
055    return null;
056  }
057
058  /**
059   * @return the double value for the given machine tag or zero if its no valid double or null
060   */
061  public static Double tagValueAsDouble(MachineTag tag) {
062    if (tag != null) {
063      try {
064        return Double.parseDouble(tag.getValue());
065      } catch (NumberFormatException e) {
066        // quietly swallow
067      }
068    }
069    return null;
070  }
071
072  /**
073   * @return the first machine tag that with the given namespace and name.
074   */
075  public static MachineTag firstTag(MachineTaggable taggable, TagName tagName) {
076    return firstTag(taggable, tagName.getNamespace().getNamespace(), tagName.getName());
077  }
078
079  /**
080   * @return the first machine tag that with the given namespace and name.
081   */
082  public static MachineTag firstTag(MachineTaggable taggable, String namespace, String tagName) {
083    for (MachineTag mt : taggable.getMachineTags()) {
084      if (mt.getNamespace().equals(namespace) && mt.getName().equals(tagName)) {
085        return mt;
086      }
087    }
088    return null;
089  }
090
091  /**
092   * @return a new list of machine tags which have the given tagName.
093   */
094  public static List<MachineTag> list(MachineTaggable taggable, TagName tagName) {
095    return list(taggable, tagName.getNamespace().getNamespace(), tagName.getName());
096  }
097
098  /**
099   * @return a new list of machine tags which have the given namespace and name.
100   */
101  public static List<MachineTag> list(MachineTaggable taggable, String namespace, String tagName) {
102    List<MachineTag> tags = Lists.newArrayList();
103    for (MachineTag mt : taggable.getMachineTags()) {
104      if (mt.getNamespace().equals(namespace) && mt.getName().equals(tagName)) {
105        tags.add(mt);
106      }
107    }
108    return tags;
109  }
110
111  /**
112   * @return a new list of machine tags which have the given namespace and a name starting with a common prefix.
113   */
114  public static List<MachineTag> listByPrefix(MachineTaggable taggable, String namespace, String prefix) {
115    List<MachineTag> tags = Lists.newArrayList();
116    for (MachineTag mt : taggable.getMachineTags()) {
117      if (mt.getNamespace().equals(namespace) && mt.getName().startsWith(prefix)) {
118        tags.add(mt);
119      }
120    }
121    return tags;
122  }
123}