@SuppressWarnings({"rawtypes", "unchecked"})
  protected <M extends Map, F> M buildCompositeMap(
      Function<F, M> mapMaker,
      PreferenceValueFunction<M> preferenceValueFunction,
      Collection<? extends IStylesheetData> defaults,
      M... extraDataMaps) {
    final M compositeMap = mapMaker.apply(null);

    // Add data from the stylesheet descriptor
    for (final IStylesheetData defaultData : defaults) {
      final String defaultValue = defaultData.getDefaultValue();
      final String name = defaultData.getName();
      compositeMap.put(name, defaultValue);
    }

    // Add any pre-existing data to the composite map
    for (final M existingValue : extraDataMaps) {
      compositeMap.putAll(existingValue);
    }

    // Iterate through scopes adding data from each stylesheet user prefs objects
    for (final Scope scope : Scope.values()) {
      final IStylesheetUserPreferences stylesheetUserPreferences =
          this.getStylesheetUserPreferences(scope, false);
      if (stylesheetUserPreferences != null) {
        final Map preferences = preferenceValueFunction.getPreferences(stylesheetUserPreferences);
        compositeMap.putAll(preferences);
      }
    }

    return compositeMap;
  }
예제 #2
0
 /**
  * Puts all values of this record into the given Map.
  *
  * @param map The Map to populate.
  * @return the given map.
  */
 <M extends Map<String, String>> M putIn(final M map) {
   if (mapping == null) {
     return map;
   }
   for (final Entry<String, Integer> entry : mapping.entrySet()) {
     final int col = entry.getValue().intValue();
     if (col < values.length) {
       map.put(entry.getKey(), values[col]);
     }
   }
   return map;
 }
 private <V, M extends Map<Object, V>> M _convertValues(
     Map<?, ?> map, Class<M> mapClass, Class<V> valueClass) {
   M newMap = (M) MapUtils.of(mapClass);
   for (Entry entry : map.entrySet()) {
     try {
       V v = convert(entry.getValue(), valueClass);
       newMap.put(entry.getKey(), v);
     } catch (ConversionException ex) {
     }
   }
   return newMap;
 }
 private <K, V, M extends Map<K, V>> M _convertContents(
     Map<?, ?> map, Class<M> mapClass, Class<K> keyClass, Class<V> valueClass) {
   M newMap = (M) MapUtils.of(mapClass);
   for (Entry entry : map.entrySet()) {
     try {
       K k = convert(entry.getKey(), keyClass);
       V v = convert(entry.getValue(), valueClass);
       newMap.put(k, v);
     } catch (ConversionException ex) {
     }
   }
   setCacheKeyValueTypes(map, keyClass, valueClass);
   return newMap;
 }
예제 #5
0
 private static <M extends Map<Key, V>, V> M transposeMapHelp(Map<Key, V> orig, M neww) {
   for (Map.Entry<Key, V> entry : orig.entrySet()) {
     Key k0 = entry.getKey();
     Key k =
         new Key(
             k0.getColumnQualifier(),
             k0.getColumnFamily(),
             k0.getRow(),
             k0.getColumnVisibilityParsed(),
             k0.getTimestamp());
     neww.put(k, entry.getValue());
   }
   return neww;
 }
 /**
  * Convert a Map to a Map with keys and values of the desired types. If it is a Map and it is of
  * the same Map, Key and Value types it is returned as is, otherwise a new Map is created with all
  * keys and values converted to the desired type. If it is a String representation of a Map it is
  * converted to a Map.
  *
  * @param <K> The key type.
  * @param <V> The value type.
  * @param <M> The Map type.
  * @param obj The Object to be converted.
  * @param mapType The desired Map type.
  * @param keyType The desired key type.
  * @param valueType The desired value type.
  * @param emptyIfNull Whether or not an empty Map should be returned if the Map could not be
  *     converted.
  * @return The converted Collection.
  */
 public <K, V, M extends Map<K, V>> M convertToMap(
     Object obj, Class<M> mapType, Class<K> keyType, Class<V> valueType, boolean emptyIfNull) {
   if (obj != null) {
     if (obj instanceof Map) {
       // If it's already a collection
       Map map = (Map) obj;
       Pair<Class<?>, Class<?>> cachedType = getCachedKeyValueTypes(map);
       // Look up the cached types if available, if they can be downcast to the
       // requested types do so and update cache
       if (cachedType != null
           && keyType.isAssignableFrom(cachedType.getKey())
           && valueType.isAssignableFrom(cachedType.getValue())) {
         setCacheKeyValueTypes(map, keyType, valueType);
         return (M) map;
       } else {
         return _convertContents(map, mapType, keyType, valueType);
       }
     } else {
       try {
         // If it's a string representation of key-value pairs
         M m = MapUtils.of(mapType);
         String string = convert(obj, String.class);
         if (string.startsWith("{") && string.endsWith("}")) {
           string = string.substring(1, string.length() - 1);
         }
         String[] entries = TextUtils.splitOnSpacesAndCommas(string);
         for (String str : entries) {
           String[] split = str.split("=");
           if (split.length == 2) {
             try {
               K k = convert(split[0], keyType);
               V v = convert(split[1], valueType);
               m.put(k, v);
             } catch (ConversionException ex) {
             }
           }
         }
         if (!m.isEmpty()) {
           return m;
         }
       } catch (ConversionException ex) {
       }
     }
   }
   if (emptyIfNull) {
     return MapUtils.of(mapType);
   }
   return null;
 }