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

com.emv.qrcode.decoder.mpm.DecoderMpm Maven / Gradle / Ivy

There is a newer version: 0.1.2
Show newest version
package com.emv.qrcode.decoder.mpm;

import java.lang.reflect.Constructor;
import java.util.AbstractMap.SimpleEntry;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;

import com.emv.qrcode.core.configuration.DecodersMpmMap;
import com.emv.qrcode.core.exception.PresentedModeException;

// @formatter:off
public abstract class DecoderMpm {

  private static final Map, Constructor>> ctorMap = new ConcurrentHashMap<>();

  protected final Iterator iterator;

  protected DecoderMpm(final String source) {
    this.iterator = new DecodeMpmIterator(source);
  }

  protected abstract T decode() throws PresentedModeException;

  protected static  Entry, BiConsumer> consumerTagLengthValue(final Class clazz, final BiConsumer consumer) {
    return new SimpleEntry<>(clazz, consumer);
  }

  /**
   * Decode MPM using string
   *
   * @param  target class
   * @param source string MPM
   * @param clazz target class
   * @return target class result
   * @throws PresentedModeException
   */
  public static  T decode(final String source, final Class clazz) throws PresentedModeException {
    try {
      final Class> parserClass = DecodersMpmMap.getDecoder(clazz);

      if (!ctorMap.containsKey(clazz)) {
        ctorMap.put(clazz, parserClass.getConstructor(String.class));
      }

      final Constructor> ctor = ctorMap.get(clazz);
      final DecoderMpm parser = ctor.newInstance(source);
      return clazz.cast(parser.decode());
    } catch (final PresentedModeException ex) {
      throw ex;
    } catch (final Exception ex) {
      throw new RuntimeException(ex);
    }
  }

}
// @formatter:on




© 2015 - 2025 Weber Informatics LLC | Privacy Policy