Пример #1
0
        /**
         * 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 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));
        }
 /**
  * Constructor copying elements from another map.
  *
  * @param map  the map to copy, must be size 1
  * @throws NullPointerException if the map is null
  * @throws IllegalArgumentException if the size is not 1
  */
 public SingletonMap(java.util.Map <Object, Object> map)
 {
     if (map.size() != 1)
     {
         throw new java.lang.IllegalArgumentException("The map size must be 1");
     }
     java.util.MapNS.Entry <Object, Object> entry = (java.util.MapNS.Entry <Object, Object>)map.entrySet().iterator().next();
     this.key   = entry.getKey();
     this.value = entry.getValue();
 }
        //-----------------------------------------------------------------------

        /**
         * Write the map out using a custom routine.
         * @param out  the output stream
         * @throws IOException
         */
        protected virtual void doWriteObject(java.io.ObjectOutputStream outJ)
        {//throws IOException {
            outJ.writeInt(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 = (java.util.MapNS.Entry <Object, Object>)it.next();
                outJ.writeObject(entry.getKey());
                outJ.writeInt(((MutableInteger)entry.getValue()).value);
            }
        }
        /**
         * Factory method to create a transforming map that will transform
         * existing contents of the specified map.
         * <p>
         * If there are any elements already in the map being decorated, they
         * will be transformed by this method.
         * Constrast this with {@link #decorate}.
         *
         * @param map  the map to decorate, must not be null
         * @param keyTransformer  the transformer to use for key conversion, null means no transformation
         * @param valueTransformer  the transformer to use for value conversion, null means no transformation
         * @throws IllegalArgumentException if map is null
         * @since Commons Collections 3.2
         */
        public static java.util.Map <Object, Object> decorateTransform(java.util.Map <Object, Object> map, Transformer keyTransformer, Transformer valueTransformer)
        {
            TransformedMap decorated = new TransformedMap(map, keyTransformer, valueTransformer);

            if (map.size() > 0)
            {
                java.util.Map <Object, Object> transformed = decorated.transformMap(map);
                decorated.clear();
                decorated.getMap().putAll(transformed);  // avoids double transformation
            }
            return(decorated);
        }
 /**
  * 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);
 }
        /**
         * Puts the values from the specified map into this map.
         * <p>
         * The map must be of size 0 or size 1.
         * If it is size 1, the key must match the key of this map otherwise an
         * IllegalArgumentException is thrown.
         *
         * @param map  the map to add, must be size 0 or 1, and the key must match
         * @throws NullPointerException if the map is null
         * @throws IllegalArgumentException if the key does not match
         */
        public virtual void putAll(java.util.Map <Object, Object> map)
        {
            switch (map.size())
            {
            case 0:
                return;

            case 1:
                java.util.MapNS.Entry <Object, Object> entry = (java.util.MapNS.Entry <Object, Object>)map.entrySet().iterator().next();
                put(entry.getKey(), entry.getValue());
                return;

            default:
                throw new java.lang.IllegalArgumentException("The map size must be 0 or 1");
            }
        }
 /**
  * Compares this map with another.
  *
  * @param obj  the object to compare to
  * @return true if equal
  */
 public override bool Equals(Object obj)
 {
     if (obj == this)
     {
         return(true);
     }
     if (obj is java.util.Map <Object, Object> == false)
     {
         return(false);
     }
     java.util.Map <Object, Object> other = (java.util.Map <Object, Object>)obj;
     if (other.size() != 1)
     {
         return(false);
     }
     java.util.MapNS.Entry <Object, Object> entry = (java.util.MapNS.Entry <Object, Object>)other.entrySet().iterator().next();
     return(isEqualKey(entry.getKey()) && isEqualValue(entry.getValue()));
 }
        /**
         * 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));
        }
Пример #10
0
        /**
         * Starts a new process based on the current state of this process builder.
         *
         * @return the new {@code Process} instance.
         * @throws NullPointerException
         *             if any of the elements of {@link #command()} is {@code null}.
         * @throws IndexOutOfBoundsException
         *             if {@link #command()} is empty.
         * @throws SecurityException
         *             if {@link SecurityManager#checkExec(String)} doesn't allow
         *             process creation.
         * @throws IOException
         *             if an I/O error happens.
         */
        public Process start()//throws IOException {
        {
            if (commandJ.isEmpty())
            {
                throw new IndexOutOfBoundsException();
            }
            String[] cmdArray = new String[commandJ.size()];
            for (int i = 0; i < cmdArray.Length; i++)
            {
                if ((cmdArray[i] = commandJ.get(i)) == null)
                {
                    throw new NullPointerException();
                }
            }
            String[] envArray = new String[environmentJ.size()];
            int      i2       = 0;

            /*
             * foreach (Map.Entry<String, String> entry in environmentJ.entrySet()) {
             * envArray[i2++] = entry.getKey() + "=" + entry.getValue(); //$NON-NLS-1$
             * }
             */
            java.util.Iterator <String> it = environmentJ.keySet().iterator();
            while (it.hasNext())
            {
                String key   = it.next();
                String value = environmentJ.get(key);
                envArray[i2++] = key + "=" + value;
            }

            java.lang.Process process = Runtime.getRuntime().exec(cmdArray, envArray,
                                                                  directoryJ);

            // TODO implement support for redirectErrorStream
            return(process);
        }
