All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.infinispan.commons.dataconversion.OneToManyTranscoder Maven / Gradle / Ivy

There is a newer version: 15.1.0.Dev04
Show newest version
package org.infinispan.commons.dataconversion;

import java.util.Collections;
import java.util.HashSet;
import java.util.Set;

/**
 * Base class for {@link Transcoder} that converts between a single format and multiple other formats and back.
 */
public abstract class OneToManyTranscoder extends AbstractTranscoder {

   protected final MediaType mainType;

   protected final Set supportedTypes = new HashSet<>();

   public OneToManyTranscoder(MediaType mainType, MediaType... supportedConversions) {
      this.mainType = mainType;
      this.supportedTypes.add(mainType);
      Collections.addAll(supportedTypes, supportedConversions);
   }

   private boolean in(MediaType mediaType, Set set) {
      return set.stream().anyMatch(s -> s.match(mediaType));
   }

   @Override
   public Set getSupportedMediaTypes() {
      return supportedTypes;
   }

   @Override
   public boolean supportsConversion(MediaType mediaType, MediaType other) {
      return mediaType.match(mainType) && in(other, supportedTypes) ||
            other.match(mainType) && in(mediaType, supportedTypes);
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy