List of usage examples for java.util Map isEmpty
boolean isEmpty();
From source file:org.kmnet.com.fw.web.el.Functions.java
/** * build query string from map with the specified {@link BeanWrapper}. * <p>//from ww w . ja v a 2 s .com * query string is encoded with "UTF-8". * </p> * @param map map * @param beanWrapper beanWrapper which has the definition of each field. * @return query string. if map is not empty, return query string. ex) name1=value&name2=value&... */ public static String mapToQuery(Map<String, Object> map, BeanWrapper beanWrapper) { if (map == null || map.isEmpty()) { return ""; } UriComponentsBuilder builder = UriComponentsBuilder.fromPath(""); Map<String, Object> uriVariables = new HashMap<String, Object>(); for (Map.Entry<String, Object> e : map.entrySet()) { String name = e.getKey(); Object value = e.getValue(); builder.queryParam(name, "{" + name + "}"); TypeDescriptor sourceType; if (beanWrapper != null) { sourceType = beanWrapper.getPropertyTypeDescriptor(name); } else { sourceType = TypeDescriptor.forObject(value); } uriVariables.put(name, CONVERSION_SERVICE.convert(value, sourceType, STRING_DESC)); } String query = builder.buildAndExpand(uriVariables).encode().toString(); // remove the beginning symbol character('?') of the query string. return query.substring(1); }
From source file:cz.cas.lib.proarc.common.process.GenericExternalProcess.java
/** * Interpolate parameter values. The replace pattern is {@code ${name}}. * @param s a string to search for placeholders * @return the resolved string//from www . j a v a2s . c om * @see #addParameter(java.lang.String, java.lang.String) */ static String interpolateParameters(String s, Map<String, String> parameters) { if (s == null || s.length() < 4 || parameters.isEmpty()) { // minimal replaceable value ${x} return s; } // finds ${name} patterns Matcher m = REPLACE_PARAM_PATTERN.matcher(s); StringBuffer sb = null; while (m.find()) { if (m.groupCount() == 1) { String param = m.group(1); String replacement = parameters.get(param); if (replacement != null) { sb = sb != null ? sb : new StringBuffer(); m.appendReplacement(sb, Matcher.quoteReplacement(replacement)); } } } if (sb == null) { return s; } m.appendTail(sb); return sb.toString(); }
From source file:fr.zcraft.zlib.tools.mojang.UUIDFetcher.java
/** * Fetches the UUIDs of the given player name from the Mojang API. * * <p><b>WARNING: this method needs to be called from a dedicated thread, as the requests to * Mojang are executed directly in the current thread and, due to the Mojang API rate limit, the * thread may be frozen to wait a bit between requests if a lot of UUID are requested.</b></p> * * <p>You can use a {@link fr.zcraft.zlib.components.worker.Worker} to retrieve UUIDs.</p> * * @param name A player name.// w ww. j a v a2s .co m * * @return The player's {@link UUID}, or {@code null} if it was not found. * @throws IOException If an exception occurs while contacting the Mojang API. * @throws InterruptedException If the thread is interrupted while sleeping when the system wait * because of the Mojang API rate limit. */ static public UUID fetch(String name) throws IOException, InterruptedException { final List<String> nameCollection = Collections.singletonList(name); final Map<String, UUID> uuid = fetch(nameCollection); if (uuid.isEmpty()) fetchRemaining(nameCollection, uuid); return uuid.get(name); }
From source file:org.bigtester.ate.GlobalUtils.java
/** * Find db initializer.//ww w . j a va 2 s .com * * @param appCtx * the app ctx * @return the test database initializer */ public static TestDatabaseInitializer findDBInitializer(ApplicationContext appCtx) throws NoSuchBeanDefinitionException { Map<String, TestDatabaseInitializer> dbInit = appCtx.getBeansOfType(TestDatabaseInitializer.class); if (dbInit.isEmpty()) { throw new NoSuchBeanDefinitionException(TestDatabaseInitializer.class); } else { TestDatabaseInitializer retVal = dbInit.values().iterator().next(); if (null == retVal) { throw new NoSuchBeanDefinitionException(TestDatabaseInitializer.class); } else { return retVal; } } }
From source file:com.ikon.util.DatabaseMetadataUtils.java
/** * Obtain a DatabaseMetadataValue from a Map *//*ww w . j a va 2 s .c om*/ public static DatabaseMetadataValue getDatabaseMetadataValueByMap(Map<String, String> map) throws DatabaseException, IllegalAccessException, InvocationTargetException { DatabaseMetadataValue dmv = new DatabaseMetadataValue(); if (!map.isEmpty() && map.containsKey(DatabaseMetadataMap.MV_NAME_TABLE)) { dmv.setTable(map.get(DatabaseMetadataMap.MV_NAME_TABLE)); if (map.containsKey(DatabaseMetadataMap.MV_NAME_ID)) { dmv.setId(new Double(map.get(DatabaseMetadataMap.MV_NAME_ID)).longValue()); } List<DatabaseMetadataType> types = DatabaseMetadataDAO.findAllTypes(dmv.getTable()); for (DatabaseMetadataType emt : types) { if (!emt.getVirtualColumn().equals(DatabaseMetadataMap.MV_NAME_ID) && !emt.getVirtualColumn().equals(DatabaseMetadataMap.MV_NAME_TABLE)) { if (map.keySet().contains(emt.getVirtualColumn())) { BeanUtils.setProperty(dmv, emt.getRealColumn(), map.get(emt.getVirtualColumn())); } } } } return dmv; }
From source file:org.terasoluna.gfw.web.el.Functions.java
/** * build query string from map with the specified {@link BeanWrapper}. * <p>// ww w . jav a 2s . co m * query string is encoded with "UTF-8".<br> * <strong>Use {@link #mapToQuery(Map)} instead of this method.</strong> * </p> * @see ObjectToMapConverter * @param map map * @param beanWrapper beanWrapper which has the definition of each field. * @return query string. if map is not empty, return query string. ex) name1=value&name2=value&... * @deprecated (since 5.0.1, to support nested fields in f:query, Use {@link #mapToQuery(Map)} instead of this method.) */ @Deprecated public static String mapToQuery(Map<String, Object> map, BeanWrapper beanWrapper) { if (map == null || map.isEmpty()) { return ""; } UriComponentsBuilder builder = UriComponentsBuilder.fromPath(""); for (Map.Entry<String, Object> e : map.entrySet()) { String name = e.getKey(); Object value = e.getValue(); TypeDescriptor sourceType; if (beanWrapper != null) { sourceType = beanWrapper.getPropertyTypeDescriptor(name); } else { sourceType = TypeDescriptor.forObject(value); } builder.queryParam(name, CONVERSION_SERVICE.convert(value, sourceType, STRING_DESC)); } String query = builder.build().encode().toString(); // remove the beginning symbol character('?') of the query string. return query.substring(1); }
From source file:com.gson.util.HttpKit.java
/** * mapurl/*from www .j a va2s . co m*/ * @return : * @throws UnsupportedEncodingException */ public static String map2Url(Map<String, String> paramToMap) throws UnsupportedEncodingException { if (null == paramToMap || paramToMap.isEmpty()) { return null; } StringBuffer url = new StringBuffer(); boolean isfist = true; for (Entry<String, String> entry : paramToMap.entrySet()) { if (isfist) { isfist = false; } else { url.append("&"); } url.append(entry.getKey()).append("="); String value = entry.getValue(); if (StringUtils.isNotEmpty(value)) { url.append(URLEncoder.encode(value, DEFAULT_CHARSET)); } } return url.toString(); }
From source file:Main.java
/** * Checks if the given map is not empty. * * @param map//w w w . j ava 2 s. c o m * map to check * @return <code>true</code> if the given reference is not <code>null</code> and map is not empty */ public static boolean isNotEmpty(final Map<?, ?> map) { return map != null && !map.isEmpty(); }
From source file:com.sangupta.snowpack.SnowpackRecover.java
/** * Start the recovery process.//from ww w .ja v a2 s. c om * * @param baseDirectory */ public static void recover(final File baseDirectory) { // basic checks if (baseDirectory == null) { throw new IllegalArgumentException("Base directory cannot be empty"); } if (!baseDirectory.isDirectory()) { throw new IllegalArgumentException("Base directory path does not represent a valid directory"); } // now read all files from disk File[] files = baseDirectory.listFiles(); if (files == null || files.length == 0) { System.out.println("No file found in the base directory... nothing to recover."); return; } // get all valid chunks from disk Map<Integer, File> validChunks = getValidChunks(files); // now check each chunk to find out the number of files in it if (validChunks.isEmpty()) { System.out.println("No valid snowpack chunks were found... nothing to recover."); return; } // rename the current metadata directory if any try { renameOldMetadataDirectory(baseDirectory); } catch (IOException e) { System.out.println("Unable to rename existing metadata directory... rename and run recovery again!"); return; } // create a new metadata directory System.out.println("Creating new METADATA database for recovery..."); final SnowpackMetadataDB metadataDB = new SnowpackMetadataDB(baseDirectory, false, 1000); // do not cache metadata // iterate over all chunks List<ChunkInfo> chunkInfos = new ArrayList<ChunkInfo>(); int totalFiles = 0; for (Entry<Integer, File> entry : validChunks.entrySet()) { File chunkFile = entry.getValue(); int chunkID = entry.getKey(); // now recover the file System.out.print("Recovering from chunk file: " + chunkFile.getAbsolutePath() + "..."); ChunkInfo chunkInfo = null; try { chunkInfo = recoverChunkInfo(chunkID, chunkFile, metadataDB); } catch (FileNotFoundException e) { // this shall never happen as we just read the file // eat up } catch (IOException e) { // this happens when we are unable to read through file // or descriptors are not correct // eat up } if (chunkInfo == null) { System.out.println("failed."); continue; } // add to list of chunk infos chunkInfos.add(chunkInfo); totalFiles += chunkInfo.numFiles; System.out.println("recovered!"); System.out.println("Recovered chunk-info: " + chunkInfo); } // close the metadata DB System.out.println("Closing recovered METADATA database..."); metadataDB.close(); // now check if something was recovered if (chunkInfos.isEmpty()) { System.out.println("Unable to recover anything from the chunk files... Sorry!"); return; } // save this chunk info file Collections.sort(chunkInfos); writeSnowpackMeta(baseDirectory, chunkInfos); // output total files System.out.println("Total number of files in pack: " + totalFiles); }
From source file:com.amazonaws.services.sqs.MessageMD5ChecksumHandler.java
/** * Throw an exception if the MD5 checksums returned in the * SendMessageBatchResult do not match the client-side calculation based on * the original messages in the SendMessageBatchRequest. *//*ww w.ja va 2 s . c om*/ private static void sendMessageBatchOperationMd5Check(SendMessageBatchRequest sendMessageBatchRequest, SendMessageBatchResult sendMessageBatchResult) { Map<String, SendMessageBatchRequestEntry> idToRequestEntryMap = new HashMap<String, SendMessageBatchRequestEntry>(); if (sendMessageBatchRequest.getEntries() != null) { for (SendMessageBatchRequestEntry entry : sendMessageBatchRequest.getEntries()) { idToRequestEntryMap.put(entry.getId(), entry); } } if (sendMessageBatchResult.getSuccessful() != null) { for (SendMessageBatchResultEntry entry : sendMessageBatchResult.getSuccessful()) { String messageBody = idToRequestEntryMap.get(entry.getId()).getMessageBody(); String bodyMd5Returned = entry.getMD5OfMessageBody(); String clientSideBodyMd5 = calculateMessageBodyMd5(messageBody); if (!clientSideBodyMd5.equals(bodyMd5Returned)) { throw new AmazonClientException(String.format(MD5_MISMATCH_ERROR_MESSAGE_WITH_ID, MESSAGE_BODY, entry.getId(), clientSideBodyMd5, bodyMd5Returned)); } Map<String, MessageAttributeValue> messageAttr = idToRequestEntryMap.get(entry.getId()) .getMessageAttributes(); if (messageAttr != null && !messageAttr.isEmpty()) { String attrMd5Returned = entry.getMD5OfMessageAttributes(); String clientSideAttrMd5 = calculateMessageAttributesMd5(messageAttr); if (!clientSideAttrMd5.equals(attrMd5Returned)) { throw new AmazonClientException(String.format(MD5_MISMATCH_ERROR_MESSAGE_WITH_ID, MESSAGE_ATTRIBUTES, entry.getId(), clientSideAttrMd5, attrMd5Returned)); } } } } }