//----------------------------------------------------------------------- /** * Constructor that wraps (not copies). * * @param map the map to decorate, must not be null * @param keyPredicate the predicate to validate the keys, null means no check * @param valuePredicate the predicate to validate to values, null means no check * @throws IllegalArgumentException if the map is null */ protected internal PredicatedMap(java.util.Map<Object, Object> map, Predicate keyPredicate, Predicate valuePredicate) : base(map) { this.keyPredicate = keyPredicate; this.valuePredicate = valuePredicate; java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = map.entrySet().iterator(); while (it.hasNext()) { java.util.MapNS.Entry<Object, Object> entry = (java.util.MapNS.Entry<Object, Object>)it.next(); Object key = entry.getKey(); Object value = entry.getValue(); validate(key, value); } }
public override void addRenderingHints(java.util.Map hints) { Iterator iterator = hints.entrySet().iterator(); while (iterator.hasNext()) { java.util.Map.Entry entry = (java.util.Map.Entry)iterator.next(); setRenderingHint((java.awt.RenderingHints.Key)entry.getKey(), entry.getValue()); } }
public override void putAll(java.util.Map<Object, Object> mapToCopy) { java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = mapToCopy.entrySet().iterator(); while (it.hasNext()) { java.util.MapNS.Entry<Object, Object> entry = (java.util.MapNS.Entry<Object, Object>)it.next(); Object key = entry.getKey(); Object value = entry.getValue(); validate(key, value); } map.putAll(mapToCopy); }
/** * Returns the array of providers which meet the user supplied set of * filters. The filter must be supplied in one of two formats: * <nl> * <li> CRYPTO_SERVICE_NAME.ALGORITHM_OR_TYPE</li> * <p/> * for example: "MessageDigest.SHA" The value associated with the key must * be an empty string. <li> CRYPTO_SERVICE_NAME.ALGORITHM_OR_TYPE</li> * ATTR_NAME:ATTR_VALUE * <p/> * for example: "Signature.MD2withRSA KeySize:512" where "KeySize:512" is * the value of the filter map entry. * </nl> * * @param filter * case-insensitive filter. * @return the providers which meet the user supplied string filter {@code * filter}. A {@code null} value signifies that none of the * installed providers meets the filter specification. * @throws InvalidParameterException * if an unusable filter is supplied. * @throws NullPointerException * if {@code filter} is {@code null}. */ public static Provider[] getProviders(java.util.Map<String, String> filter) { lock (lockJ) { if (filter == null) { throw new java.lang.NullPointerException("The filter is null"); //$NON-NLS-1$ } if (filter.isEmpty()) { return null; } java.util.List<Provider> result = Services.getProvidersList(); java.util.Set<java.util.MapNS.Entry<String, String>> keys = filter.entrySet(); java.util.MapNS.Entry<String, String> entry; for (java.util.Iterator<java.util.MapNS.Entry<String, String>> it = keys.iterator(); it.hasNext(); ) { entry = it.next(); String key = entry.getKey(); String val = entry.getValue(); String attribute = null; int i = key.indexOf(' '); int j = key.indexOf('.'); if (j == -1) { throw new InvalidParameterException("The filter is not in the required format"); //$NON-NLS-1$ } if (i == -1) { // <crypto_service>.<algorithm_or_type> if (val.length() != 0) { throw new InvalidParameterException("The filter is not in the required format"); //$NON-NLS-1$ } } else { // <crypto_service>.<algorithm_or_type> <attribute_name> if (val.length() == 0) { throw new InvalidParameterException("The filter is not in the required format"); //$NON-NLS-1$ } attribute = key.substring(i + 1); if (attribute.trim().length() == 0) { throw new InvalidParameterException("The filter is not in the required format"); //$NON-NLS-1$ } key = key.substring(0, i); } String serv = key.substring(0, j); String alg = key.substring(j + 1); if (serv.length() == 0 || alg.length() == 0) { throw new InvalidParameterException("The filter is not in the required format"); //$NON-NLS-1$ } Provider p; for (int k = 0; k < result.size(); k++) { try { p = result.get(k); } catch (java.lang.IndexOutOfBoundsException e) { break; } if (!p.implementsAlg(serv, alg, attribute, val)) { result.remove(p); k--; } } } if (result.size() > 0) { return result.toArray(new Provider[result.size()]); } return null; } }
/** * Create a new Transformer that uses the input object as a key to find the * transformer to call. * <p> * The Map consists of object keys and Transformer values. A transformer * is called if the input object equals the key. If there is no match, the * default transformer is called. The default transformer is set in the map * using a null key. If no default is set, null will be returned in a default case. * * @see org.apache.commons.collections.functors.SwitchTransformer * * @param objectsAndTransformers a map of objects to transformers * @return the transformer * @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if the map is empty * @throws IllegalArgumentException if any transformer in the map is null */ public static Transformer switchMapTransformer(java.util.Map<Object, Object> objectsAndTransformers) { Transformer[] trs = null; Predicate[] preds = null; if (objectsAndTransformers == null) { throw new java.lang.IllegalArgumentException("The object and transformer map must not be null"); } Transformer def = (Transformer)objectsAndTransformers.remove(null); int size = objectsAndTransformers.size(); trs = new Transformer[size]; preds = new Predicate[size]; int i = 0; for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = objectsAndTransformers.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = (java.util.MapNS.Entry<Object, Object>)it.next(); preds[i] = EqualPredicate.getInstance(entry.getKey()); trs[i] = (Transformer)entry.getValue(); i++; } return switchTransformer(preds, trs, def); }
/** * Create a new Closure that uses the input object as a key to find the * closure to call. * <p> * The Map consists of object keys and Closure values. A closure * is called if the input object equals the key. If there is no match, the * default closure is called. The default closure is set in the map * using a null key. * * @see org.apache.commons.collections.functors.SwitchClosure * * @param objectsAndClosures a map of objects to closures * @return the closure * @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if the map is empty * @throws IllegalArgumentException if any closure in the map is null */ public static Closure switchMapClosure(java.util.Map<Object, Object> objectsAndClosures) { Closure[] trs = null; Predicate[] preds = null; if (objectsAndClosures == null) { throw new java.lang.IllegalArgumentException("The object and closure map must not be null"); } Closure def = (Closure)objectsAndClosures.remove(null); int size = objectsAndClosures.size(); trs = new Closure[size]; preds = new Predicate[size]; int i = 0; for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = objectsAndClosures.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = it.next(); preds[i] = EqualPredicate.getInstance(entry.getKey()); trs[i] = (Closure)entry.getValue(); i++; } return switchClosure(preds, trs, def); }
/** * Transforms a map. * <p> * The transformer itself may throw an exception if necessary. * * @param map the map to transform * @throws the transformed object */ protected virtual java.util.Map<Object, Object> transformMap(java.util.Map<Object, Object> map) { if (map.isEmpty()) { return map; } java.util.Map<Object, Object> result = new LinkedMap(map.size()); for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = map.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = it.next(); result.put(transformKey(entry.getKey()), transformValue(entry.getValue())); } return result; }
/** * Applies a given set of attributes to the given range of the string. * * @param attributes * the set of attributes that will be applied to this string. * @param start * the start of the range where the attribute will be applied. * @param end * the end of the range where the attribute will be applied. * @throws IllegalArgumentException * if {@code start < 0}, {@code end} is greater than the length * of this string, or if {@code start >= end}. */ public void addAttributes( java.util.Map<AttributedCharacterIteratorNS.Attribute, System.Object> attributes, int start, int end) { java.util.Iterator<java.util.MapNS.Entry<AttributedCharacterIteratorNS.Attribute,System.Object>> it = attributes.entrySet().iterator(); while (it.hasNext()) { java.util.MapNS.Entry<AttributedCharacterIteratorNS.Attribute, System.Object> entry = it.next(); addAttribute(entry.getKey(), entry.getValue(), start, end); } }
/** * Create a new Transformer that calls one of the transformers depending * on the predicates. * <p> * The Map consists of Predicate keys and Transformer values. A transformer * is called if its matching predicate returns true. Each predicate is evaluated * until one returns true. If no predicates evaluate to true, the default * transformer is called. The default transformer is set in the map with a * null key. The ordering is that of the iterator() method on the entryset * collection of the map. * * @param predicatesAndTransformers a map of predicates to transformers * @return the <code>switch</code> transformer * @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if any transformer in the map is null * @throws ClassCastException if the map elements are of the wrong type */ public static Transformer getInstance(java.util.Map<Object, Object> predicatesAndTransformers) { Transformer[] transformers = null; Predicate[] preds = null; if (predicatesAndTransformers == null) { throw new java.lang.IllegalArgumentException("The predicate and transformer map must not be null"); } if (predicatesAndTransformers.size() == 0) { return ConstantTransformer.NULL_INSTANCE; } // convert to array like this to guarantee iterator() ordering Transformer defaultTransformer = (Transformer)predicatesAndTransformers.remove(null); int size = predicatesAndTransformers.size(); if (size == 0) { return (defaultTransformer == null ? ConstantTransformer.NULL_INSTANCE : defaultTransformer); } transformers = new Transformer[size]; preds = new Predicate[size]; int i = 0; for (java.util.Iterator<Object> it = (java.util.Iterator<Object>)predicatesAndTransformers.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = (java.util.MapNS.Entry<Object, Object>)it.next(); preds[i] = (Predicate)entry.getKey(); transformers[i] = (Transformer)entry.getValue(); i++; } return new SwitchTransformer(preds, transformers, defaultTransformer); }
/** * Creates an {@code AttributedString} from the given text and the * attributes. The whole text has the given attributes applied. * * @param value * the text to take as base for this attributed string. * @param attributes * the attributes that the text is associated with. * @throws IllegalArgumentException * if the length of {@code value} is 0 but the size of {@code * attributes} is greater than 0. * @throws NullPointerException * if {@code value} is {@code null}. */ public AttributedString(System.String value, java.util.Map<AttributedCharacterIteratorNS.Attribute, System.Object> attributes) { if (value == null) { throw new java.lang.NullPointerException(); } if (value.Length == 0 && !attributes.isEmpty()) { // text.0B=Cannot add attributes to empty string throw new java.lang.IllegalArgumentException("Cannot add attributes to empty string"); //$NON-NLS-1$ } text = value; attributeMap = new java.util.HashMap<AttributedCharacterIteratorNS.Attribute, java.util.List<IAC_Range>>();//(attributes.size() * 4 / 3) + 1); java.util.Iterator<java.util.MapNS.Entry<AttributedCharacterIteratorNS.Attribute,System.Object>> it = attributes.entrySet().iterator(); while (it.hasNext()) { java.util.MapNS.Entry<AttributedCharacterIteratorNS.Attribute, System.Object> entry = it.next(); java.util.ArrayList<IAC_Range> ranges = new java.util.ArrayList<IAC_Range>(1); ranges.add(new IAC_Range(0, text.Length, entry.getValue())); attributeMap.put((AttributedCharacterIteratorNS.Attribute) entry .getKey(), ranges); } }
/** * Implementation providing functionality for {@link #debugPrint} and for * {@link #verbosePrint}. This prints the given map with nice line breaks. * If the debug flag is true, it additionally prints the type of the object * value. If the contents of a map include the map itself, then the text * <em>(this Map)</em> is printed out. If the contents include a * parent container of the map, the the text <em>(ancestor[i] Map)</em> is * printed, where i actually indicates the number of levels which must be * traversed in the sequential list of ancestors (e.g. father, grandfather, * great-grandfather, etc). * * @param out the stream to print to * @param label the label to be used, may be <code>null</code>. * If <code>null</code>, the label is not output. * It typically represents the name of the property in a bean or similar. * @param map the map to print, may be <code>null</code>. * If <code>null</code>, the text 'null' is output * @param lineage a stack consisting of any maps in which the previous * argument is contained. This is checked to avoid infinite recursion when * printing the output * @param debug flag indicating whether type names should be output. * @throws NullPointerException if the stream is <code>null</code> */ private static void verbosePrintInternal( java.lang.PrintStream outJ, Object label, java.util.Map<Object, Object> map, ArrayStack lineage, bool debug) { printIndent(outJ, lineage.size()); if (map == null) { if (label != null) { outJ.print(label); outJ.print(" = "); } outJ.println("null"); return; } if (label != null) { outJ.print(label); outJ.println(" = "); } printIndent(outJ, lineage.size()); outJ.println("{"); lineage.push(map); for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = map.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = it.next(); Object childKey = entry.getKey(); Object childValue = entry.getValue(); if (childValue is java.util.Map<Object, Object> && !lineage.contains(childValue)) { verbosePrintInternal( outJ, (childKey == null ? "null" : childKey), (java.util.Map<Object, Object>)childValue, lineage, debug); } else { printIndent(outJ, lineage.size()); outJ.print(childKey); outJ.print(" = "); int lineageIndex = lineage.indexOf(childValue); if (lineageIndex == -1) { outJ.print(childValue); } else if (lineage.size() - 1 == lineageIndex) { outJ.print("(this Map)"); } else { outJ.print( "(ancestor[" + (lineage.size() - 1 - lineageIndex - 1) + "] Map)"); } if (debug && childValue != null) { outJ.print(' '); outJ.println(childValue.getClass().getName()); } else { outJ.println(); } } } lineage.pop(); printIndent(outJ, lineage.size()); outJ.println(debug ? "} " + map.getClass().getName() : "}"); }
// Conversion methods //------------------------------------------------------------------------- /** * Gets a new Properties object initialised with the values from a Map. * A null input will return an empty properties object. * * @param map the map to convert to a Properties object, may not be null * @return the properties object */ public static java.util.Properties toProperties(java.util.Map<Object, Object> map) { java.util.Properties answer = new java.util.Properties(); if (map != null) { for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> iter = map.entrySet().iterator(); iter.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = iter.next(); Object key = entry.getKey(); Object value = entry.getValue(); answer.put(key, value); } } return answer; }
// Misc //----------------------------------------------------------------------- /** * Inverts the supplied map returning a new HashMap such that the keys of * the input are swapped with the values. * <p> * This operation assumes that the inverse mapping is well defined. * If the input map had multiple entries with the same value mapped to * different keys, the returned map will map one of those keys to the * value, but the exact key which will be mapped is undefined. * * @param map the map to invert, may not be null * @return a new HashMap containing the inverted data * @throws NullPointerException if the map is null */ public static java.util.Map<Object, Object> invertMap(java.util.Map<Object, Object> map) { java.util.HashMap<Object, Object> outJ = new java.util.HashMap<Object, Object>(map.size()); for (java.util.Iterator<java.util.MapNS.Entry<Object, Object>> it = map.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = it.next(); outJ.put(entry.getValue(), entry.getKey()); } return outJ; }
/** * Create a new Closure that calls one of the closures depending * on the predicates. * <p> * The Map consists of Predicate keys and Closure values. A closure * is called if its matching predicate returns true. Each predicate is evaluated * until one returns true. If no predicates evaluate to true, the default * closure is called. The default closure is set in the map with a * null key. The ordering is that of the iterator() method on the entryset * collection of the map. * * @param predicatesAndClosures a map of predicates to closures * @return the <code>switch</code> closure * @throws IllegalArgumentException if the map is null * @throws IllegalArgumentException if any closure in the map is null * @throws ClassCastException if the map elements are of the wrong type */ public static Closure getInstance(java.util.Map<Object, Object> predicatesAndClosures) { Closure[] closures = null; Predicate[] preds = null; if (predicatesAndClosures == null) { throw new java.lang.IllegalArgumentException("The predicate and closure map must not be null"); } if (predicatesAndClosures.size() == 0) { return NOPClosure.INSTANCE; } // convert to array like this to guarantee iterator() ordering Closure defaultClosure = (Closure)predicatesAndClosures.remove(null); int size = predicatesAndClosures.size(); if (size == 0) { return (defaultClosure == null ? NOPClosure.INSTANCE : defaultClosure); } closures = new Closure[size]; preds = new Predicate[size]; int i = 0; for (java.util.Iterator<Object> it = (java.util.Iterator<Object>)predicatesAndClosures.entrySet().iterator(); it.hasNext(); ) { java.util.MapNS.Entry<Object, Object> entry = (java.util.MapNS.Entry<Object, Object>)it.next(); preds[i] = (Predicate)entry.getKey(); closures[i] = (Closure)entry.getValue(); i++; } return new SwitchClosure(preds, closures, defaultClosure); }