Пример #11
0
        /**
         * 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));
        }
 //-----------------------------------------------------------------------
 public int size()
 {
     return(map.size());
 }
 /**
  * Constructor that copies the input map creating an independent copy.
  * <p>
  * This method performs different behaviour depending on whether the map
  * specified is a MultiMap or not. If a MultiMap is specified, each internal
  * collection is also cloned. If the specified map only implements Map, then
  * the values are not cloned.
  * <p>
  * NOTE: From Commons Collections 3.1 this method correctly copies a MultiMap
  * to form a truly independent new map.
  * NOTE: From Commons Collections 3.2 this method delegates to the newly
  * added putAll(Map) override method.
  *
  * @param mapToCopy  a Map to copy
  */
 public MultiHashMap(java.util.Map <Object, Object> mapToCopy)
     : base((int)(mapToCopy.size() * 1.4f))
 {
     putAll(mapToCopy);
 }
Пример #14
0
        // Basic object methods
        // ----------------------------------------------------------------------

        /**
         * Compare the specified object with this list for equality.  This
         * implementation uses exactly the code that is used to define the
         * list equals function in the documentation for the
         * <code>Map.equals</code> method.
         *
         * @param o  the object to be compared to this list
         * @return true if the two maps are equal
         */
        public override bool Equals(Object o)
        {
            // Simple tests that require no synchronization
            if (o == this)
            {
                return(true);
            }
            else if (!(o is java.util.Map <Object, Object>))
            {
                return(false);
            }
            java.util.Map <Object, Object> mo = (java.util.Map <Object, Object>)o;

            // Compare the two maps for equality
            if (fast)
            {
                if (mo.size() != map.size())
                {
                    return(false);
                }
                java.util.Iterator <java.util.MapNS.Entry <Object, Object> > i = map.entrySet().iterator();
                while (i.hasNext())
                {
                    java.util.MapNS.Entry <Object, Object> e = i.next();
                    Object key   = e.getKey();
                    Object value = e.getValue();
                    if (value == null)
                    {
                        if (!(mo.get(key) == null && mo.containsKey(key)))
                        {
                            return(false);
                        }
                    }
                    else
                    {
                        if (!value.equals(mo.get(key)))
                        {
                            return(false);
                        }
                    }
                }
                return(true);
            }
            else
            {
                lock (map)
                {
                    if (mo.size() != map.size())
                    {
                        return(false);
                    }
                    java.util.Iterator <java.util.MapNS.Entry <Object, Object> > i = map.entrySet().iterator();
                    while (i.hasNext())
                    {
                        java.util.MapNS.Entry <Object, Object> e = i.next();
                        Object key   = e.getKey();
                        Object value = e.getValue();
                        if (value == null)
                        {
                            if (!(mo.get(key) == null && mo.containsKey(key)))
                            {
                                return(false);
                            }
                        }
                        else
                        {
                            if (!value.equals(mo.get(key)))
                            {
                                return(false);
                            }
                        }
                    }
                    return(true);
                }
            }
        }
 /**
  * Constructor copying elements from another map.
  * <p/>
  * The maximum size is set from the map's size.
  *
  * @param map  the map to copy
  * @param scanUntilRemovable  scan until a removeable entry is found, default false
  * @throws NullPointerException if the map is null
  * @throws IllegalArgumentException if the map is empty
  * @since Commons Collections 3.1
  */
 public LRUMap(java.util.Map <Object, Object> map, bool scanUntilRemovable) :
     this(map.size(), DEFAULT_LOAD_FACTOR, scanUntilRemovable)
 {
     putAll(map);
 }
