Search in sources :

Example 76 with LinkedHashMap

use of java.util.LinkedHashMap in project camel by apache.

the class JSonSchemaHelper method parseJsonSchema.

/**
     * Parses the json schema to split it into a list or rows, where each row contains key value pairs with the metadata
     *
     * @param group the group to parse from such as <tt>component</tt>, <tt>componentProperties</tt>, or <tt>properties</tt>.
     * @param json the json
     * @return a list of all the rows, where each row is a set of key value pairs with metadata
     */
public static List<Map<String, String>> parseJsonSchema(String group, String json, boolean parseProperties) {
    List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
    if (json == null) {
        return answer;
    }
    boolean found = false;
    // parse line by line
    String[] lines = json.split("\n");
    for (String line : lines) {
        // we need to find the group first
        if (!found) {
            String s = line.trim();
            found = s.startsWith("\"" + group + "\":") && s.endsWith("{");
            continue;
        }
        // we should stop when we end the group
        if (line.equals("  },") || line.equals("  }")) {
            break;
        }
        // need to safe encode \" so we can parse the line
        line = line.replaceAll("\"\\\\\"\"", '"' + QUOT + '"');
        Map<String, String> row = new LinkedHashMap<String, String>();
        Matcher matcher = PATTERN.matcher(line);
        String key;
        if (parseProperties) {
            // when parsing properties the first key is given as name, so the first parsed token is the value of the name
            key = "name";
        } else {
            key = null;
        }
        while (matcher.find()) {
            if (key == null) {
                key = matcher.group(1);
            } else {
                String value = matcher.group(1);
                if (value != null) {
                    // its text based
                    value = value.trim();
                    // decode
                    value = value.replaceAll(QUOT, "\"");
                    value = decodeJson(value);
                }
                if (value == null) {
                    // not text then its maybe an enum?
                    value = matcher.group(2);
                    if (value != null) {
                        // its an enum so strip out " and trim spaces after comma
                        value = value.replaceAll("\"", "");
                        value = value.replaceAll(", ", ",");
                        value = value.trim();
                    }
                }
                if (value == null) {
                    // not text then its maybe a boolean?
                    value = matcher.group(3);
                }
                if (value == null) {
                    // not text then its maybe a integer?
                    value = matcher.group(4);
                }
                if (value != null) {
                    row.put(key, value);
                }
                // reset
                key = null;
            }
        }
        if (!row.isEmpty()) {
            answer.add(row);
        }
    }
    return answer;
}
Also used : Matcher(java.util.regex.Matcher) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 77 with LinkedHashMap

use of java.util.LinkedHashMap in project camel by apache.

the class CamelControllerImpl method getCamelContexts.

