Example usage for java.util Map containsKey

List of usage examples for java.util Map containsKey

Introduction

In this page you can find the example usage for java.util Map containsKey.

Prototype

boolean containsKey(Object key);

Source Link

Document

Returns true if this map contains a mapping for the specified key.

Usage

From source file:com.joliciel.talismane.utils.StringUtils.java

public static Map<String, String> convertArgs(String[] args) {
    Map<String, String> argMap = new HashMap<String, String>();
    for (String arg : args) {
        int equalsPos = arg.indexOf('=');
        if (equalsPos < 0) {
            throw new RuntimeException("Argument " + arg + " has no value");
        }// w  w w .  j  ava  2s  . co m

        String argName = arg.substring(0, equalsPos);
        String argValue = arg.substring(equalsPos + 1);
        if (argMap.containsKey(argName))
            throw new RuntimeException("Duplicate command-line argument: " + argName);
        argMap.put(argName, argValue);
    }
    return argMap;
}

From source file:com.mmj.app.common.util.URLUtils.java

@SuppressWarnings("unchecked")
public static Map<String, Object> getParams(URL url) {
    Map<String, Object> paramMap = new HashMap<String, Object>();
    String query = url.getQuery();
    if (query == null) {
        return paramMap;
    }//  w ww  . java2 s  .  c  om
    String[] params = query.split(split);
    for (String param : params) {
        if (StringUtils.isEmpty(param)) {
            continue;
        }
        String name = param.substring(0, param.indexOf("="));
        String value = param.substring(param.indexOf("=") + 1, param.length());
        if (paramMap.containsKey(name)) {
            Object object = paramMap.get(name);
            List<String> values = null;
            if (object instanceof List) {
                values = (List<String>) object;
            } else {
                values = new ArrayList<String>();
                values.add(object.toString());
            }
            values.add(value);
            paramMap.put(name, values);
        } else {
            paramMap.put(name, value);
        }
    }
    return paramMap;
}

From source file:com.google.mr4c.util.CustomFormat.java

/**
  * Create a CustomFormat with custom regular expressions for some variables
*//*from   www . ja v a2  s  .com*/
public static CustomFormat createInstance(String pattern, Map<String, String> regexMap) {
    CustomFormat format = new CustomFormat();
    format.m_nameList = extractNames(pattern);
    format.m_nameSet = new HashSet<String>(format.m_nameList);
    format.m_pattern = pattern;
    Map<String, String> varMap = new HashMap<String, String>();
    for (String name : format.m_nameSet) {
        String regex = String.format("(%s)", regexMap.containsKey(name) ? regexMap.get(name) : VALUE_REGEX);
        varMap.put(name, regex);
    }
    format.m_regex = StrSubstitutor.replace(pattern, varMap);
    return format;
}

From source file:com.datumbox.common.dataobjects.MatrixDataset.java

public static RealVector parseRecord(Record r, Map<Object, Integer> featureIdsReference) {
    if (featureIdsReference.isEmpty()) {
        throw new RuntimeException("The featureIdsReference map should not be empty.");
    }//from  w  w w  . java  2  s.  c o m

    int d = featureIdsReference.size();

    RealVector v = new ArrayRealVector(d);

    boolean addConstantColumn = featureIdsReference.containsKey(Dataset.constantColumnName);

    if (addConstantColumn) {
        v.setEntry(0, 1.0); //add the constant column
    }
    for (Map.Entry<Object, Object> entry : r.getX().entrySet()) {
        Object feature = entry.getKey();
        Double value = Dataset.toDouble(entry.getValue());
        if (value != null) {
            Integer featureId = featureIdsReference.get(feature);
            if (featureId != null) {//if the feature exists in our database
                v.setEntry(featureId, value);
            }
        } else {
            //else the X matrix maintains the 0.0 default value
        }
    }

    return v;
}

From source file:com.glaf.core.util.Tools.java

