Example usage for org.apache.commons.collections.keyvalue DefaultKeyValue DefaultKeyValue

List of usage examples for org.apache.commons.collections.keyvalue DefaultKeyValue DefaultKeyValue

Introduction

In this page you can find the example usage for org.apache.commons.collections.keyvalue DefaultKeyValue DefaultKeyValue.

Prototype

public DefaultKeyValue(final Object key, final Object value) 

Source Link

Document

Constructs a new pair with the specified key and given value.

Usage

From source file:br.ufal.cideei.util.count.MetricsTable.java

public void setProperty(String method, String property, String value) {
    map.put(method, new DefaultKeyValue(property, value));
    if (map.size(method) == MAX_SIZE) {
        dumpEntry(method, map.getCollection(method));
        map.remove(method);//from   ww  w  . j a  v a  2 s.  c o m
    }
    if (!columnMapping.containsValue(property)) {
        columnMapping.put(columnCounter, property);
        columnCounter++;
    }
}

From source file:br.ufal.cideei.util.count.MetricsTable.java

public void setProperty(String method, String property, Double value) {
    map.put(method, new DefaultKeyValue(property, value));
    if (map.size(method) == MAX_SIZE) {
        dumpEntry(method, map.getCollection(method));
        map.remove(method);/*  www  .j a v a 2s  . com*/
    }
    if (!columnMapping.containsValue(property)) {
        columnMapping.put(columnCounter, property);
        columnCounter++;
    }
}

From source file:metrics.sink.MetricsTable.java

public void setProperty(ID id, String property, String value) {
    if (id == null)
        throw new IllegalArgumentException("The element must not be null. ");
    map.put(id, new DefaultKeyValue(property, value));
    if (map.size(id) == MAX_NUMBER_PROPERTIES) {
        dumpEntry(id, map.getCollection(id));
        map.remove(id);//from w w w  . j a va2 s  .com
    }
    if (!columnMapping.containsValue(property)) {
        columnMapping.put(columnCount, property);
        columnCount++;
    }
}

From source file:metrics.sink.MetricsTable.java

public void setProperty(ID id, String property, Number value) {
    map.put(id, new DefaultKeyValue(property, value));
    if (map.size(id) == MAX_NUMBER_PROPERTIES) {
        dumpEntry(id, map.getCollection(id));
        map.remove(id);//from  ww  w .  j  ava  2s . c  o  m
    }
    if (!columnMapping.containsValue(property)) {
        columnMapping.put(columnCount, property);
        columnCount++;
    }
}

From source file:com.ebuddy.cassandra.dao.ColumnFamilyTemplateTest.java

@Test(groups = { "unit" })
public void testReadColumns() throws Exception {
    Map<String, String> testResultMap = new HashMap<String, String>();
    testResultMap.put(columnNames.get(0), columnValues.get(0));
    testResultMap.put(columnNames.get(1), columnValues.get(1));

    ColumnSlice columnSlice = mock(ColumnSlice.class);
    HColumn column1 = mock(HColumn.class);
    HColumn column2 = mock(HColumn.class);

    when(column1.getName()).thenReturn(columnNames.get(0));
    when(column1.getValue()).thenReturn(columnValues.get(0));
    when(column2.getName()).thenReturn(columnNames.get(1));
    when(column2.getValue()).thenReturn(columnValues.get(1));

    when(columnSlice.getColumns()).thenReturn(Arrays.asList(column1, column2));
    when(executionResult.get()).thenReturn(columnSlice);

    //=========================
    List<KeyValue> actualResult = columnFamilyTestDao.readColumns(rowKey,
            new ColumnMapper<KeyValue, String, String>() {
                @Override// w  w w.  jav  a  2  s.  c o m
                public KeyValue mapColumn(String columnName, String columnValue) {
                    return new DefaultKeyValue(columnName, columnValue);
                }
            });
    //=========================

    Map<String, String> resultMap = new HashMap<String, String>();
    for (KeyValue kv : actualResult) {
        resultMap.put((String) kv.getKey(), (String) kv.getValue());
    }

    assertEquals(resultMap, testResultMap);
}

From source file:org.apache.cocoon.el.impl.objectmodel.ObjectModelImpl.java

public Object put(Object key, Object value) {
    modified = true;//from  w  w  w . j a va2s  .c o m
    if (!localContexts.empty()) {
        ((ArrayStack) localContexts.peek()).push(new DefaultKeyValue(key, value));
    }

    singleValueMap.put(key, value);
    multiValueMap.put(key, value);

    return value;
}

