/**
         * 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));
        }
Exemplo n.º 2
0
        public override Object[] toArray <Object>(Object[] array)
        {
            Object[] result = array;
            if (array.Length > 0)
            {
                // we must create a new array to handle multi-threaded situations
                // where another thread could access data before we decorate it
                //result = (Object[]) Array.newInstance(array.getClass().getComponentType(), 0);
                result = new Object[array.Length];
            }
            Object [] input = collection.toArray(result);
            for (int i = 0; i < result.Length; i++)
            {
                java.util.MapNS.Entry <object, object> makeUnmodifiable = (java.util.MapNS.Entry <object, object>)input[i];
                UnmodifiableEntry ue = new UnmodifiableEntry(makeUnmodifiable);
                result[i] = ((Object)(object)ue);
            }

            // check to see if result should be returned straight
            if (result.Length > array.Length)
            {
                return(result);
            }

            // copy back into input array to fulfil the method contract
            java.lang.SystemJ.arraycopy(result, 0, array, 0, result.Length);
            if (array.Length > result.Length)
            {
                array[result.Length] = default(Object);
            }
            return(array);
        }
Exemplo n.º 3
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));
        }
        //-----------------------------------------------------------------------

        /**
         * Returns the Map as a string.
         *
         * @return the Map as a String
         */
        public override String ToString()
        {
            if (isEmpty())
            {
                return("{}");
            }
            java.lang.StringBuffer buf = new java.lang.StringBuffer();
            buf.append('{');
            bool first = true;

            java.util.Iterator <Object> it = 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();
                if (first)
                {
                    first = false;
                }
                else
                {
                    buf.append(", ");
                }
                buf.append(key == this ? "(this Map)" : key);
                buf.append('=');
                buf.append(value == this ? "(this Map)" : value);
            }
            buf.append('}');
            return(buf.toString());
        }
Exemplo n.º 5
0
 public void remove()
 {
     if (lastReturned == null)
     {
         throw new java.lang.IllegalStateException();
     }
     if (root.fast)
     {
         lock (root)
         {
             if (expected != root.map)
             {
                 throw new java.util.ConcurrentModificationException();
             }
             root.remove(lastReturned.getKey());
             lastReturned = null;
             expected     = root.map;
         }
     }
     else
     {
         iterator.remove();
         lastReturned = null;
     }
 }
        public bool ignoreGlobalAllowedByRobots(java.util.List <String> userAgents, String url)
        {
            String path = getPath(url);

            java.util.MapNS.Entry <Match, Match> matches = computeMatchPriorities(userAgents, path, true);
            return(allowVerdict(matches.getKey(), matches.getValue()));
        }
 /**
  * Constructor that wraps (not copies).
  *
  * @param entry  the <code>Map.Entry</code> to decorate, must not be null
  * @throws IllegalArgumentException if the collection is null
  */
 public AbstractMapEntryDecorator(java.util.MapNS.Entry <Object, Object> entry)
 {
     if (entry == null)
     {
         throw new java.lang.IllegalArgumentException("Map Entry must not be null");
     }
     this.entry = entry;
 }
 public override void putAll(java.util.Map <Object, Object> 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();
         put(entry.getKey(), entry.getValue());
     }
 }
 /**
  * Constructor.
  *
  * @param parent  the parent bag
  */
 public BagIterator(AbstractMapBag parent)
 {
     this.parent        = parent;
     this.entryIterator = parent.map.entrySet().iterator();
     this.current       = null;
     this.mods          = parent.modCount;
     this.canRemove     = false;
 }
Exemplo n.º 10
0
 public virtual void remove()
 {
     if (last == null)
     {
         throw new java.lang.IllegalStateException();
     }
     root.remove(last.getKey());
     last = null;
 }
Exemplo n.º 11
0
 /**
  *  Adds all the mappings in the specified map to this map, replacing any
  *  mappings that already exist (as per {@link Map#putAll(Map)}).  The order
  *  in which the entries are added is determined by the iterator returned
  *  from {@link Map#entrySet()} for the specified map.
  *
  *  @param t the mappings that should be added to this map.
  *
  *  @throws NullPointerException if <code>t</code> is <code>null</code>
  */
 public virtual void putAll(java.util.Map <Object, Object> t)
 {
     java.util.Iterator <java.util.MapNS.Entry <Object, Object> > iter = t.entrySet().iterator();
     while (iter.hasNext())
     {
         java.util.MapNS.Entry <Object, Object> entry = iter.next();
         put(entry.getKey(), entry.getValue());
     }
 }