public List<Map<String, String>> getCamelContexts() throws Exception {
    List<Map<String, String>> answer = new ArrayList<Map<String, String>>();
    List<CamelContext> camelContexts = getLocalCamelContexts();
    for (CamelContext camelContext : camelContexts) {
        Map<String, String> row = new LinkedHashMap<String, String>();
        row.put("name", camelContext.getName());
        row.put("state", camelContext.getStatus().name());
        row.put("uptime", camelContext.getUptime());
        if (camelContext.getManagedCamelContext() != null) {
            row.put("exchangesTotal", "" + camelContext.getManagedCamelContext().getExchangesTotal());
            row.put("exchangesInflight", "" + camelContext.getManagedCamelContext().getExchangesInflight());
            row.put("exchangesFailed", "" + camelContext.getManagedCamelContext().getExchangesFailed());
        } else {
            row.put("exchangesTotal", "0");
            row.put("exchangesInflight", "0");
            row.put("exchangesFailed", "0");
        }
        answer.add(row);
    }
    return answer;
}
Also used : CamelContext(org.apache.camel.CamelContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 78 with LinkedHashMap

use of java.util.LinkedHashMap in project jforum2 by rafaelsteil.

the class PostRepository method selectAllByTopicByLimit.

public static List selectAllByTopicByLimit(int topicId, int start, int count) {
    String tid = Integer.toString(topicId);
    List posts = (List) cache.get(FQN, tid);
    if (posts == null || posts.size() == 0) {
        PostDAO pm = DataAccessDriver.getInstance().newPostDAO();
        posts = pm.selectAllByTopic(topicId);
        for (Iterator iter = posts.iterator(); iter.hasNext(); ) {
            PostCommon.preparePostForDisplay((Post) iter.next());
        }
        Map topics = (Map) cache.get(FQN);
        if (topics == null || topics.size() == 0 || topics.size() < CACHE_SIZE) {
            cache.add(FQN, tid, posts);
        } else {
            if (!(topics instanceof LinkedHashMap)) {
                topics = new LinkedHashMap(topics) {

                    protected boolean removeEldestEntry(java.util.Map.Entry eldest) {
                        return this.size() > CACHE_SIZE;
                    }
                };
            }
            topics.put(tid, posts);
            cache.add(FQN, topics);
        }
    }
    int size = posts.size();
    return posts.subList(start, (size < start + count) ? size : start + count);
}
Also used : PostDAO(net.jforum.dao.PostDAO) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 79 with LinkedHashMap

use of java.util.LinkedHashMap in project rest.li by linkedin.

the class TestQueryParamsDataMap method testProcessProjections.

/**
   * Creates MaskTrees and extracts the subsequent DataMap to verify that processProjections can correctly
   * convert it correctly into a Map that can later be constructed and encoded into an URI
   */
@Test
public void testProcessProjections() {
    //Construct a MaskTree from a series of PathSpecs. Extract the subsequent Datamap representation.
    final MaskTree rootObjectsMask = MaskCreator.createPositiveMask(new PathSpec("foo", PathSpec.WILDCARD, "bar"));
    final MaskTree metadataMask = MaskCreator.createPositiveMask(new PathSpec("foo", "bar"), new PathSpec("bar", "baz"), new PathSpec("qux"));
    final MaskTree pagingMask = MaskCreator.createPositiveMask(new PathSpec("total"), new PathSpec("count"), new PathSpec("links", PathSpec.WILDCARD, "rel"));
    //For each type of projection, plus one query string parameter
    final DataMap resultMap = new DataMap(4);
    resultMap.put(RestConstants.FIELDS_PARAM, rootObjectsMask.getDataMap());
    resultMap.put(RestConstants.METADATA_FIELDS_PARAM, metadataMask.getDataMap());
    resultMap.put(RestConstants.PAGING_FIELDS_PARAM, pagingMask.getDataMap());
    resultMap.put("someQueryString", "someValue");
    final Map<String, List<String>> processedProjections = new LinkedHashMap<String, List<String>>();
    final DataMap processedDataMap = QueryParamsDataMap.processProjections(resultMap, processedProjections);
    Assert.assertTrue(processedDataMap.size() == 1, "Processed datamap should only have one item left!");
    final Map<String, Set<String>> expectedProcessedProjections = new LinkedHashMap<String, Set<String>>();
    //"{fields=[foo:($*:(bar))], metadataFields=[foo:(bar),bar:(baz),qux], pagingFields=[total,count,links:($*:(rel))]}"
    expectedProcessedProjections.put(RestConstants.FIELDS_PARAM, Collections.singleton("foo:($*:(bar))"));
    expectedProcessedProjections.put(RestConstants.METADATA_FIELDS_PARAM, new HashSet<String>(Arrays.asList("foo:(bar)", "bar:(baz)", "qux")));
    expectedProcessedProjections.put(RestConstants.PAGING_FIELDS_PARAM, new HashSet<String>(Arrays.asList("total", "count", "links:($*:(rel))")));
    Assert.assertEquals(processedProjections.size(), expectedProcessedProjections.size(), "We must have the correct number of" + " expected projections!");
    for (final Map.Entry<String, List<String>> entry : processedProjections.entrySet()) {
        //Acceptable because these are always comma delimited
        final Set<String> actualProjectionValueSet = new HashSet<String>(Arrays.asList(entry.getValue().get(0).split(",")));
        Assert.assertEquals(actualProjectionValueSet, expectedProcessedProjections.get(entry.getKey()), "The individual projection " + "for " + entry.getKey() + " does not match what is expected!");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) PathSpec(com.linkedin.data.schema.PathSpec) DataMap(com.linkedin.data.DataMap) LinkedHashMap(java.util.LinkedHashMap) MaskTree(com.linkedin.data.transform.filter.request.MaskTree) DataList(com.linkedin.data.DataList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) DataMap(com.linkedin.data.DataMap) Map(java.util.Map) HashSet(java.util.HashSet) Test(org.testng.annotations.Test)

Example 80 with LinkedHashMap

use of java.util.LinkedHashMap in project okhttp by square.

the class RequestBodyCompression method run.

public void run() throws Exception {
    Map<String, String> requestBody = new LinkedHashMap<>();
    requestBody.put("longUrl", "https://publicobject.com/2014/12/04/html-formatting-javadocs/");
    RequestBody jsonRequestBody = RequestBody.create(MEDIA_TYPE_JSON, mapJsonAdapter.toJson(requestBody));
    Request request = new Request.Builder().url("https://www.googleapis.com/urlshortener/v1/url?key=" + GOOGLE_API_KEY).post(jsonRequestBody).build();
    try (Response response = client.newCall(request).execute()) {
        if (!response.isSuccessful())
            throw new IOException("Unexpected code " + response);
        System.out.println(response.body().string());
    }
}
Also used : Response(okhttp3.Response) Request(okhttp3.Request) IOException(java.io.IOException) LinkedHashMap(java.util.LinkedHashMap) RequestBody(okhttp3.RequestBody)

Aggregations

LinkedHashMap (java.util.LinkedHashMap)2398 Map (java.util.Map)691 ArrayList (java.util.ArrayList)675 HashMap (java.util.HashMap)439 Test (org.junit.Test)317 List (java.util.List)298 IOException (java.io.IOException)152 HashSet (java.util.HashSet)128 Set (java.util.Set)110 LinkedHashSet (java.util.LinkedHashSet)100 File (java.io.File)93 LinkedList (java.util.LinkedList)75 TreeMap (java.util.TreeMap)72 Node (org.apache.hadoop.hive.ql.lib.Node)70 NodeProcessor (org.apache.hadoop.hive.ql.lib.NodeProcessor)68 Rule (org.apache.hadoop.hive.ql.lib.Rule)68 GraphWalker (org.apache.hadoop.hive.ql.lib.GraphWalker)66 Dispatcher (org.apache.hadoop.hive.ql.lib.Dispatcher)65 Iterator (java.util.Iterator)64 DefaultRuleDispatcher (org.apache.hadoop.hive.ql.lib.DefaultRuleDispatcher)64