From source file:org.apache.cocoon.el.impl.objectmodel.ObjectModelImpl.java

public void putAll(Map mapToCopy) {
    modified = true;/*from  w  ww  . j  av  a  2s .com*/
    if (!localContexts.empty()) {
        ArrayStack entries = (ArrayStack) localContexts.peek();
        for (Iterator keysIterator = mapToCopy.keySet().iterator(); keysIterator.hasNext();) {
            Object key = keysIterator.next();
            entries.push(new DefaultKeyValue(key, mapToCopy.get(key)));
        }
    }

    singleValueMap.putAll(mapToCopy);
    multiValueMap.putAll(mapToCopy);
}

From source file:org.easyrec.plugin.aggregator.impl.AggregatorServiceImpl.java

private List<KeyValue> convertAndOrderField(HashMap<String, Integer> tmpField, Integer threshold) {

    List<Entry<String, Integer>> list = new ArrayList<>(tmpField.entrySet());
    Collections.sort(list, new Comparator<Entry<String, Integer>>() {
        @Override//from   ww  w . ja va 2  s.co  m
        public int compare(Entry<String, Integer> o1, Entry<String, Integer> o2) {
            return o2.getValue().compareTo(o1.getValue()); //Note: exchanged o2 and o1 for descending order!
        }
    });

    List<KeyValue> result = new ArrayList<>();
    int i = 0;
    for (Iterator<Entry<String, Integer>> it = list.iterator(); it.hasNext(); i++) {
        if (i >= threshold)
            break;
        Entry<String, Integer> entry = it.next();
        result.add(new DefaultKeyValue(entry.getKey(), entry.getValue()));
    }
    return result;

}

From source file:org.jahia.services.seo.jcr.VanityUrlManager.java

/**
 * Add, update or delete all vanity URL mappings for a specific content node and the language codes
 * set in the list of VanityUrl beans. First we load all existing mappings for all the languages
 * set in the updatedLocales collection. Then we compare the existing with the list of URL mappings
 * given in the vanityUrls collection to know which nodes need to be added, updated or deleted.
 *
 * If the default mapping for a language is set for a new or updated mapping, then we check if
 * there already is another default URL for this language and set its default flag to false.
 *
 * We also check whether the same added URL is already existing for a different node or language
 * in the current site (and which is not being deleted in the same operation) and throw a
 * ConstraintViolationException if this is the case. We also throw a ConstraintViolationException
 * if two URL mappings for the same language in the given vanityUrls collection have the default flag
 * set to true.//from w w  w  . ja  va2 s.  c  o m
 *
 * @param contentNode the content node for which to add the given mapping
 * @param vanityUrls the list of VanityUrls bean representing the URLs to be added or updated
 * @param updatedLocales a set with all locales, which have been edited (e.g. if all mappings for a language
 *        need to be deleted, add the language to this set, while in the vanityUrls list there will be no
 *        mappings for that language)
 * @param session the JCR session used to find and save the vanity URL nodes
 * @return true if any vanity URL was added,updated or deleted or false if no change was done
 * @throws ConstraintViolationException if the vanity URL mapping already exists for a different content node or language in the site
 * @throws RepositoryException if there was an unexpected exception accessing the repository
 */