Exemplo n.º 12
0
 public Object next()
 {
     if (expected != root.map)
     {
         throw new java.util.ConcurrentModificationException();
     }
     lastReturned = (java.util.MapNS.Entry <Object, Object>)iterator.next();
     return(view.iteratorNext(lastReturned));
 }
Exemplo n.º 13
0
 protected virtual java.util.MapNS.Entry <Object, Object> nextEntry()
 {
     if (!hasNext())
     {
         throw new java.util.NoSuchElementException();
     }
     last = (java.util.MapNS.Entry <Object, Object>)current.remove(current.size() - 1);
     return(last);
 }
 /**
  * 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();
 }
        //-----------------------------------------------------------------------

        /**
         * Removes the last returned key from the underlying <code>Map</code>.
         * <p>
         * This method can be called once per call to <code>next()</code>.
         *
         * @throws UnsupportedOperationException if remove is not supported by the map
         * @throws IllegalStateException if <code>next()</code> has not yet been called
         * @throws IllegalStateException if <code>remove()</code> has already been called
         *  since the last call to <code>next()</code>
         */
        public void remove()
        {
            if (canRemove == false)
            {
                throw new java.lang.IllegalStateException("Iterator remove() can only be called once after next()");
            }
            iterator.remove();
            last      = null;
            canRemove = false;
        }
        //-----------------------------------------------------------------------

        /**
         * 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);
            }
        }
Exemplo n.º 17
0
 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);
 }
 /**
  * Clear the map.
  * <p>
  * This clears each collection in the map, and so may be slow.
  */
 public override void clear()
 {
     // For gc, clear each list in the map
     java.util.Set <java.util.MapNS.Entry <Object, Object> >      pairs         = base.entrySet();
     java.util.Iterator <java.util.MapNS.Entry <Object, Object> > pairsIterator = pairs.iterator();
     while (pairsIterator.hasNext())
     {
         java.util.MapNS.Entry <Object, Object> keyValuePair = pairsIterator.next();
         java.util.Collection <Object>          coll         = (java.util.Collection <Object>)keyValuePair.getValue();
         coll.clear();
     }
     base.clear();
 }
        /**
         * Gets a hash code for the Bag compatible with the definition of equals.
         * The hash code is defined as the sum total of a hash code for each element.
         * The per element hash code is defined as
         * <code>(e==null ? 0 : e.hashCode()) ^ noOccurances)</code>.
         * This hash code is compatible with the Set interface.
         *
         * @return the hash code of the Bag
         */
        public override int GetHashCode()
        {
            int total = 0;

            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();
                Object         element = entry.getKey();
                MutableInteger count   = (MutableInteger)entry.getValue();
                total += (element == null ? 0 : element.GetHashCode()) ^ count.value;
            }
            return(total);
        }
Exemplo n.º 20
0
 public override Object[] toArray <Object>(Object[] arr)
 {
     // special implementation to handle disappearing entries
     java.util.ArrayList <Object> list = new java.util.ArrayList <Object>();
     java.util.Iterator <Object>  it   = (java.util.Iterator <Object>) this.iterator();
     while (it.hasNext())
     {
         java.util.MapNS.Entry <Object, Object> e = (java.util.MapNS.Entry <Object, Object>)it.next(); //? right???
         object o  = new DefaultMapEntry(e.getKey(), e.getValue());
         Object o2 = (Object)o;
         list.add(o2);
     }
     return(list.toArray(arr));
 }
        public void remove()
        {
            if (canRemove == false)
            {
                throw new java.lang.IllegalStateException("Iterator remove() can only be called once after next()");
            }
            // store value as remove may change the entry in the decorator (eg.TreeMap)
            Object value = last.getValue();

            iterator.remove();
            parent.maps[1].remove(value);
            last      = null;
            canRemove = false;
        }
 /**
  * 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);
 }
        //-----------------------------------------------------------------------

        /**
         * Clones the map creating an independent copy.
         * <p>
         * The clone will shallow clone the collections as well as the map.
         *
         * @return the cloned map
         */
        public Object clone()
        {
            MultiHashMap cloned = (MultiHashMap)base.MemberwiseClone();

            // clone each Collection container
            for (java.util.Iterator <java.util.MapNS.Entry <Object, Object> > it = cloned.entrySet().iterator(); it.hasNext();)
            {
                java.util.MapNS.Entry <Object, Object> entry   = it.next();
                java.util.Collection <Object>          coll    = (java.util.Collection <Object>)entry.getValue();
                java.util.Collection <Object>          newColl = createCollection(coll);
                entry.setValue(newColl);
            }
            return(cloned);
        }
 public Object next()
 {
     if (parent.modCount != mods)
     {
         throw new java.util.ConcurrentModificationException();
     }
     if (itemCount == 0)
     {
         current   = (java.util.MapNS.Entry <Object, Object>)entryIterator.next();
         itemCount = ((MutableInteger)current.getValue()).value;
     }
     canRemove = true;
     itemCount--;
     return(current.getKey());
 }
