Java Code Examples for java.util.NavigableMap#putAll()

The following examples show how to use java.util.NavigableMap#putAll() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ArrayStoreConverter.java    From jstarcraft-core with Apache License 2.0 6 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    Class<?> componentClass = null;
    Type componentType = null;
    if (type instanceof GenericArrayType) {
        GenericArrayType genericArrayType = GenericArrayType.class.cast(type);
        componentType = genericArrayType.getGenericComponentType();
        componentClass = TypeUtility.getRawType(componentType, null);
    } else {
        Class<?> clazz = TypeUtility.getRawType(type, null);
        componentType = clazz.getComponentType();
        componentClass = clazz.getComponentType();
    }
    Specification specification = Specification.getSpecification(componentClass);
    StoreConverter converter = context.getStoreConverter(specification);
    int size = Array.getLength(instance);
    IndexableField indexable = new StoredField(path + ".size", size);
    indexables.put(path + ".size", indexable);
    for (int index = 0; index < size; index++) {
        Object element = Array.get(instance, index);
        indexables.putAll(converter.encode(context, path + "[" + index + "]", field, annotation, componentType, element));
    }
    return indexables;
}
 
Example 2
Source File: InMemoryDataReaderDAO.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Iterator<Change> readTimeline(Key key, boolean includeContentData,
                                     UUID start, UUID end, boolean reversed, long limit, ReadConsistency consistency) {
    checkNotNull(key, "key");

    String table = key.getTable().getName();

    Ordering<UUID> ordering = reversed ? TimeUUIDs.ordering().reverse() : TimeUUIDs.ordering();
    NavigableMap<UUID, Change> map = Maps.newTreeMap(ordering);
    if (includeContentData) {
        map.putAll(safeGet(_contentChanges, table, key.getKey()));
    }
    if (start != null) {
        map = map.tailMap(start, true);
    }
    if (end != null) {
        map = map.headMap(end, true);
    }
    return Iterators.limit(map.values().iterator(), (int) Math.min(limit, Integer.MAX_VALUE));
}
 
Example 3
Source File: MapPoll.java    From PGM with GNU Affero General Public License v3.0 5 votes vote down vote up
private void selectMaps() {
  // Sorting beforehand, saves future key remaps, as bigger values are placed at the end
  List<MapInfo> sortedDist =
      mapScores.entrySet().stream()
          .sorted(Comparator.comparingDouble(Map.Entry::getValue))
          .map(Map.Entry::getKey)
          .collect(Collectors.toList());

  NavigableMap<Double, MapInfo> cumulativeScores = new TreeMap<>();
  double maxWeight = cummulativeMap(0, sortedDist, cumulativeScores);

  for (int i = 0; i < voteSize; i++) {
    NavigableMap<Double, MapInfo> subMap =
        cumulativeScores.tailMap(Math.random() * maxWeight, true);
    Map.Entry<Double, MapInfo> selected = subMap.pollFirstEntry();

    if (selected == null) break; // No more maps to poll
    votes.put(selected.getValue(), new HashSet<>()); // Add map to votes
    if (votes.size() >= voteSize) break; // Skip replace logic after all maps have been selected

    // Remove map from pool, updating cumulative scores
    double selectedWeight = getWeight(selected.getValue());
    maxWeight -= selectedWeight;

    NavigableMap<Double, MapInfo> temp = new TreeMap<>();
    cummulativeMap(selected.getKey() - selectedWeight, subMap.values(), temp);

    subMap.clear();
    cumulativeScores.putAll(temp);
  }
}
 