public boolean saveVanityUrlMappings(JCRNodeWrapper contentNode, List<VanityUrl> vanityUrls,
        final Set<String> updatedLocales, JCRSessionWrapper session) throws RepositoryException {
    Map<String, Map<String, VanityUrl>> existingMappings = new HashMap<String, Map<String, VanityUrl>>();
    Map<String, KeyValue> oldDefaultMappings = new HashMap<String, KeyValue>();
    JCRNodeWrapper vanityUrlMappingsNode = null;
    if (!contentNode.hasNode(VANITYURLMAPPINGS_NODE)) {
        if (!contentNode.isNodeType(JAHIAMIX_VANITYURLMAPPED)) {
            contentNode.addMixin(JAHIAMIX_VANITYURLMAPPED);
        }
        vanityUrlMappingsNode = contentNode.addNode(VANITYURLMAPPINGS_NODE, JAHIANT_VANITYURLS);
    } else {
        vanityUrlMappingsNode = contentNode.getNode(VANITYURLMAPPINGS_NODE);

        // first we get all existing mappings and find the old default mappings per language
        for (NodeIterator it = vanityUrlMappingsNode.getNodes(); it.hasNext();) {
            JCRNodeWrapper currentNode = (JCRNodeWrapper) it.next();
            String language = currentNode.getPropertyAsString(JCR_LANGUAGE);
            if (updatedLocales.contains(language)) {
                Map<String, VanityUrl> existingVanityUrls = existingMappings.get(language);
                if (existingVanityUrls == null) {
                    existingMappings.put(language, new HashMap<String, VanityUrl>());
                }
                VanityUrl vanityUrl = populateJCRData(currentNode, new VanityUrl());
                existingMappings.get(language).put(currentNode.getName(), vanityUrl);
                if (currentNode.getProperty(PROPERTY_DEFAULT).getBoolean()) {
                    oldDefaultMappings.put(language, new DefaultKeyValue(currentNode.getName(), vanityUrl));
                }
            }
        }
    }

    // as next we need to find out, which mappings need to be updated or added and
    // get the collection of new default mappings.
    List<VanityUrl> toAdd = new ArrayList<VanityUrl>();
    Map<String, VanityUrl> toUpdate = new HashMap<String, VanityUrl>();
    Map<String, VanityUrl> newDefaultMappings = new HashMap<String, VanityUrl>();

    for (VanityUrl vanityUrl : vanityUrls) {
        if (vanityUrl.isDefaultMapping()) {
            VanityUrl otherDefaultMapping = newDefaultMappings.put(vanityUrl.getLanguage(), vanityUrl);
            if (otherDefaultMapping != null) {

                throw new ConstraintViolationException(
                        "Two mappings are set as default for the same language: " + vanityUrl.getUrl() + " and "
                                + otherDefaultMapping.getUrl() + " for language: " + vanityUrl.getLanguage(),
                        null);
            }
        }
        boolean found = false;
        Map<String, VanityUrl> mappings = existingMappings.get(vanityUrl.getLanguage());
        if (mappings != null) {
            for (Map.Entry<String, VanityUrl> entry : mappings.entrySet()) {
                if (entry.getValue().equals(vanityUrl)) {
                    mappings.remove(entry.getKey());
                    found = true;
                    if (entry.getValue().isActive() != vanityUrl.isActive()
                            || entry.getValue().isDefaultMapping() != vanityUrl.isDefaultMapping()) {
                        vanityUrl.setIdentifier(entry.getValue().getIdentifier());
                        toUpdate.put(entry.getKey(), vanityUrl);
                    }
                    break;
                }
            }
        }
        if (!found && updatedLocales.contains(vanityUrl.getLanguage())) {
            toAdd.add(vanityUrl);
        }
    }
    // Compare the new default settings with the old ones to see which mapping should
    // be default. Also consider the case, that in the new collection none is set to
    // default, then we take the previous default one or if there was also no default,
    // then the first found mapping for a language will be default.
    if (!newDefaultMappings.keySet().containsAll(updatedLocales)) {
        for (String locale : updatedLocales) {
            if (!newDefaultMappings.containsKey(locale)) {
                boolean defaultWasSet = false;
                VanityUrl oldDefaultVanityUrl = null;
                if (oldDefaultMappings.get(locale) != null) {
                    oldDefaultVanityUrl = (VanityUrl) oldDefaultMappings.get(locale).getValue();

                    for (Map.Entry<String, VanityUrl> entry : toUpdate.entrySet()) {
                        VanityUrl vanityUrl = entry.getValue();
                        if (vanityUrl.equals(oldDefaultVanityUrl)) {
                            vanityUrl.setDefaultMapping(true);
                            newDefaultMappings.put(locale, vanityUrl);
                            defaultWasSet = true;
                        }
                    }
                }
                if (!defaultWasSet) {
                    for (VanityUrl vanityUrl : vanityUrls) {
                        if (locale.equals(vanityUrl.getLanguage())) {
                            vanityUrl.setDefaultMapping(true);
                            newDefaultMappings.put(locale, vanityUrl);
                            break;
                        }
                    }
                }
            }
        }
    }
    // At last we need to see, which mappings are no longer existing in the new collection,
    // which means that they need to be completely removed
    List<Map.Entry<String, VanityUrl>> toDelete = new ArrayList<Map.Entry<String, VanityUrl>>();
    for (Map<String, VanityUrl> existingVanityUrls : existingMappings.values()) {
        toDelete.addAll(existingVanityUrls.entrySet());
    }
    // Compare the new default settings with the old ones to know if the default flag needs
    // to be set to false for the previous default.
    List<String> removeDefaultMapping = new ArrayList<String>();
    for (Map.Entry<String, KeyValue> oldDefaultMapping : oldDefaultMappings.entrySet()) {
        VanityUrl oldDefaultVanityUrl = (VanityUrl) oldDefaultMapping.getValue().getValue();
        VanityUrl newDefaultVanityUrl = newDefaultMappings.get(oldDefaultMapping.getKey());
        if (!oldDefaultVanityUrl.equals(newDefaultVanityUrl)) {
            boolean oldDefaultWillBeDeleted = false;
            for (Map.Entry<String, VanityUrl> entry : toDelete) {
                if (oldDefaultVanityUrl.equals(entry.getValue())) {
                    oldDefaultWillBeDeleted = true;
                    break;
                }
            }
            if (!(oldDefaultWillBeDeleted || toUpdate.values().contains(oldDefaultVanityUrl))) {
                removeDefaultMapping.add((String) oldDefaultMapping.getValue().getKey());
            }
        }
    }

    // Check if the added vanity URLs are really unique for the site
    for (VanityUrl vanityUrl : toAdd) {
        checkUniqueConstraint(contentNode, vanityUrl, toDelete);
    }

    // If there is no change do nothing otherwise do all the operations and
    // save the session
    if (toUpdate.isEmpty() && toAdd.isEmpty() && toDelete.isEmpty()) {
        return false;
    } else {
        session.checkout(vanityUrlMappingsNode);
        for (Map.Entry<String, VanityUrl> entry : toUpdate.entrySet()) {
            JCRNodeWrapper vanityUrlNode = vanityUrlMappingsNode.getNode(entry.getKey());
            VanityUrl vanityUrl = entry.getValue();
            session.checkout(vanityUrlNode);
            vanityUrlNode.setProperty(PROPERTY_ACTIVE, vanityUrl.isActive());
            vanityUrlNode.setProperty(PROPERTY_DEFAULT, vanityUrl.isDefaultMapping());
        }
        for (String index : removeDefaultMapping) {
            JCRNodeWrapper vanityUrlNode = vanityUrlMappingsNode.getNode(index);
            session.checkout(vanityUrlNode);
            vanityUrlNode.setProperty(PROPERTY_DEFAULT, false);
        }
        for (Map.Entry<String, VanityUrl> entry : toDelete) {
            JCRNodeWrapper vanityUrlNode = vanityUrlMappingsNode.getNode(entry.getKey());
            session.checkout(vanityUrlNode);
            vanityUrlNode.remove();
        }

        for (VanityUrl vanityUrl : toAdd) {
            JCRNodeWrapper vanityUrlNode = vanityUrlMappingsNode
                    .addNode(JCRContentUtils.escapeLocalNodeName(vanityUrl.getUrl()), JAHIANT_VANITYURL);
            session.checkout(vanityUrlNode);
            vanityUrlNode.setProperty(PROPERTY_URL, vanityUrl.getUrl());
            vanityUrlNode.setProperty(JCR_LANGUAGE, vanityUrl.getLanguage());
            vanityUrlNode.setProperty(PROPERTY_ACTIVE, vanityUrl.isActive());
            vanityUrlNode.setProperty(PROPERTY_DEFAULT, vanityUrl.isDefaultMapping());
        }
        session.save();
    }
    return true;
}

From source file:org.jahia.taglibs.facet.Functions.java

/**
 * Get a list of applied facets//from  www.  j av  a 2 s. c  o  m
 *
 * @param filterString the already decoded filter String from the query parameter
 * @return a Map with the facet group as key and a KeyValue with the facet value as key and the query as value
 * @see org.jahia.taglibs.functions.Functions#decodeUrlParam(String)
 */
public static Map<String, List<KeyValue>> getAppliedFacetFilters(String filterString) {
    Map<String, List<KeyValue>> appliedFacetFilters = new LinkedHashMap<String, List<KeyValue>>();
    if (!StringUtils.isEmpty(filterString)) {
        for (String filterInstance : FILTER_STRING_PATTERN.split(filterString)) {
            String[] filterTokens = FACET_PARAM_DELIM_PATTERN.split(filterInstance);
            if (filterTokens.length == 3) {
                List<KeyValue> filterList = appliedFacetFilters.get(filterTokens[0]);
                if (filterList == null) {
                    filterList = new ArrayList<KeyValue>();
                    appliedFacetFilters.put(filterTokens[0], filterList);
                }
                filterList.add(new DefaultKeyValue(filterTokens[1], filterTokens[2]));
            }
        }
    }
    return appliedFacetFilters;
}