001/*
002 * Licensed under the Apache License, Version 2.0 (the "License");
003 * you may not use this file except in compliance with the License.
004 * You may obtain a copy of the License at
005 *
006 *     http://www.apache.org/licenses/LICENSE-2.0
007 *
008 * Unless required by applicable law or agreed to in writing, software
009 * distributed under the License is distributed on an "AS IS" BASIS,
010 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011 * See the License for the specific language governing permissions and
012 * limitations under the License.
013 */
014package org.gbif.api.ws.mixin;
015
016import org.gbif.api.model.occurrence.Download;
017import org.gbif.api.model.occurrence.Occurrence;
018import org.gbif.api.model.registry.Dataset;
019import org.gbif.api.model.registry.Network;
020import org.gbif.api.model.registry.Node;
021import org.gbif.api.model.registry.Organization;
022import org.gbif.api.model.registry.search.DatasetSearchResult;
023
024import java.util.HashMap;
025import java.util.Map;
026import java.util.Map.Entry;
027import java.util.function.Predicate;
028import java.util.stream.Collectors;
029
030/**
031 * Mixins are typically used to leave serialization-oriented annotations outside the models to avoid
032 * introducing coupling on a specific SerDe.
033 * <p>
034 * This class provides access to predefined mixins used in the GBIF web service application (client and server).
035 */
036@SuppressWarnings("unused")
037public class Mixins {
038
039  // utility class
040  private Mixins() {
041  }
042
043  private static final Map<Class<?>, Class<?>> PREDEFINED_MIXINS = new HashMap<>();
044
045  static {
046    PREDEFINED_MIXINS.put(Dataset.class, DatasetMixin.class);
047    PREDEFINED_MIXINS.put(DatasetSearchResult.class, DatasetMixin.class);
048    PREDEFINED_MIXINS.put(Download.class, LicenseMixin.class);
049    PREDEFINED_MIXINS.put(Occurrence.class, OccurrenceMixin.class);
050    PREDEFINED_MIXINS.put(Node.class, NodeMixin.class);
051    PREDEFINED_MIXINS.put(Network.class, NetworkMixin.class);
052    PREDEFINED_MIXINS.put(Organization.class, OrganizationMixin.class);
053  }
054
055  /**
056   * Return an immutable map of the predefined Jackson Mixins used by the web services.
057   *
058   * @return immutable map of the predefined Jackson Mixins
059   */
060  public static Map<Class<?>, Class<?>> getPredefinedMixins() {
061    return PREDEFINED_MIXINS;
062  }
063
064  /**
065   * Get an immutable map of the predefined Jackson Mixins after the provided keyFilter is applied.
066   *
067   * @param keyFilter predicate to filter the predefined mixins based on the model class
068   * @return immutable map of the predefined Jackson Mixins after applying the predicate
069   */
070  public static Map<Class<?>, Class<?>> getPredefinedMixins(Predicate<Class<?>> keyFilter) {
071    return PREDEFINED_MIXINS.entrySet()
072      .stream()
073      .filter(entry -> keyFilter.test(entry.getKey()))
074      .collect(Collectors.toMap(Entry::getKey, Entry::getValue));
075  }
076}