List of usage examples for java.util Map isEmpty
boolean isEmpty();
From source file:Main.java
/** * Checks if the given map is empty.//from w w w .j ava2 s.com * * @param map * map to check * @return <code>true</code> if the given reference is <code>null</code> or map is empty */ public static boolean isEmpty(final Map<?, ?> map) { return map == null || map.isEmpty(); }
From source file:com.datumbox.framework.common.dataobjects.MatrixDataframe.java
/** * Parses a single Record and converts it to RealVector by using an already * existing mapping between feature names and column ids. * /*from w w w .j a v a2s .com*/ * @param r * @param featureIdsReference * @return */ public static RealVector parseRecord(Record r, Map<Object, Integer> featureIdsReference) { if (featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should not be empty."); } int d = featureIdsReference.size(); RealVector v = new ArrayRealVector(d); boolean addConstantColumn = featureIdsReference.containsKey(Dataframe.COLUMN_NAME_CONSTANT); 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 = TypeInference.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:edu.isi.misd.scanner.network.registry.web.controller.BaseController.java
public static Map<String, String> validateParameterMap(Map<String, String> paramMap, String... paramNames) throws BadRequestException { HashMap<String, String> validParams = new HashMap<String, String>(); if (!paramMap.isEmpty()) { String paramValue;/*from w w w .j a v a 2s . c om*/ for (String paramName : paramNames) { paramValue = paramMap.remove(paramName); if (paramValue != null) { validParams.put(paramName, paramValue); } } if (!paramMap.isEmpty()) { throw new BadRequestException(paramMap.keySet()); } } return validParams; }
From source file:com.datumbox.framework.common.dataobjects.MatrixDataframe.java
/** * Parses a testing dataset and converts it to MatrixDataframe by using an already existing mapping between feature names and column ids. Typically used * to parse the testing or validation dataset. * /*from w ww . j a va2 s.com*/ * @param newData * @param recordIdsReference * @param featureIdsReference * @return */ public static MatrixDataframe parseDataset(Dataframe newData, Map<Integer, Integer> recordIdsReference, Map<Object, Integer> featureIdsReference) { if (featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should not be empty."); } int n = newData.size(); int d = featureIdsReference.size(); MatrixDataframe m = new MatrixDataframe(new OpenMapRealMatrix(n, d), new ArrayRealVector(n)); if (newData.isEmpty()) { return m; } boolean extractY = (newData.getYDataType() == TypeInference.DataType.NUMERICAL); boolean addConstantColumn = featureIdsReference.containsKey(Dataframe.COLUMN_NAME_CONSTANT); int rowId = 0; for (Map.Entry<Integer, Record> e : newData.entries()) { Integer rId = e.getKey(); Record r = e.getValue(); if (recordIdsReference != null) { recordIdsReference.put(rId, rowId); } if (extractY) { m.Y.setEntry(rowId, TypeInference.toDouble(r.getY())); } if (addConstantColumn) { m.X.setEntry(rowId, 0, 1.0); //add the constant column } for (Map.Entry<Object, Object> entry : r.getX().entrySet()) { Object feature = entry.getKey(); Double value = TypeInference.toDouble(entry.getValue()); if (value != null) { Integer featureId = featureIdsReference.get(feature); if (featureId != null) {//if the feature exists in our database m.X.setEntry(rowId, featureId, value); } } //else the X matrix maintains the 0.0 default value } ++rowId; } return m; }
From source file:de.hofuniversity.iisys.neo4j.websock.util.JsonConverter.java
/** * Converts a generic websocket query into JSON. * Detects maps and lists that are already in the proper wrapped format and * lets them pass through.//from w w w .j a v a 2s .c o m * The query given must not be null. * * @param query query to convert * @return query converted to JSON */ @SuppressWarnings("unchecked") public static JSONObject toJson(final WebsockQuery query) throws JSONException { final JSONObject json = new JSONObject(); //basic attributes json.put(WebsockConstants.QUERY_ID, query.getId()); json.put(WebsockConstants.QUERY_TYPE, query.getType().getCode()); //parameters Map<String, Object> params = query.getParameters(); if (params != null && !params.isEmpty()) { if (params instanceof JSONMap) { json.put(WebsockConstants.PARAMETERS, ((JSONMap) params).getJson()); } else { json.put(WebsockConstants.PARAMETERS, convertFromMap(params)); } } //payload Object payload = query.getPayload(); if (payload != null) { if (payload instanceof Map) { if (payload instanceof JSONMap) { payload = ((JSONMap) payload).getJson(); } else { payload = convertFromMap((Map<String, ?>) payload); } } else if (payload instanceof List) { if (payload instanceof JSONList) { payload = ((JSONList) payload).getJson(); } else { payload = convertFromList((List<?>) payload); } } json.put(WebsockConstants.PAYLOAD, payload); } return json; }
From source file:org.bndtools.rt.repository.marshall.CapReqJson.java
public static void writeCapability(Capability capability, JsonGenerator generator) throws IOException { generator.writeStartObject();/* w ww. j a v a 2 s .co m*/ generator.writeStringField("ns", capability.getNamespace()); Map<String, Object> attrs = capability.getAttributes(); if (!attrs.isEmpty()) { generator.writeArrayFieldStart("attrs"); for (Entry<String, Object> entry : attrs.entrySet()) { writeAttribute(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } Map<String, String> dirs = capability.getDirectives(); if (!dirs.isEmpty()) { generator.writeArrayFieldStart("dirs"); for (Entry<String, String> entry : dirs.entrySet()) { writeDirective(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } generator.writeEndObject(); }
From source file:org.bndtools.rt.repository.marshall.CapReqJson.java
public static void writeRequirement(Requirement requirement, JsonGenerator generator) throws IOException { generator.writeStartObject();// ww w .j a va 2s.c o m generator.writeStringField("ns", requirement.getNamespace()); Map<String, Object> attrs = requirement.getAttributes(); if (!attrs.isEmpty()) { generator.writeArrayFieldStart("attrs"); for (Entry<String, Object> entry : attrs.entrySet()) { writeAttribute(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } Map<String, String> dirs = requirement.getDirectives(); if (!dirs.isEmpty()) { generator.writeArrayFieldStart("dirs"); for (Entry<String, String> entry : dirs.entrySet()) { writeDirective(entry.getKey(), entry.getValue(), generator); } generator.writeEndArray(); } generator.writeEndObject(); }
From source file:com.dell.asm.asmcore.asmmanager.util.razor.RazorUtil.java
/** * Parse the return value of the razor node GET api, e.g. * http://RAZOR_HOST/api/collections/nodes/node2 * * @param mapper ObjectMapper to parse JSON * @param nodeJson Json node data// ww w . ja v a 2s . c o m * @return Device object */ public static RazorDevice parseNodeJson(ObjectMapper mapper, String nodeJson) throws IOException { Map node = mapper.readValue(nodeJson, Map.class); Map hwInfo = (Map) node.get(NODE_HW_INFO_KEY); String uuid = (String) hwInfo.get(NODE_HW_INFO_UUID_KEY); RazorDevice ret = new RazorDevice(); ret.setId((String) node.get("name")); ret.setUuid(uuid); Map policy = (Map) node.get(NODE_POLICY); if (policy == null || policy.isEmpty()) { ret.setStatus(RazorDeviceStatus.UNPROVISIONED); } else { //System.out.println("Policy..."); for (Object key : policy.keySet()) { String policyName = (String) key; Object policyValueObject = policy.get(policyName); String policyValueString = policyValueObject.toString(); ret.getPolicy().put(policyName, policyValueString); //System.out.println(policyName + ":" + policyValueString); } //System.out.println(""); ret.setStatus(RazorDeviceStatus.PROVISIONED); } List tagslist = (List) node.get("tags"); if (tagslist != null && tagslist.size() > 0) { for (Object aTagslist : tagslist) { Map tags = (Map) aTagslist; if (tags != null && tags.size() > 0) { //System.out.println("Tag..."); for (Object key : tags.keySet()) { String tagName = (String) key; Object tagValueObject = tags.get(tagName); String tagValueString = tagValueObject.toString(); ret.getTags().put(tagName, tagValueString); //System.out.println(tagName + ":" + tagValueString); } //System.out.println(""); } } } Map facts = (Map) node.get(NODE_FACTS_KEY); if (facts == null) { LOGGER.warn("No facts found for node uuid " + ret.getId()); } else { for (Object key : facts.keySet()) { String factName = (String) key; Object factValueObject = facts.get(factName); // Fact values are generally strings, but can be other raw types like integers, // e.g. for physicalprocessorcount String factValue = factValueObject.toString(); ret.getFacts().put(factName, factValue); switch (factName) { case FACT_IPADDRESS_KEY: ret.setIpAddress(factValue); break; case FACT_MACADDRESS_KEY: ret.setMacAddress(factValue); break; case FACT_MANUFACTURER_KEY: ret.setManufacturer(factValue); break; } } } return ret; }
From source file:com.datumbox.framework.common.dataobjects.MatrixDataframe.java
/** * Method used to generate a training Dataframe to a MatrixDataframe and extracts its contents to Matrixes. It populates the featureIdsReference map with the mappings * between the feature names and the column ids of the matrix. Typically used * to convert the training dataset.// w ww . j a v a2 s . c o m * * @param dataset * @param addConstantColumn * @param recordIdsReference * @param featureIdsReference * @return */ public static MatrixDataframe newInstance(Dataframe dataset, boolean addConstantColumn, Map<Integer, Integer> recordIdsReference, Map<Object, Integer> featureIdsReference) { if (!featureIdsReference.isEmpty()) { throw new IllegalArgumentException("The featureIdsReference map should be empty."); } int n = dataset.size(); int d = dataset.xColumnSize(); if (addConstantColumn) { ++d; } MatrixDataframe m = new MatrixDataframe(new OpenMapRealMatrix(n, d), new ArrayRealVector(n)); if (dataset.isEmpty()) { return m; } boolean extractY = (dataset.getYDataType() == TypeInference.DataType.NUMERICAL); int featureId = 0; if (addConstantColumn) { for (int row = 0; row < n; ++row) { m.X.setEntry(row, featureId, 1.0); //put the constant in evey row } featureIdsReference.put(Dataframe.COLUMN_NAME_CONSTANT, featureId); ++featureId; } int rowId = 0; for (Map.Entry<Integer, Record> e : dataset.entries()) { Integer rId = e.getKey(); Record r = e.getValue(); if (recordIdsReference != null) { recordIdsReference.put(rId, rowId); } if (extractY) { m.Y.setEntry(rowId, TypeInference.toDouble(r.getY())); } for (Map.Entry<Object, Object> entry : r.getX().entrySet()) { Object feature = entry.getKey(); Integer knownFeatureId = featureIdsReference.get(feature); if (knownFeatureId == null) { featureIdsReference.put(feature, featureId); knownFeatureId = featureId; ++featureId; } Double value = TypeInference.toDouble(entry.getValue()); if (value != null) { m.X.setEntry(rowId, knownFeatureId, value); } //else the X matrix maintains the 0.0 default value } ++rowId; } return m; }
From source file:com.wx.kernel.util.HttpKit.java
/** * Build queryString of the url/*from w ww .j a v a2 s .c om*/ */ private static String buildUrlWithQueryString(String url, Map<String, String> queryParas) { if (queryParas == null || queryParas.isEmpty()) return url; StringBuilder sb = new StringBuilder(url); boolean isFirst; if (url.indexOf("?") == -1) { isFirst = true; sb.append("?"); } else { isFirst = false; } for (Entry<String, String> entry : queryParas.entrySet()) { if (isFirst) isFirst = false; else sb.append("&"); String key = entry.getKey(); String value = entry.getValue(); if (StringUtils.isNotEmpty(value)) try { value = URLEncoder.encode(value, CHARSET); } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } sb.append(key).append("=").append(value); } return sb.toString(); }