Пример #16
0
        static internal java.util.Map DeriveStyle(java.util.Map attribs, FontStyle style, bool createNew)
        {
            java.util.Map newAttribs;
            if (createNew)
            {
                newAttribs = new java.util.Hashtable(attribs.size());
                java.util.Iterator it = attribs.keySet().iterator();
                while (it.hasNext())
                {
                    object key   = it.next();
                    object value = attribs.get(key);
                    if (value != null)
                    {
                        newAttribs.put(key, value);
                    }
                }
            }
            else
            {
                newAttribs = attribs;
            }

            //Bold
            if ((style & FontStyle.Bold) == FontStyle.Bold)
            {
                newAttribs.put(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
            }
            else
            {
                newAttribs.remove(TextAttribute.WEIGHT);
            }

            //Italic
            if ((style & FontStyle.Italic) == FontStyle.Italic)
            {
                newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
            }
            else
            {
                newAttribs.put(TextAttribute.POSTURE, TextAttribute.POSTURE_REGULAR);
            }

            //Underline
            if ((style & FontStyle.Underline) == FontStyle.Underline)
            {
                newAttribs.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
            }
            else
            {
                newAttribs.remove(TextAttribute.UNDERLINE);
            }

            //Strikeout
            if ((style & FontStyle.Strikeout) == FontStyle.Strikeout)
            {
                newAttribs.put(TextAttribute.STRIKETHROUGH, TextAttribute.STRIKETHROUGH_ON);
            }
            else
            {
                newAttribs.remove(TextAttribute.STRIKETHROUGH);
            }

            return(newAttribs);
        }
 public virtual int size()
 {
     return(map.size());
 }
Пример #18
0
 private void setPermissions(java.util.List <android.content.pm.PermissionInfo> permList
                             )
 {
     mGroupLabelCache = new java.util.HashMap <string, java.lang.CharSequence>();
     //add the default label so that uncategorized permissions can go here
     mGroupLabelCache.put(mDefaultGrpName, java.lang.CharSequenceProxy.Wrap(mDefaultGrpLabel
                                                                            ));
     // Map containing group names and a list of permissions under that group
     // categorized as dangerous
     mDangerousMap = new java.util.HashMap <string, string>();
     // Map containing group names and a list of permissions under that group
     // categorized as normal
     mNormalMap = new java.util.HashMap <string, string>();
     // Additional structures needed to ensure that permissions are unique under
     // each group
     java.util.Map <string, java.util.List <android.content.pm.PermissionInfo> > dangerousMap
         = new java.util.HashMap <string, java.util.List <android.content.pm.PermissionInfo
                                                          > >();
     java.util.Map <string, java.util.List <android.content.pm.PermissionInfo> > normalMap
         = new java.util.HashMap <string, java.util.List <android.content.pm.PermissionInfo
                                                          > >();
     android.widget.AppSecurityPermissions.PermissionInfoComparator permComparator = new
                                                                                     android.widget.AppSecurityPermissions.PermissionInfoComparator(mPm);
     if (permList != null)
     {
         // First pass to group permissions
         foreach (android.content.pm.PermissionInfo pInfo in Sharpen.IterableProxy.Create(
                      permList))
         {
             if (localLOGV)
             {
                 android.util.Log.i(TAG, "Processing permission:" + pInfo.name);
             }
             if (!isDisplayablePermission(pInfo))
             {
                 if (localLOGV)
                 {
                     android.util.Log.i(TAG, "Permission:" + pInfo.name + " is not displayable");
                 }
                 continue;
             }
             java.util.Map <string, java.util.List <android.content.pm.PermissionInfo> > permInfoMap
                 = (pInfo.protectionLevel == android.content.pm.PermissionInfo.PROTECTION_DANGEROUS
                    ) ? dangerousMap : normalMap;
             string grpName = (pInfo.group == null) ? mDefaultGrpName : pInfo.group;
             if (localLOGV)
             {
                 android.util.Log.i(TAG, "Permission:" + pInfo.name + " belongs to group:" + grpName
                                    );
             }
             java.util.List <android.content.pm.PermissionInfo> grpPermsList = permInfoMap.get(
                 grpName);
             if (grpPermsList == null)
             {
                 grpPermsList = new java.util.ArrayList <android.content.pm.PermissionInfo>();
                 permInfoMap.put(grpName, grpPermsList);
                 grpPermsList.add(pInfo);
             }
             else
             {
                 int idx = java.util.Collections.binarySearch(grpPermsList, pInfo, permComparator);
                 if (localLOGV)
                 {
                     android.util.Log.i(TAG, "idx=" + idx + ", list.size=" + grpPermsList.size());
                 }
                 if (idx < 0)
                 {
                     idx = -idx - 1;
                     grpPermsList.add(idx, pInfo);
                 }
             }
         }
         // Second pass to actually form the descriptions
         // Look at dangerous permissions first
         aggregateGroupDescs(dangerousMap, mDangerousMap);
         aggregateGroupDescs(normalMap, mNormalMap);
     }
     mCurrentState = android.widget.AppSecurityPermissions.State.NO_PERMS;
     if (mDangerousMap.size() > 0)
     {
         mCurrentState = (mNormalMap.size() > 0) ? android.widget.AppSecurityPermissions.State
                         .BOTH : android.widget.AppSecurityPermissions.State.DANGEROUS_ONLY;
     }
     else
     {
         if (mNormalMap.size() > 0)
         {
             mCurrentState = android.widget.AppSecurityPermissions.State.NORMAL_ONLY;
         }
     }
     if (localLOGV)
     {
         android.util.Log.i(TAG, "mCurrentState=" + mCurrentState);
     }
     showPermissions();
 }