/**
  * 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);
 }
Exemple #2
0
		private static string[] finishSplit(java.util.List<string> list, string input, int
			 begin, int maxSize, int limit)
		{
			// Add trailing text.
			if (begin < input.Length)
			{
				list.add(Sharpen.StringHelper.Substring(input, begin));
			}
			else
			{
				if (limit != 0)
				{
					// No point adding the empty string if limit == 0, just to remove it below.
					list.add(string.Empty);
				}
			}
			// Remove all trailing empty matches in the limit == 0 case.
			if (limit == 0)
			{
				int i = list.size() - 1;
				while (i >= 0 && string.IsNullOrEmpty(list.get(i)))
				{
					list.remove(i);
					i--;
				}
			}
			// Convert to an array.
			return list.toArray(new string[list.size()]);
		}
 /**
  * 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);
 }
 /**
  * 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);
 }
 /**
  * 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);
 }