Example 4
Source File: MapStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Map.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type keyType = types[0];
    Class<?> keyClazz = TypeUtility.getRawType(keyType, null);
    Type valueType = types[1];
    Class<?> valueClazz = TypeUtility.getRawType(valueType, null);

    try {
        // TODO 此处需要代码重构
        Map<Object, Object> map = Map.class.cast(instance);
        Specification keySpecification = Specification.getSpecification(keyClazz);
        StoreConverter keyConverter = context.getStoreConverter(keySpecification);
        Specification valueSpecification = Specification.getSpecification(valueClazz);
        StoreConverter valueConverter = context.getStoreConverter(valueSpecification);

        int size = map.size();
        IndexableField indexable = new StoredField(path + ".size", size);
        indexables.put(path + ".size", indexable);
        int index = 0;
        for (Entry<Object, Object> keyValue : map.entrySet()) {
            Object key = keyValue.getKey();
            indexables.putAll(keyConverter.encode(context, path + "[" + index + "_key]", field, annotation, keyType, key));
            Object value = keyValue.getValue();
            indexables.putAll(valueConverter.encode(context, path + "[" + index + "_value]", field, annotation, valueType, value));
            index++;
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 5
Source File: CollectionStoreConverter.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public NavigableMap<String, IndexableField> encode(LuceneContext context, String path, Field field, LuceneStore annotation, Type type, Object instance) {
    NavigableMap<String, IndexableField> indexables = new TreeMap<>();
    // 兼容UniMi
    type = TypeUtility.refineType(type, Collection.class);
    ParameterizedType parameterizedType = ParameterizedType.class.cast(type);
    Type[] types = parameterizedType.getActualTypeArguments();
    Type elementType = types[0];
    Class<?> elementClazz = TypeUtility.getRawType(elementType, null);

    try {
        // TODO 此处需要代码重构
        Collection<?> collection = Collection.class.cast(instance);
        Specification specification = Specification.getSpecification(elementClazz);
        StoreConverter converter = context.getStoreConverter(specification);

        int size = collection.size();
        IndexableField indexable = new StoredField(path + ".size", size);
        indexables.put(path + ".size", indexable);
        int index = 0;
        for (Object element : collection) {
            indexables.putAll(converter.encode(context, path + "[" + index + "]", field, annotation, elementType, element));
            index++;
        }
        return indexables;
    } catch (Exception exception) {
        // TODO
        throw new StorageException(exception);
    }
}
 
Example 6
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * putAll adds all key-value pairs from the given map
 */
public void testPutAll() {
    NavigableMap empty = map0();
    NavigableMap map = map5();
    empty.putAll(map);
    assertEquals(5, empty.size());
    assertTrue(empty.containsKey(one));
    assertTrue(empty.containsKey(two));
    assertTrue(empty.containsKey(three));
    assertTrue(empty.containsKey(four));
    assertTrue(empty.containsKey(five));
}
 
Example 7
Source File: TreeSubMapTest.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/**
 * putAll adds all key-value pairs from the given map
 */
public void testDescendingPutAll() {
    NavigableMap empty = dmap0();
    NavigableMap map = dmap5();
    empty.putAll(map);
    assertEquals(5, empty.size());
    assertTrue(empty.containsKey(m1));
    assertTrue(empty.containsKey(m2));
    assertTrue(empty.containsKey(m3));
    assertTrue(empty.containsKey(m4));
    assertTrue(empty.containsKey(m5));
}
 
Example 8
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * putAll adds all key-value pairs from the given map
 */
public void testPutAll() {
    NavigableMap empty = map0();
    NavigableMap map = map5();
    empty.putAll(map);
    assertEquals(5, empty.size());
    assertTrue(empty.containsKey(one));
    assertTrue(empty.containsKey(two));
    assertTrue(empty.containsKey(three));
    assertTrue(empty.containsKey(four));
    assertTrue(empty.containsKey(five));
}
 
Example 9
Source File: TreeSubMapTest.java    From j2objc with Apache License 2.0 5 votes vote down vote up
/**
 * putAll adds all key-value pairs from the given map
 */
public void testDescendingPutAll() {
    NavigableMap empty = dmap0();
    NavigableMap map = dmap5();
    empty.putAll(map);
    assertEquals(5, empty.size());
    assertTrue(empty.containsKey(m1));
    assertTrue(empty.containsKey(m2));
    assertTrue(empty.containsKey(m3));
    assertTrue(empty.containsKey(m4));
    assertTrue(empty.containsKey(m5));
}
 
Example 10
Source File: QaIndicatorReportController.java    From webcurator with Apache License 2.0 4 votes vote down vote up
@Override
protected ModelAndView showForm(HttpServletRequest request,
		HttpServletResponse response, BindException errors)
		throws Exception {
	
	ModelAndView mav = new ModelAndView();
	
	// fetch the indicator oid from the request (hyperlinked from QA Summary Page)
	String iOid = request.getParameter("indicatorOid");
	
	if (iOid != null) {

		// prepare the indicator oid
		Long indicatorOid = Long.parseLong(iOid);
		
		// get the indicator
		Indicator indicator = indicatorDAO.getIndicatorByOid(indicatorOid);
		
		// add it to the ModelAndView so that we can access it within the jsp
		mav.addObject("indicator", indicator);

		// add the target instance
		TargetInstance instance = targetInstanceManager.getTargetInstance(indicator.getTargetInstanceOid());
		mav.addObject(TargetInstanceCommand.MDL_INSTANCE, instance);

		// ensure that the user belongs to the agency that created the indicator
		if (agencyUserManager.getAgenciesForLoggedInUser().contains(indicator.getAgency())) {
			// summarise the indicator values in the reportLines
			HashMap<String, Integer> summary = new HashMap<String, Integer>();
			NavigableMap<String, Integer> sortedSummary = null;
			int total = 0;
			// if the indicator is not in the exclusion list ...
			if (!excludedIndicators.containsKey(indicator.getName())) {
				// fetch the report lines for this indicator
				List<IndicatorReportLine> reportLines = indicator.getIndicatorReportLines();
				// add them ti the model
				mav.addObject("reportLines", reportLines);
				
				Comparator comparitor = null;
				
				Iterator<IndicatorReportLine> lines = reportLines.iterator();
				
				if (sortOrder.equals("countdesc")) {
					comparitor = new DescendingValueComparator(summary);
					sortedSummary = new TreeMap<String, Integer>(comparitor);
				} else if (sortOrder.equals("countasc")) {
					comparitor = new AscendingValueComparator(summary);
					sortedSummary = new TreeMap<String, Integer>(comparitor);
				} else if (sortOrder.equals("indicatorvaluedesc")) {
					sortedSummary = new TreeMap<String, Integer>().descendingMap();
				} else if (sortOrder.equals("indicatorvalueasc")) {
					sortedSummary = new TreeMap<String, Integer>();
				}
				
				while (lines.hasNext()) {
					IndicatorReportLine line = lines.next();
					if (summary.containsKey(line.getLine())) {
						// increment the current entry value
						summary.put(line.getLine(),
								summary.get(line.getLine()) + 1);
					} else {
						// initialise the value for the entry
						summary.put(line.getLine(), 1);
					}
					total++;
				}
				sortedSummary.putAll(summary);
				mav.addObject("sortedSummary", sortedSummary);
				mav.addObject("total", total);
				mav.addObject("sortorder", sortOrder);
				
				mav.setViewName("QaIndicatorReport");
			} else {
				// otherwise redirect to the configured view
		      	StringBuilder queryString = new StringBuilder("redirect:");
		      	queryString.append(excludedIndicators.get(indicator.getName()));
		      	queryString.append("?");
		      	queryString.append("indicatorOid=");
           		queryString.append(indicatorOid);

           		return new ModelAndView(queryString.toString());
			}

		}
	}
	return mav;

}