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

com.beust.jcommander.FuzzyMap Maven / Gradle / Ivy

There is a newer version: 1.0.366
Show newest version
package com.beust.jcommander;

import com.beust.jcommander.internal.Maps;

import java.util.Map;

/**
 * Helper class to perform fuzzy key look ups: looking up case insensitive or
 * abbreviated keys.
 */
public class FuzzyMap {
  interface IKey {
    String getName();
  }

  public static  V findInMap(Map map, IKey name,
      boolean caseSensitive, boolean allowAbbreviations) {
    if (allowAbbreviations) {
      return findAbbreviatedValue(map, name, caseSensitive);
    } else {
      if (caseSensitive) {
        return map.get(name);
      } else {
        for (IKey c : map.keySet()) {
          if (c.getName().equalsIgnoreCase(name.getName())) {
            return map.get(c);
          }
        }
      }
    }
    return null;
  }

  private static  V findAbbreviatedValue(Map map, IKey name,
      boolean caseSensitive) {
    String string = name.getName();
    Map results = Maps.newHashMap();
    for (IKey c : map.keySet()) {
      String n = c.getName();
      boolean match = (caseSensitive && n.startsWith(string))
          || ((! caseSensitive) && n.toLowerCase().startsWith(string.toLowerCase()));
      if (match) {
        results.put(n, map.get(c));
      }
    }

    V result;
    if (results.size() > 1) {
      throw new ParameterException("Ambiguous option: " + name
          + " matches " + results.keySet());
    } else if (results.size() == 1) {
      result = results.values().iterator().next();
    } else {
      result = null;
    }

    return result;
  }


}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy