/[classpath]/classpath/java/lang/System.java
ViewVC logotype

Diff of /classpath/java/lang/System.java

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.38.2.10 by gnu_andrew, Sun Apr 3 00:09:10 2005 UTC revision 1.38.2.11 by gnu_andrew, Mon Apr 4 00:24:48 2005 UTC
# Line 44  import gnu.classpath.VMStackWalker; Line 44  import gnu.classpath.VMStackWalker;
44    
45  import java.io.InputStream;  import java.io.InputStream;
46  import java.io.PrintStream;  import java.io.PrintStream;
47    import java.util.AbstractCollection;
48  import java.util.Collection;  import java.util.Collection;
49    import java.util.Collections;
50  import java.util.HashMap;  import java.util.HashMap;
51  import java.util.Iterator;  import java.util.Iterator;
52  import java.util.List;  import java.util.List;
# Line 494  public final class System Line 496  public final class System
496      if (environmentMap == null)      if (environmentMap == null)
497        {        {
498          List<String> environ = VMSystem.environ();          List<String> environ = VMSystem.environ();
499          Map<String,String> variables = new HashMap();          Map<String,String> variables = new EnvironmentMap();
500          for (String pair : environ)          for (String pair : environ)
501            {            {
502              String[] parts;              String[] parts = pair.split("=");
   
             parts = pair.split("=");  
503              variables.put(parts[0], parts[1]);              variables.put(parts[0], parts[1]);
504            }            }
505          environmentMap = new HashMap<String,String>(variables)          environmentMap = Collections.unmodifiableMap(variables);
           {  
             /**  
              * Blocks the removal of all mappings from the map.  
              *  
              * @throws UnsupportedOperationException as mappings  
              *         cannot be removed from this map.  
              */  
             public void clear()  
             {  
               throw new  
               UnsupportedOperationException("This map does not " +  
                                             "allow the removal of mappings.");  
             }  
               
             /**  
              * Blocks queries containing a null key or one which is not  
              * of type <code>String</code>.  All other queries  
              * are forwarded to the superclass.  
              *  
              * @param key the key to look for in the map.  
              * @return true if the key exists in the map.  
              * @throws NullPointerException if the specified key is null.  
              * @throws ClassCastException if the specified key is not a String.  
              */  
             public boolean containsKey(Object key)  
             {  
               if (key == null)  
               {  
                 throw new  
                 NullPointerException("This map does not support null keys.");  
               }  
               if (!(key instanceof String))  
               {  
                 throw new  
                 ClassCastException("This map only supports Strings.");  
               }  
               return super.containsKey(key);  
             }  
               
             /**  
              * Blocks queries using a null or non-<code>String</code> value.  
              * All other queries are forwarded to the superclass.  
              *  
              * @param value the value to look for in the map.  
              * @return true if the value exists in the map.  
              * @throws NullPointerException if the specified value is null.  
              * @throws ClassCastException if the specified value is not a String.  
              */  
             public boolean containsValue(Object value)  
             {  
               if (value == null)  
               {  
                 throw new  
                 NullPointerException("This map does not support null values.");  
               }  
               if (!(value instanceof String))  
               {  
                 throw new  
                 ClassCastException("This map only supports Strings.");  
               }  
               return super.containsValue(value);  
             }  
   
             /**  
              * Returns a set view of the map entries, with the same  
              * provisions as for the underlying map.  
              *  
              * @return a set containing the map entries.  
              */  
             public Set<Map.Entry<String,String>> entrySet()  
             {  
               return new EnvironmentSet<Map.Entry<String,String>>(super.entrySet());  
             }  
   
             /**  
              * Blocks queries containing a null key.  All other  
              * queries are passed on to the superclass.  
              *  
              * @param key the key to retrieve the value for.  
              * @return the value associated with the given key.  
              * @throws NullPointerException if the specified key is null.  
              * @throws ClassCastException if the specified key is not a String.  
              */  
             public String get(Object key)  
             {  
               if (key == null)  
               {  
                 throw new  
                 NullPointerException("This map does not support null keys.");  
               }  
               return super.get(key);  
             }  
   
             /**  
              * Returns a set view of the keys, with the same  
              * provisions as for the underlying map.  
              *  
              * @return a set containing the keys.  
              */  
             public Set<String> keySet()  
             {  
               return new EnvironmentSet<String>(super.keySet());  
             }  
   
             /**  
              * Blocks the addition of mappings to the map.  
              *  
              * @param key the key to add.  
              * @param value the value to add.  
              * @return the previous value of the specified key, or  
              *         null if there was no previous value.  
              * @throws UnsupportedOperationException as this map can't  
              *         be extended.  
              */  
             public String put(String key, String value)  
             {  
               throw new  
               UnsupportedOperationException("This map can not be extended.");  
             }  
   
             /**  
              * Blocks the addition of mappings to the map.  
              *  
              * @param m the map from which to take the new entries.  
              * @throws UnsupportedOperationException as this map can't  
              *         be extended.  
              */  
             public void putAll(Map<? extends String, ? extends String> m)  
             {  
               throw new  
               UnsupportedOperationException("This map can not be extended.");  
             }  
               
             /**  
              * Blocks the removal of entries from the map.  
              *  
              * @param key the key of the entry to remove.  
              * @return the removed value.  
              * @throws UnsupportedOperationException as entries can't  
              *         be removed from this map.  
              */  
             public String remove(Object key)  
             {  
               throw new  
               UnsupportedOperationException("Entries can not be removed " +  
                                             "from this map.");  
             }  
   
             /**  
              * Returns a collection view of the values, with the same  
              * provisions as for the underlying map.  
              *  
              * @return a collection containing the values.  
              */  
             public Collection<String> values()  
             {  
               return new EnvironmentCollection<String>(super.values());  
             }  
   
           };  
506        }        }
507      return environmentMap;      return environmentMap;
508    }    }
# Line 781  public final class System Line 621  public final class System
621    
622    
623    /**    /**
624     * This is an specialised <code>Collection</code>, providing     * This is a specialised <code>Collection</code>, providing
625     * the necessary provisions for the collections used by the     * the necessary provisions for the collections used by the
626     * environment variable map.  Namely, it prevents     * environment variable map.  Namely, it prevents
627     * modifications and the use of queries with null     * querying anything but <code>String</code>s.
    * or non-<code>String</code> values.  
628     *     *
629     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
630     */     */
631    private static class EnvironmentCollection<E>    private static class EnvironmentCollection
632      implements Collection<E>      extends AbstractCollection<String>
633    {    {
634    
635      /**      /**
636       * The wrapped collection.       * The wrapped collection.
637       */       */
638      protected Collection<E> c;      protected Collection<String> c;
639    
640      /**      /**
641       * Constructs a new environment collection, which       * Constructs a new environment collection, which
# Line 805  public final class System Line 644  public final class System
644       * @param coll the collection to use as a base for       * @param coll the collection to use as a base for
645       *             this collection.       *             this collection.
646       */       */
647      public EnvironmentCollection(Collection<? extends E> coll)      public EnvironmentCollection(Collection<String> coll)
     {  
       c = (Collection<E>) coll;  
     }  
       
     /**  
      * Blocks the addition of elements to the collection.  
      *  
      * @param entry the new entry.  
      * @throws UnsupportedOperationException as the underlying  
      *         map does not support new elements.  
                  */  
     public boolean add(E entry)  
     {  
       throw new  
         UnsupportedOperationException("The addition of elements is " +  
                                       "not supported.");  
     }  
       
     /**  
      * Blocks the addition of a collection of elements to  
      * the collection.  
      *  
      * @param c the collection of elements to add.  
      * @throws UnsupportedOperationException as the underlying  
      *         map does not support new elements.  
      */  
     public boolean addAll(Collection<? extends E> c)  
648      {      {
649        throw new        c = coll;
         UnsupportedOperationException("The addition of elements is " +  
                                       "not supported.");  
650      }      }
651            
     /**  
      * Blocks the removal of all elements.  
      *  
      * @throws UnsupportedOperationException as elements  
      *         cannot be removed from this map.  
      */  
     public void clear()  
     {  
       throw new  
         UnsupportedOperationException("This map does not " +  
                                       "allow the removal of elements.");  
     }  
       
652      /**      /**
653       * Blocks queries containing a null object or an object which       * Blocks queries containing a null object or an object which
654       * isn't of type <code>String</code>.  All other queries       * isn't of type <code>String</code>.  All other queries
# Line 865  public final class System Line 662  public final class System
662      public boolean contains(Object obj)      public boolean contains(Object obj)
663      {      {
664        if (obj == null)        if (obj == null)
         {  
665            throw new            throw new
666              NullPointerException("This collection does not support null values.");              NullPointerException("This collection does not support " +
667          }                                   "null values.");
668        if (!(obj instanceof String))        if (!(obj instanceof String))
         {  
669            throw new            throw new
670              ClassCastException("This collection only supports Strings.");              ClassCastException("This collection only supports Strings.");
         }  
671        return c.contains(obj);        return c.contains(obj);
672      }      }
673            
# Line 893  public final class System Line 687  public final class System
687        for (Object o: coll)        for (Object o: coll)
688          {          {
689            if (o == null)            if (o == null)
             {  
690                throw new                throw new
691                  NullPointerException("This collection does not support null values.");                  NullPointerException("This collection does not support " +
692              }                                       "null values.");
693            if (!(o instanceof String))            if (!(o instanceof String))
             {  
694                throw new                throw new
695                  ClassCastException("This collection only supports Strings.");                  ClassCastException("This collection only supports Strings.");
             }  
696          }          }
697        return c.containsAll(coll);        return c.containsAll(coll);
698      }      }
699    
700      /**      /**
      * This simply calls the same method on the wrapped  
      * collection.  
      *  
      * @return true if the collection is empty.  
      */  
     public boolean isEmpty()  
     {  
       return c.isEmpty();  
     }  
   
     /**  
701       * This returns an iterator over the map elements, with the       * This returns an iterator over the map elements, with the
702       * same provisions as for the collection and underlying map.       * same provisions as for the collection and underlying map.
703       *       *
704       * @return an iterator over the map elements.       * @return an iterator over the map elements.
705       */       */
706      public Iterator<E> iterator()      public Iterator<String> iterator()
707      {      {
708        return new        return c.iterator();
         EnvironmentIterator<E>(c.iterator());  
709      }      }
710            
711      /**      /**
712       * Blocks the removal of elements from the collection.       * Blocks the removal of elements from the collection.
713       *       *
714       * @return true if the removal was sucessful.       * @return true if the removal was sucessful.
715       * @throws UnsupportedOperationException as elements can't       * @throws NullPointerException if the collection is null.
716       *         be removed from this collection.       * @throws NullPointerException if any collection entry is null.
717         * @throws ClassCastException if any collection entry is not a String.
718       */       */
719      public boolean remove(Object key)      public boolean remove(Object key)
720      {      {
721        throw new        if (key == null)
722          UnsupportedOperationException("Elements can not be removed " +            throw new
723                                        "from this collection.");              NullPointerException("This collection does not support " +
724                                     "null values.");
725          if (!(key instanceof String))
726              throw new
727                ClassCastException("This collection only supports Strings.");
728          return c.contains(key);
729      }        }  
730                    
731      /**      /**
732       * Blocks the removal of all elements in the specified       * Blocks the removal of all elements in the specified
733       * collection from the collection.       * collection from the collection.
734       *       *
735       * @param c the collection of elements to remove.       * @param coll the collection of elements to remove.
736       * @return true if the elements were removed.       * @return true if the elements were removed.
737       * @throws UnsupportedOperationException as elements can't       * @throws NullPointerException if the collection is null.
738       *         be removed from this collection.       * @throws NullPointerException if any collection entry is null.
739         * @throws ClassCastException if any collection entry is not a String.
740       */       */
741      public boolean removeAll(Collection<?> c)      public boolean removeAll(Collection<?> coll)
742      {      {
743        throw new        for (Object o: coll)
744          UnsupportedOperationException("Elements can not be removed " +          {
745                                        "from this collection.");            if (o == null)
746                  throw new
747                    NullPointerException("This collection does not support " +
748                                         "null values.");
749              if (!(o instanceof String))
750                throw new
751                  ClassCastException("This collection only supports Strings.");
752            }
753          return c.removeAll(coll);
754      }      }
755            
756      /**      /**
# Line 965  public final class System Line 759  public final class System
759       *       *
760       * @param c the collection of elements to retain.       * @param c the collection of elements to retain.
761       * @return true if the other elements were removed.       * @return true if the other elements were removed.
762       * @throws UnsupportedOperationException as elements can't       * @throws NullPointerException if the collection is null.
763       *         be removed from this collection.       * @throws NullPointerException if any collection entry is null.
764         * @throws ClassCastException if any collection entry is not a String.
765       */       */
766      public boolean retainAll(Collection<?> c)      public boolean retainAll(Collection<?> coll)
767      {      {
768        throw new        for (Object o: coll)
769                    UnsupportedOperationException("Elements can not be removed " +          {
770                                                  "from this collection.");            if (o == null)
771                  throw new
772                    NullPointerException("This collection does not support " +
773                                         "null values.");
774              if (!(o instanceof String))
775                throw new
776                  ClassCastException("This collection only supports Strings.");
777            }
778          return c.containsAll(coll);
779      }      }
780    
781      /**      /**
# Line 986  public final class System Line 789  public final class System
789        return c.size();        return c.size();
790      }      }
791    
792      } // class EnvironmentCollection<String>
793    
794      /**
795       * This is a specialised <code>HashMap</code>, which
796       * prevents the addition or querying of anything other than
797       * <code>String</code> objects.
798       *
799       * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
800       */
801      private static class EnvironmentMap
802        extends HashMap<String,String>
803      {
804        
805      /**      /**
806       * This simply calls the same method on the wrapped       * Cache the entry set.
807       * collection.       */
808        private transient Set<Map.Entry<String,String>> entries;
809    
810        /**
811         * Cache the key set.
812         */
813        private transient Set<String> keys;
814    
815        /**
816         * Cache the value collection.
817         */
818        private transient Collection<String> values;
819    
820        /**
821         * Constructs a new empty <code>EnvironmentMap</code>.
822         */
823        EnvironmentMap()
824        {
825          super();
826        }
827        
828        /**
829         * Blocks queries containing a null key or one which is not
830         * of type <code>String</code>.  All other queries
831         * are forwarded to the superclass.
832         *
833         * @param key the key to look for in the map.
834         * @return true if the key exists in the map.
835         * @throws NullPointerException if the specified key is null.
836         */
837        public boolean containsKey(Object key)
838        {
839          if (key == null)
840            throw new
841              NullPointerException("This map does not support null keys.");
842          if (!(key instanceof String))
843            throw new
844              ClassCastException("This map only allows queries using Strings.");
845          return super.containsKey(key);
846        }
847        
848        /**
849         * Blocks queries using a null or non-<code>String</code> value.
850         * All other queries are forwarded to the superclass.
851       *       *
852       * @return the collection in array form.       * @param value the value to look for in the map.
853         * @return true if the value exists in the map.
854         * @throws NullPointerException if the specified value is null.
855       */       */
856      public Object[] toArray()      public boolean containsValue(Object value)
857      {      {
858        return c.toArray();        if (value == null)
859              throw new
860                NullPointerException("This map does not support null values.");
861          if (!(value instanceof String))
862            throw new
863              ClassCastException("This map only allows queries using Strings.");
864          return super.containsValue(value);
865      }      }
866    
867      /**      /**
868       * This simply calls the same method on the wrapped       * Returns a set view of the map entries, with the same
869       * collection.       * provisions as for the underlying map.
870       *       *
871       * @param a the array to use to type the result.       * @return a set containing the map entries.
      * @return the collection in appropriately-typed  
      *         array form.  
872       */       */
873      public <T> T[] toArray(T[] a)      public Set<Map.Entry<String,String>> entrySet()
874      {      {
875        return c.toArray(a);        if (entries == null)
876            entries = super.entrySet();
877          return entries;
878      }      }
879    
   } // class EnvironmentCollection<E>  
   
   private static class EnvironmentSet<S>  
     extends EnvironmentCollection<S>  
     implements Set<S>  
   {  
   
880      /**      /**
881       * Constructs a new environment set, which       * Blocks queries containing a null or non-<code>String</code> key.
882       * wraps the elements of the supplied set.       * All other queries are passed on to the superclass.
883       *       *
884       * @param set the set to use as a base for       * @param key the key to retrieve the value for.
885       *             this set.       * @return the value associated with the given key.
886         * @throws NullPointerException if the specified key is null.
887         * @throws ClassCastException if the specified key is not a String.
888       */       */
889      public EnvironmentSet(Set<? extends S> set)      public String get(Object key)
890      {      {
891        super(set);        if (key == null)
892            throw new
893              NullPointerException("This map does not support null keys.");
894          if (!(key instanceof String))
895            throw new
896              ClassCastException("This map only allows queries using Strings.");
897          return super.get(key);
898      }      }
899        
900      /**      /**
901       * This simply calls the same method on the wrapped       * Returns a set view of the keys, with the same
902       * collection.       * provisions as for the underlying map.
903       *       *
904       * @param obj the object to compare with.       * @return a set containing the keys.
      * @return true if the two objects are equal.  
905       */       */
906      public boolean equals(Object obj)      public Set<String> keySet()
907      {      {
908        return c.equals(obj);        if (keys == null)
909            keys = new EnvironmentSet(super.keySet());
910          return keys;
911      }      }
912        
913      /**      /**
914       * This simply calls the same method on the wrapped       * Removes a key-value pair from the map.  The queried key may not
915       * collection.       * be null or of a type other than a <code>String</code>.
916       *       *
917       * @return the hashcode of the collection.       * @param key the key of the entry to remove.
918         * @return the removed value.
919         * @throws NullPointerException if the specified key is null.
920         * @throws ClassCastException if the specified key is not a String.
921         */
922        public String remove(Object key)
923        {
924          if (key == null)
925            throw new
926              NullPointerException("This map does not support null keys.");
927          if (!(key instanceof String))
928            throw new
929              ClassCastException("This map only allows queries using Strings.");
930          return super.remove(key);
931        }
932        
933        /**
934         * Returns a collection view of the values, with the same
935         * provisions as for the underlying map.
936         *
937         * @return a collection containing the values.
938       */       */
939      public int hashCode()      public Collection<String> values()
940      {      {
941        return c.hashCode();        if (values == null)
942            values = new EnvironmentCollection(super.values());
943          return values;
944      }      }
945        
946      }
947    
   } // class EnvironmentSet<S>  
   
   /* The class below is a clone of UnmodifiableIterator  
    * from java.util.Collections. Please keep it in sync */  
948    /**    /**
949     * The implementation of the various iterator methods in the     * This is a specialised <code>Set</code>, providing
950     * unmodifiable classes.     * the necessary provisions for the collections used by the
951       * environment variable map.  Namely, it prevents
952       * modifications and the use of queries with null
953       * or non-<code>String</code> values.
954     *     *
955     * @author Eric Blake (ebb9@email.byu.edu)     * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
956     */     */
957    private static class EnvironmentIterator<T>    private static class EnvironmentSet
958      implements Iterator<T>      extends EnvironmentCollection
959        implements Set<String>
960    {    {
     /**  
      * The wrapped iterator.  
      */  
     private final Iterator<T> i;  
   
     /**  
      * Only trusted code creates a wrapper.  
      * @param i the wrapped iterator  
      */  
     EnvironmentIterator(Iterator<T> i)  
     {  
       this.i = i;  
     }  
961    
962      /**      /**
963       * Obtains the next element in the underlying collection.       * Constructs a new environment set, which
964         * wraps the elements of the supplied set.
965       *       *
966       * @return the next element in the collection.       * @param set the set to use as a base for
967       * @throws NoSuchElementException if there are no more elements.       *             this set.
968       */       */
969      public T next()      public EnvironmentSet(Set<String> set)
970      {      {
971        return i.next();        super(set);
972      }      }
973    
974      /**      /**
975       * Tests whether there are still elements to be retrieved from the       * This simply calls the same method on the wrapped
976       * underlying collection by <code>next()</code>.  When this method       * collection.
      * returns <code>true</code>, an exception will not be thrown on calling  
      * <code>next()</code>.  
977       *       *
978       * @return <code>true</code> if there is at least one more element in the underlying       * @param obj the object to compare with.
979       *         collection.       * @return true if the two objects are equal.
980       */       */
981      public boolean hasNext()      public boolean equals(Object obj)
982      {      {
983        return i.hasNext();        return c.equals(obj);
984      }      }
985    
986      /**      /**
987       * Blocks the removal of elements from the underlying collection by the       * This simply calls the same method on the wrapped
988       * iterator.       * collection.
989       *       *
990       * @throws UnsupportedOperationException as an unmodifiable collection       * @return the hashcode of the collection.
      *         does not support the removal of elements by its iterator.  
991       */       */
992      public void remove()      public int hashCode()
993      {      {
994        throw new UnsupportedOperationException("The removal of elements is " +        return c.hashCode();
                                               "not supported.");  
995      }      }
996    } // class EnvironmentIterator<I>  
997      } // class EnvironmentSet<String>
998    
999  } // class System  } // class System

Legend:
Removed from v.1.38.2.10  
changed lines
  Added in v.1.38.2.11

savannah-hackers-public@gnu.org
ViewVC Help
Powered by ViewVC 1.1.26