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.util;
017
018import org.gbif.api.model.registry.MachineTag;
019import org.gbif.api.model.registry.MachineTaggable;
020import org.gbif.api.vocabulary.TagName;
021import org.gbif.api.vocabulary.TagNamespace;
022
023import java.util.ArrayList;
024import java.util.List;
025import java.util.function.Function;
026import java.util.stream.Collectors;
027
028import javax.annotation.Nullable;
029
030/**
031 * Utility class to simplify your life when dealing with machine tags.
032 */
033public class MachineTagUtils {
034
035  private MachineTagUtils() {
036  }
037
038  /**
039   * Returns a copy of the original list of machine taggable entities, that have the machine tag.
040   */
041  public static <T extends MachineTaggable> List<T> filter(List<T> source,
042    @Nullable final String namespace, @Nullable final String name, @Nullable final String value) {
043    return source.stream()
044      .filter(element -> predicate(namespace, name, value, element))
045      .collect(Collectors.toList());
046  }
047
048  private static <T extends MachineTaggable> boolean predicate(
049    @Nullable String namespace, @Nullable String name, @Nullable String value, T element) {
050    for (MachineTag machineTag : element.getMachineTags()) {
051      if ((namespace == null || machineTag.getNamespace().equals(namespace)) &&
052        (name == null || machineTag.getName().equals(name)) &&
053        (value == null || machineTag.getValue().equals(value))) {
054        return true;
055      }
056    }
057    return false;
058  }
059
060  /**
061   * @return the int value for the given machine tag or zero if its no valid integer or null
062   */
063  public static Integer tagValueAsInteger(MachineTag tag) {
064    if (tag != null) {
065      try {
066        return Integer.parseInt(tag.getValue());
067      } catch (NumberFormatException e) {
068        // quietly swallow
069      }
070    }
071    return null;
072  }
073
074  /**
075   * @return the double value for the given machine tag or zero if its no valid double or null
076   */
077  public static Double tagValueAsDouble(MachineTag tag) {
078    if (tag != null) {
079      try {
080        return Double.parseDouble(tag.getValue());
081      } catch (NumberFormatException e) {
082        // quietly swallow
083      }
084    }
085    return null;
086  }
087
088  /**
089   * @return the first machine tag that with the given TagName.
090   */
091  public static MachineTag firstTag(MachineTaggable taggable, TagName tagName) {
092    return firstTag(taggable, tagName.getNamespace().getNamespace(), tagName.getName());
093  }
094
095  /**
096   * @return the first machine tag that with the given namespace and name.
097   */
098  public static MachineTag firstTag(MachineTaggable taggable, String namespace, String tagName) {
099    for (MachineTag mt : taggable.getMachineTags()) {
100      if (mt.getNamespace().equals(namespace) && mt.getName().equals(tagName)) {
101        return mt;
102      }
103    }
104    return null;
105  }
106
107  /**
108   * @return the result of applying the supplied function to the first machine tag with the given TagName.
109   */
110  public static <T> T firstTag(MachineTaggable taggable, TagName tagName, Function<MachineTag, T> function) {
111    return function.apply(firstTag(taggable, tagName));
112  }
113
114  /**
115   * @return the result of applying the supplied function to the first machine tag with the given namespace and name.
116   */
117  public static <T> T firstTag(MachineTaggable taggable, String namespace, String tagName, Function<MachineTag, T> function) {
118    return function.apply(firstTag(taggable, namespace, tagName));
119  }
120
121  /**
122   * @return a new list of machine tags which have the given tagNamespace.
123   */
124  public static List<MachineTag> list(MachineTaggable taggable, TagNamespace tagNamespace) {
125    List<MachineTag> tags = new ArrayList<>();
126    for (MachineTag mt : taggable.getMachineTags()) {
127      if (mt.getNamespace().equals(tagNamespace.getNamespace())) {
128        tags.add(mt);
129      }
130    }
131    return tags;
132  }
133
134  /**
135   * @return a new list of machine tags which have the given tagName.
136   */
137  public static List<MachineTag> list(MachineTaggable taggable, TagName tagName) {
138    return list(taggable, tagName.getNamespace().getNamespace(), tagName.getName());
139  }
140
141  /**
142   * @return a new list of machine tags which have the given namespace and name.
143   */
144  public static List<MachineTag> list(MachineTaggable taggable, String namespace, String tagName) {
145    List<MachineTag> tags = new ArrayList<>();
146    for (MachineTag mt : taggable.getMachineTags()) {
147      if (mt.getNamespace().equals(namespace) && mt.getName().equals(tagName)) {
148        tags.add(mt);
149      }
150    }
151    return tags;
152  }
153
154  /**
155   * @return a new list of machine tags which have the given namespace and a name starting with a common prefix.
156   */
157  public static List<MachineTag> listByPrefix(MachineTaggable taggable, String namespace, String prefix) {
158    List<MachineTag> tags = new ArrayList<>();
159    for (MachineTag mt : taggable.getMachineTags()) {
160      if (mt.getNamespace().equals(namespace) && mt.getName().startsWith(prefix)) {
161        tags.add(mt);
162      }
163    }
164    return tags;
165  }
166}