Exemplo n.º 25
0
        // Basics
        //-----------------------------------------------------------------------

        /**
         * Compares this Map Entry with another Map Entry.
         * <p>
         * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
         *
         * @param obj  the object to compare to
         * @return true if equal key and value
         */
        public override bool Equals(Object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (obj is java.util.MapNS.Entry <Object, Object> == false)
            {
                return(false);
            }
            java.util.MapNS.Entry <Object, Object> other = (java.util.MapNS.Entry <Object, Object>)obj;
            return
                ((getKey() == null ? other.getKey() == null : getKey().equals(other.getKey())) &&
                 (getValue() == null ? other.getValue() == null : getValue().equals(other.getValue())));
        }
Exemplo n.º 26
0
        //-----------------------------------------------------------------------

        /**
         * 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);
            }
        }
Exemplo n.º 27
0
            public override bool Equals(Object obj)
            {
                if (obj == this)
                {
                    return(true);
                }
                if (obj is java.util.MapNS.Entry <Object, Object> == false)
                {
                    return(false);
                }

                java.util.MapNS.Entry <Object, Object> e2 = (java.util.MapNS.Entry <Object, Object>)obj;
                return(
                    (key == null ? e2.getKey() == null : key.equals(e2.getKey())) &&
                    (value == null ? e2.getValue() == null : value.equals(e2.getValue())));
            }
        /**
         * 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");
            }
        }
Exemplo n.º 29
0
        /**
         * Compares this <code>Map.Entry</code> with another <code>Map.Entry</code>.
         * <p>
         * Implemented per API documentation of {@link java.util.Map.Entry#equals(Object)}
         *
         * @param obj  the object to compare to
         * @return true if equal key and value
         */
        public bool equals(Object obj)
        {
            if (obj == this)
            {
                return(true);
            }
            if (obj is java.util.MapNS.Entry <Object, Object> == false)
            {
                return(false);
            }
            java.util.MapNS.Entry <Object, Object> other = (java.util.MapNS.Entry <Object, Object>)obj;
            Object value = getValue();

            return
                ((key == null ? other.getKey() == null : key.equals(other.getKey())) &&
                 (value == null ? other.getValue() == null : value.equals(other.getValue())));
        }
Exemplo n.º 30
0
            public override bool contains(Object obj)
            {
                java.util.MapNS.Entry <Object, Object> entry = (java.util.MapNS.Entry <Object, Object>)obj;
                int hash = root.getHash(entry.getKey());

                lock (root.locks[hash])
                {
                    for (Node n = root.buckets[hash]; n != null; n = n.next)
                    {
                        if (n.equals(entry))
                        {
                            return(true);
                        }
                    }
                }
                return(false);
            }
 //-----------------------------------------------------------------------
 /**
  * Resets the state of the iterator.
  */
 public void reset()
 {
     iterator = (java.util.Iterator<Object>)map.entrySet().iterator();
     last = null;
     canRemove = false;
 }
 public virtual void reset()
 {
     iterator = new java.util.ArrayList<Object>((java.util.Collection<Object>)parent.entrySet()).listIterator();
     last = null;
 }
 public virtual void remove()
 {
     iterator.remove();
     parent.remove(last.getKey());
     last = null;
 }
 public virtual Object previous()
 {
     last = (java.util.MapNS.Entry<Object, Object>)iterator.previous();
     return last.getKey();
 }
 /**
  * Gets the next <em>key</em> from the <code>Map</code>.
  *
  * @return the next key in the iteration
  * @throws java.util.NoSuchElementException if the iteration is finished
  */
 public Object next()
 {
     last = (java.util.MapNS.Entry<Object, Object>)iterator.next();
     canRemove = true;
     return last.getKey();
 }
 //-----------------------------------------------------------------------
 /**
  * Removes the last returned key from the underlying <code>Map</code>.
  * <p>
  * This method can be called once per call to <code>next()</code>.
  *
  * @throws UnsupportedOperationException if remove is not supported by the map
  * @throws IllegalStateException if <code>next()</code> has not yet been called
  * @throws IllegalStateException if <code>remove()</code> has already been called
  *  since the last call to <code>next()</code>
  */
 public void remove()
 {
     if (canRemove == false)
     {
         throw new java.lang.IllegalStateException("Iterator remove() can only be called once after next()");
     }
     iterator.remove();
     last = null;
     canRemove = false;
 }