@SuppressWarnings("unchecked")
public static void populate(Object model, Map<String, Object> dataMap) {
    if (model instanceof Map) {
        Map<String, Object> map = (Map<String, Object>) model;
        Set<Entry<String, Object>> entrySet = map.entrySet();
        for (Entry<String, Object> entry : entrySet) {
            String key = entry.getKey();
            if (dataMap.containsKey(key)) {
                map.put(key, dataMap.get(key));
            }/*  w ww .j a v  a 2 s  .  com*/
        }
    } else {
        PropertyDescriptor[] propertyDescriptor = BeanUtils.getPropertyDescriptors(model.getClass());
        for (int i = 0; i < propertyDescriptor.length; i++) {
            PropertyDescriptor descriptor = propertyDescriptor[i];
            String propertyName = descriptor.getName();
            if (propertyName.equalsIgnoreCase("class")) {
                continue;
            }
            String value = null;
            Object x = null;
            Object object = dataMap.get(propertyName);
            if (object != null && object instanceof String) {
                value = (String) object;
            }
            try {
                Class<?> clazz = descriptor.getPropertyType();
                if (value != null) {
                    x = getValue(clazz, value);
                } else {
                    x = object;
                }
                if (x != null) {
                    // PropertyUtils.setProperty(model, propertyName, x);
                    ReflectUtils.setFieldValue(model, propertyName, x);
                }
            } catch (Exception ex) {
                logger.debug(ex);
            }
        }
    }
}

From source file:eu.scape_project.pt.mapred.input.ControlFileInputFormat.java

/**
 * Adds control line to the locationmap and keeps locations balanced.
 *
 * @param locationMap mapping of locations to arraylists of control lines
 * @param hosts locations of the control line
 * @param line control line//from  w w  w  . j  av a 2s .  c om
 * @param lineNum current line number of original control file
 */
public static void addToLocationMap(Map<String, ArrayList<String>> locationMap, String[] hosts, String line,
        int lineNum) {
    for (String host : hosts) {
        ArrayList<String> lines = locationMap.containsKey(host) ? locationMap.get(host)
                : new ArrayList<String>();
        // if the location is unbalanced (got too few lines) add line for that location
        if (lines.size() < (float) lineNum / hosts.length) {
            lines.add(line);
            locationMap.put(host, lines);
            break;
        }
    }
}

From source file:com.oneis.appserver.FileUploads.java

/**
 * Determine whether an HTTP request contains file uploads. If so, returns a
 * FileUploads object initialised for decoding the stream.
 *
 * @return FileUploads object to use to decode the streamed data.
 */// w w  w.  ja  v  a 2  s.  c  o  m
static public FileUploads createIfUploads(HttpServletRequest request) {
    String contentType = request.getHeader("Content-Type");
    if (contentType == null) {
        return null;
    }

    // Set up a parser for the various values
    ParameterParser paramParser = new ParameterParser();
    paramParser.setLowerCaseNames(true);

    // Decode content type
    Map contentTypeDecoded = paramParser.parse(contentType, PARAMPARSER_SEPERATORS);

    String boundaryStr = null;
    if (!contentTypeDecoded.containsKey("multipart/form-data")
            || (boundaryStr = (String) contentTypeDecoded.get("boundary")) == null) {
        // Wasn't a file upload
        return null;
    }

    byte[] boundary = null;
    try {
        boundary = boundaryStr.getBytes("ISO-8859-1");
    } catch (java.io.UnsupportedEncodingException e) {
        throw new RuntimeException("Expected charset ISO-8859-1 not installed");
    }
    if (boundaryStr == null || boundary.length < 2) {
        return null;
    }

    // Looks good... create an object to signal success
    return new FileUploads(boundary);
}

From source file:io.lavagna.service.PermissionService.java

private static Map<String, RoleAndPermissions> toMap(List<RoleAndPermission> l) {
    Map<String, RoleAndPermissions> res = new TreeMap<>();
    for (RoleAndPermission rap : l) {
        if (!res.containsKey(rap.getRoleName())) {
            res.put(rap.getRoleName(), new RoleAndPermissions(rap));
        }/*from  w  ww .  j a v a  2s.com*/
        if (rap.getPermission() != null) {
            res.get(rap.getRoleName()).roleAndPermissions.add(rap);
        }
    }
    return res;
}

From source file:org.jboss.tools.livereload.core.internal.util.ReloadCommandGenerator.java

/**
 * Dispatch the files by their extension
 * /*from ww  w . j av a2s . com*/
 * @param files
 * @return a map where files are indexed by their extension (lowercased)
 */
private static Map<String, List<IResource>> dispatch(List<IResource> files) {
    final Map<String, List<IResource>> dispatchedFiles = new LinkedHashMap<String, List<IResource>>();
    for (IResource file : files) {
        final String fileExtension = file.getFileExtension() != null ? file.getFileExtension().toLowerCase()
                : null;
        // skip files without extension
        if (fileExtension == null) {
            continue;
        }
        if (dispatchedFiles.containsKey(fileExtension)) {
            dispatchedFiles.get(fileExtension).add(file);
        } else {
            dispatchedFiles.put(fileExtension, new ArrayList<IResource>(Arrays.asList(file)));
        }
    }
    return dispatchedFiles;
}