libjava/
[official-gcc.git] / libjava / classpath / java / lang / System.java
blob9fd6bfe12cd941cee69f6df4b11cdb4298d755a5
1 /* System.java -- useful methods to interface with the system
2 Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2007
3 Free Software Foundation, Inc.
5 This file is part of GNU Classpath.
7 GNU Classpath is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
10 any later version.
12 GNU Classpath is distributed in the hope that it will be useful, but
13 WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Classpath; see the file COPYING. If not, write to the
19 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
20 02110-1301 USA.
22 Linking this library statically or dynamically with other modules is
23 making a combined work based on this library. Thus, the terms and
24 conditions of the GNU General Public License cover the whole
25 combination.
27 As a special exception, the copyright holders of this library give you
28 permission to link this library with independent modules to produce an
29 executable, regardless of the license terms of these independent
30 modules, and to copy and distribute the resulting executable under
31 terms of your choice, provided that you also meet, for each linked
32 independent module, the terms and conditions of the license of that
33 module. An independent module is a module which is not derived from
34 or based on this library. If you modify this library, you may extend
35 this exception to your version of the library, but you are not
36 obligated to do so. If you do not wish to do so, delete this
37 exception statement from your version. */
40 package java.lang;
42 import gnu.classpath.SystemProperties;
43 import gnu.classpath.VMStackWalker;
45 import java.io.IOException;
46 import java.io.InputStream;
47 import java.io.PrintStream;
48 import java.nio.channels.Channel;
49 import java.nio.channels.spi.SelectorProvider;
50 import java.util.AbstractCollection;
51 import java.util.Collection;
52 import java.util.Collections;
53 import java.util.HashMap;
54 import java.util.Iterator;
55 import java.util.List;
56 import java.util.Map;
57 import java.util.Set;
58 import java.util.Properties;
59 import java.util.PropertyPermission;
61 /**
62 * System represents system-wide resources; things that represent the
63 * general environment. As such, all methods are static.
65 * @author John Keiser
66 * @author Eric Blake (ebb9@email.byu.edu)
67 * @since 1.0
68 * @status still missing 1.4 functionality
70 public final class System
72 // WARNING: System is a CORE class in the bootstrap cycle. See the comments
73 // in vm/reference/java/lang/Runtime for implications of this fact.
75 /**
76 * The standard InputStream. This is assigned at startup and starts its
77 * life perfectly valid. Although it is marked final, you can change it
78 * using {@link #setIn(InputStream)} through some hefty VM magic.
80 * <p>This corresponds to the C stdin and C++ cin variables, which
81 * typically input from the keyboard, but may be used to pipe input from
82 * other processes or files. That should all be transparent to you,
83 * however.
85 public static final InputStream in = VMSystem.makeStandardInputStream();
87 /**
88 * The standard output PrintStream. This is assigned at startup and
89 * starts its life perfectly valid. Although it is marked final, you can
90 * change it using {@link #setOut(PrintStream)} through some hefty VM magic.
92 * <p>This corresponds to the C stdout and C++ cout variables, which
93 * typically output normal messages to the screen, but may be used to pipe
94 * output to other processes or files. That should all be transparent to
95 * you, however.
97 public static final PrintStream out = VMSystem.makeStandardOutputStream();
99 /**
100 * The standard output PrintStream. This is assigned at startup and
101 * starts its life perfectly valid. Although it is marked final, you can
102 * change it using {@link #setErr(PrintStream)} through some hefty VM magic.
104 * <p>This corresponds to the C stderr and C++ cerr variables, which
105 * typically output error messages to the screen, but may be used to pipe
106 * output to other processes or files. That should all be transparent to
107 * you, however.
109 public static final PrintStream err = VMSystem.makeStandardErrorStream();
112 * A cached copy of the environment variable map.
114 private static Map<String,String> environmentMap;
117 * This class is uninstantiable.
119 private System()
124 * Set {@link #in} to a new InputStream. This uses some VM magic to change
125 * a "final" variable, so naturally there is a security check,
126 * <code>RuntimePermission("setIO")</code>.
128 * @param in the new InputStream
129 * @throws SecurityException if permission is denied
130 * @since 1.1
132 public static void setIn(InputStream in)
134 SecurityManager sm = SecurityManager.current; // Be thread-safe.
135 if (sm != null)
136 sm.checkPermission(new RuntimePermission("setIO"));
138 VMSystem.setIn(in);
142 * Set {@link #out} to a new PrintStream. This uses some VM magic to change
143 * a "final" variable, so naturally there is a security check,
144 * <code>RuntimePermission("setIO")</code>.
146 * @param out the new PrintStream
147 * @throws SecurityException if permission is denied
148 * @since 1.1
150 public static void setOut(PrintStream out)
152 SecurityManager sm = SecurityManager.current; // Be thread-safe.
153 if (sm != null)
154 sm.checkPermission(new RuntimePermission("setIO"));
155 VMSystem.setOut(out);
159 * Set {@link #err} to a new PrintStream. This uses some VM magic to change
160 * a "final" variable, so naturally there is a security check,
161 * <code>RuntimePermission("setIO")</code>.
163 * @param err the new PrintStream
164 * @throws SecurityException if permission is denied
165 * @since 1.1
167 public static void setErr(PrintStream err)
169 SecurityManager sm = SecurityManager.current; // Be thread-safe.
170 if (sm != null)
171 sm.checkPermission(new RuntimePermission("setIO"));
172 VMSystem.setErr(err);
176 * Set the current SecurityManager. If a security manager already exists,
177 * then <code>RuntimePermission("setSecurityManager")</code> is checked
178 * first. Since this permission is denied by the default security manager,
179 * setting the security manager is often an irreversible action.
181 * <STRONG>Spec Note:</STRONG> Don't ask me, I didn't write it. It looks
182 * pretty vulnerable; whoever gets to the gate first gets to set the policy.
183 * There is probably some way to set the original security manager as a
184 * command line argument to the VM, but I don't know it.
186 * @param sm the new SecurityManager
187 * @throws SecurityException if permission is denied
189 public static synchronized void setSecurityManager(SecurityManager sm)
191 // Implementation note: the field lives in SecurityManager because of
192 // bootstrap initialization issues. This method is synchronized so that
193 // no other thread changes it to null before this thread makes the change.
194 if (SecurityManager.current != null)
195 SecurityManager.current.checkPermission
196 (new RuntimePermission("setSecurityManager"));
198 // java.security.Security's class initialiser loads and parses the
199 // policy files. If it hasn't been run already it will be run
200 // during the first permission check. That initialisation will
201 // fail if a very restrictive security manager is in force, so we
202 // preload it here.
203 if (SecurityManager.current == null)
207 Class.forName("java.security.Security");
209 catch (ClassNotFoundException e)
214 SecurityManager.current = sm;
218 * Get the current SecurityManager. If the SecurityManager has not been
219 * set yet, then this method returns null.
221 * @return the current SecurityManager, or null
223 public static SecurityManager getSecurityManager()
225 return SecurityManager.current;
229 * Get the current time, measured in the number of milliseconds from the
230 * beginning of Jan. 1, 1970. This is gathered from the system clock, with
231 * any attendant incorrectness (it may be timezone dependent).
233 * @return the current time
234 * @see java.util.Date
236 public static long currentTimeMillis()
238 return VMSystem.currentTimeMillis();
241 /**
242 * <p>
243 * Returns the current value of a nanosecond-precise system timer.
244 * The value of the timer is an offset relative to some arbitrary fixed
245 * time, which may be in the future (making the value negative). This
246 * method is useful for timing events where nanosecond precision is
247 * required. This is achieved by calling this method before and after the
248 * event, and taking the difference betweent the two times:
249 * </p>
250 * <p>
251 * <code>long startTime = System.nanoTime();</code><br />
252 * <code>... <emph>event code</emph> ...</code><br />
253 * <code>long endTime = System.nanoTime();</code><br />
254 * <code>long duration = endTime - startTime;</code><br />
255 * </p>
256 * <p>
257 * Note that the value is only nanosecond-precise, and not accurate; there
258 * is no guarantee that the difference between two values is really a
259 * nanosecond. Also, the value is prone to overflow if the offset
260 * exceeds 2^63.
261 * </p>
263 * @return the time of a system timer in nanoseconds.
264 * @since 1.5
266 public static long nanoTime()
268 return VMSystem.nanoTime();
272 * Copy one array onto another from <code>src[srcStart]</code> ...
273 * <code>src[srcStart+len-1]</code> to <code>dest[destStart]</code> ...
274 * <code>dest[destStart+len-1]</code>. First, the arguments are validated:
275 * neither array may be null, they must be of compatible types, and the
276 * start and length must fit within both arrays. Then the copying starts,
277 * and proceeds through increasing slots. If src and dest are the same
278 * array, this will appear to copy the data to a temporary location first.
279 * An ArrayStoreException in the middle of copying will leave earlier
280 * elements copied, but later elements unchanged.
282 * @param src the array to copy elements from
283 * @param srcStart the starting position in src
284 * @param dest the array to copy elements to
285 * @param destStart the starting position in dest
286 * @param len the number of elements to copy
287 * @throws NullPointerException if src or dest is null
288 * @throws ArrayStoreException if src or dest is not an array, if they are
289 * not compatible array types, or if an incompatible runtime type
290 * is stored in dest
291 * @throws IndexOutOfBoundsException if len is negative, or if the start or
292 * end copy position in either array is out of bounds
294 public static void arraycopy(Object src, int srcStart,
295 Object dest, int destStart, int len)
297 VMSystem.arraycopy(src, srcStart, dest, destStart, len);
301 * Get a hash code computed by the VM for the Object. This hash code will
302 * be the same as Object's hashCode() method. It is usually some
303 * convolution of the pointer to the Object internal to the VM. It
304 * follows standard hash code rules, in that it will remain the same for a
305 * given Object for the lifetime of that Object.
307 * @param o the Object to get the hash code for
308 * @return the VM-dependent hash code for this Object
309 * @since 1.1
311 public static int identityHashCode(Object o)
313 return VMSystem.identityHashCode(o);
317 * Get all the system properties at once. A security check may be performed,
318 * <code>checkPropertiesAccess</code>. Note that a security manager may
319 * allow getting a single property, but not the entire group.
321 * <p>The required properties include:
322 * <dl>
323 * <dt>java.version</dt> <dd>Java version number</dd>
324 * <dt>java.vendor</dt> <dd>Java vendor specific string</dd>
325 * <dt>java.vendor.url</dt> <dd>Java vendor URL</dd>
326 * <dt>java.home</dt> <dd>Java installation directory</dd>
327 * <dt>java.vm.specification.version</dt> <dd>VM Spec version</dd>
328 * <dt>java.vm.specification.vendor</dt> <dd>VM Spec vendor</dd>
329 * <dt>java.vm.specification.name</dt> <dd>VM Spec name</dd>
330 * <dt>java.vm.version</dt> <dd>VM implementation version</dd>
331 * <dt>java.vm.vendor</dt> <dd>VM implementation vendor</dd>
332 * <dt>java.vm.name</dt> <dd>VM implementation name</dd>
333 * <dt>java.specification.version</dt> <dd>Java Runtime Environment version</dd>
334 * <dt>java.specification.vendor</dt> <dd>Java Runtime Environment vendor</dd>
335 * <dt>java.specification.name</dt> <dd>Java Runtime Environment name</dd>
336 * <dt>java.class.version</dt> <dd>Java class version number</dd>
337 * <dt>java.class.path</dt> <dd>Java classpath</dd>
338 * <dt>java.library.path</dt> <dd>Path for finding Java libraries</dd>
339 * <dt>java.io.tmpdir</dt> <dd>Default temp file path</dd>
340 * <dt>java.compiler</dt> <dd>Name of JIT to use</dd>
341 * <dt>java.ext.dirs</dt> <dd>Java extension path</dd>
342 * <dt>os.name</dt> <dd>Operating System Name</dd>
343 * <dt>os.arch</dt> <dd>Operating System Architecture</dd>
344 * <dt>os.version</dt> <dd>Operating System Version</dd>
345 * <dt>file.separator</dt> <dd>File separator ("/" on Unix)</dd>
346 * <dt>path.separator</dt> <dd>Path separator (":" on Unix)</dd>
347 * <dt>line.separator</dt> <dd>Line separator ("\n" on Unix)</dd>
348 * <dt>user.name</dt> <dd>User account name</dd>
349 * <dt>user.home</dt> <dd>User home directory</dd>
350 * <dt>user.dir</dt> <dd>User's current working directory</dd>
351 * </dl>
353 * In addition, gnu defines several other properties, where ? stands for
354 * each character in '0' through '9':
355 * <dl>
356 * <dt>gnu.classpath.home</dt> <dd>Path to the classpath libraries.</dd>
357 * <dt>gnu.classpath.version</dt> <dd>Version of the classpath libraries.</dd>
358 * <dt>gnu.classpath.vm.shortname</dt> <dd>Succinct version of the VM name;
359 * used for finding property files in file system</dd>
360 * <dt>gnu.classpath.home.url</dt> <dd> Base URL; used for finding
361 * property files in file system</dd>
362 * <dt>gnu.cpu.endian</dt> <dd>big or little</dd>
363 * <dt>gnu.java.io.encoding_scheme_alias.iso-8859-?</dt> <dd>8859_?</dd>
364 * <dt>gnu.java.io.encoding_scheme_alias.iso8859_?</dt> <dd>8859_?</dd>
365 * <dt>gnu.java.io.encoding_scheme_alias.iso-latin-_?</dt> <dd>8859_?</dd>
366 * <dt>gnu.java.io.encoding_scheme_alias.latin?</dt> <dd>8859_?</dd>
367 * <dt>gnu.java.io.encoding_scheme_alias.utf-8</dt> <dd>UTF8</dd>
368 * <dt>gnu.java.util.zoneinfo.dir</dt> <dd>Root of zoneinfo tree</dd>
369 * <dt>gnu.javax.print.server</dt> <dd>Hostname of external CUPS server.</dd>
370 * </dl>
372 * @return the system properties, will never be null
373 * @throws SecurityException if permission is denied
375 public static Properties getProperties()
377 SecurityManager sm = SecurityManager.current; // Be thread-safe.
378 if (sm != null)
379 sm.checkPropertiesAccess();
380 return SystemProperties.getProperties();
384 * Set all the system properties at once. A security check may be performed,
385 * <code>checkPropertiesAccess</code>. Note that a security manager may
386 * allow setting a single property, but not the entire group. An argument
387 * of null resets the properties to the startup default.
389 * @param properties the new set of system properties
390 * @throws SecurityException if permission is denied
392 public static void setProperties(Properties properties)
394 SecurityManager sm = SecurityManager.current; // Be thread-safe.
395 if (sm != null)
396 sm.checkPropertiesAccess();
397 SystemProperties.setProperties(properties);
401 * Get a single system property by name. A security check may be performed,
402 * <code>checkPropertyAccess(key)</code>.
404 * @param key the name of the system property to get
405 * @return the property, or null if not found
406 * @throws SecurityException if permission is denied
407 * @throws NullPointerException if key is null
408 * @throws IllegalArgumentException if key is ""
410 public static String getProperty(String key)
412 SecurityManager sm = SecurityManager.current; // Be thread-safe.
413 if (sm != null)
414 sm.checkPropertyAccess(key);
415 if (key.length() == 0)
416 throw new IllegalArgumentException("key can't be empty");
417 return SystemProperties.getProperty(key);
421 * Get a single system property by name. A security check may be performed,
422 * <code>checkPropertyAccess(key)</code>.
424 * @param key the name of the system property to get
425 * @param def the default
426 * @return the property, or def if not found
427 * @throws SecurityException if permission is denied
428 * @throws NullPointerException if key is null
429 * @throws IllegalArgumentException if key is ""
431 public static String getProperty(String key, String def)
433 SecurityManager sm = SecurityManager.current; // Be thread-safe.
434 if (sm != null)
435 sm.checkPropertyAccess(key);
436 // This handles both the null pointer exception and the illegal
437 // argument exception.
438 if (key.length() == 0)
439 throw new IllegalArgumentException("key can't be empty");
440 return SystemProperties.getProperty(key, def);
444 * Set a single system property by name. A security check may be performed,
445 * <code>checkPropertyAccess(key, "write")</code>.
447 * @param key the name of the system property to set
448 * @param value the new value
449 * @return the previous value, or null
450 * @throws SecurityException if permission is denied
451 * @throws NullPointerException if key is null
452 * @throws IllegalArgumentException if key is ""
453 * @since 1.2
455 public static String setProperty(String key, String value)
457 SecurityManager sm = SecurityManager.current; // Be thread-safe.
458 if (sm != null)
459 sm.checkPermission(new PropertyPermission(key, "write"));
460 // This handles both the null pointer exception and the illegal
461 // argument exception.
462 if (key.length() == 0)
463 throw new IllegalArgumentException("key can't be empty");
464 return SystemProperties.setProperty(key, value);
468 * Remove a single system property by name. A security check may be
469 * performed, <code>checkPropertyAccess(key, "write")</code>.
471 * @param key the name of the system property to remove
472 * @return the previous value, or null
473 * @throws SecurityException if permission is denied
474 * @throws NullPointerException if key is null
475 * @throws IllegalArgumentException if key is ""
476 * @since 1.5
478 public static String clearProperty(String key)
480 SecurityManager sm = SecurityManager.current; // Be thread-safe.
481 if (sm != null)
482 sm.checkPermission(new PropertyPermission(key, "write"));
483 // This handles both the null pointer exception and the illegal
484 // argument exception.
485 if (key.length() == 0)
486 throw new IllegalArgumentException("key can't be empty");
487 return SystemProperties.remove(key);
491 * Gets the value of an environment variable.
493 * @param name the name of the environment variable
494 * @return the string value of the variable or null when the
495 * environment variable is not defined.
496 * @throws NullPointerException
497 * @throws SecurityException if permission is denied
498 * @since 1.5
499 * @specnote This method was deprecated in some JDK releases, but
500 * was restored in 1.5.
502 public static String getenv(String name)
504 if (name == null)
505 throw new NullPointerException();
506 SecurityManager sm = SecurityManager.current; // Be thread-safe.
507 if (sm != null)
508 sm.checkPermission(new RuntimePermission("getenv." + name));
509 return VMSystem.getenv(name);
513 * <p>
514 * Returns an unmodifiable view of the system environment variables.
515 * If the underlying system does not support environment variables,
516 * an empty map is returned.
517 * </p>
518 * <p>
519 * The returned map is read-only and does not accept queries using
520 * null keys or values, or those of a type other than <code>String</code>.
521 * Attempts to modify the map will throw an
522 * <code>UnsupportedOperationException</code>, while attempts
523 * to pass in a null value will throw a
524 * <code>NullPointerException</code>. Types other than <code>String</code>
525 * throw a <code>ClassCastException</code>.
526 * </p>
527 * <p>
528 * As the returned map is generated using data from the underlying
529 * platform, it may not comply with the <code>equals()</code>
530 * and <code>hashCode()</code> contracts. It is also likely that
531 * the keys of this map will be case-sensitive.
532 * </p>
533 * <p>
534 * Use of this method may require a security check for the
535 * RuntimePermission "getenv.*".
536 * </p>
538 * @return a map of the system environment variables.
539 * @throws SecurityException if the checkPermission method of
540 * an installed security manager prevents access to
541 * the system environment variables.
542 * @since 1.5
544 public static Map<String, String> getenv()
546 SecurityManager sm = SecurityManager.current; // Be thread-safe.
547 if (sm != null)
548 sm.checkPermission(new RuntimePermission("getenv.*"));
549 if (environmentMap == null)
551 List<String> environ = (List<String>)VMSystem.environ();
552 Map<String,String> variables = new EnvironmentMap();
553 for (String pair : environ)
555 String[] parts = pair.split("=");
556 if (parts.length == 2)
557 variables.put(parts[0], parts[1]);
558 else
559 variables.put(parts[0], "");
561 environmentMap = Collections.unmodifiableMap(variables);
563 return environmentMap;
567 * Terminate the Virtual Machine. This just calls
568 * <code>Runtime.getRuntime().exit(status)</code>, and never returns.
569 * Obviously, a security check is in order, <code>checkExit</code>.
571 * @param status the exit status; by convention non-zero is abnormal
572 * @throws SecurityException if permission is denied
573 * @see Runtime#exit(int)
575 public static void exit(int status)
577 Runtime.getRuntime().exit(status);
581 * Calls the garbage collector. This is only a hint, and it is up to the
582 * implementation what this hint suggests, but it usually causes a
583 * best-effort attempt to reclaim unused memory from discarded objects.
584 * This calls <code>Runtime.getRuntime().gc()</code>.
586 * @see Runtime#gc()
588 public static void gc()
590 Runtime.getRuntime().gc();
594 * Runs object finalization on pending objects. This is only a hint, and
595 * it is up to the implementation what this hint suggests, but it usually
596 * causes a best-effort attempt to run finalizers on all objects ready
597 * to be reclaimed. This calls
598 * <code>Runtime.getRuntime().runFinalization()</code>.
600 * @see Runtime#runFinalization()
602 public static void runFinalization()
604 Runtime.getRuntime().runFinalization();
608 * Tell the Runtime whether to run finalization before exiting the
609 * JVM. This is inherently unsafe in multi-threaded applications,
610 * since it can force initialization on objects which are still in use
611 * by live threads, leading to deadlock; therefore this is disabled by
612 * default. There may be a security check, <code>checkExit(0)</code>. This
613 * calls <code>Runtime.runFinalizersOnExit()</code>.
615 * @param finalizeOnExit whether to run finalizers on exit
616 * @throws SecurityException if permission is denied
617 * @see Runtime#runFinalizersOnExit(boolean)
618 * @since 1.1
619 * @deprecated never rely on finalizers to do a clean, thread-safe,
620 * mop-up from your code
622 public static void runFinalizersOnExit(boolean finalizeOnExit)
624 Runtime.runFinalizersOnExit(finalizeOnExit);
628 * Load a code file using its explicit system-dependent filename. A security
629 * check may be performed, <code>checkLink</code>. This just calls
630 * <code>Runtime.getRuntime().load(filename)</code>.
632 * <p>
633 * The library is loaded using the class loader associated with the
634 * class associated with the invoking method.
636 * @param filename the code file to load
637 * @throws SecurityException if permission is denied
638 * @throws UnsatisfiedLinkError if the file cannot be loaded
639 * @see Runtime#load(String)
641 public static void load(String filename)
643 Runtime.getRuntime().load(filename, VMStackWalker.getCallingClassLoader());
647 * Load a library using its explicit system-dependent filename. A security
648 * check may be performed, <code>checkLink</code>. This just calls
649 * <code>Runtime.getRuntime().load(filename)</code>.
651 * <p>
652 * The library is loaded using the class loader associated with the
653 * class associated with the invoking method.
655 * @param libname the library file to load
656 * @throws SecurityException if permission is denied
657 * @throws UnsatisfiedLinkError if the file cannot be loaded
658 * @see Runtime#load(String)
660 public static void loadLibrary(String libname)
662 Runtime.getRuntime().loadLibrary(libname,
663 VMStackWalker.getCallingClassLoader());
667 * Convert a library name to its platform-specific variant.
669 * @param libname the library name, as used in <code>loadLibrary</code>
670 * @return the platform-specific mangling of the name
671 * @since 1.2
673 public static String mapLibraryName(String libname)
675 return VMRuntime.mapLibraryName(libname);
679 * Returns the inherited channel of the VM.
681 * This wraps the inheritedChannel() call of the system's default
682 * {@link SelectorProvider}.
684 * @return the inherited channel of the VM
686 * @throws IOException If an I/O error occurs
687 * @throws SecurityException If an installed security manager denies access
688 * to RuntimePermission("inheritedChannel")
690 * @since 1.5
692 public static Channel inheritedChannel()
693 throws IOException
695 return SelectorProvider.provider().inheritedChannel();
699 * This is a specialised <code>Collection</code>, providing
700 * the necessary provisions for the collections used by the
701 * environment variable map. Namely, it prevents
702 * querying anything but <code>String</code>s.
704 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
706 private static class EnvironmentCollection
707 extends AbstractCollection<String>
711 * The wrapped collection.
713 protected Collection<String> c;
716 * Constructs a new environment collection, which
717 * wraps the elements of the supplied collection.
719 * @param coll the collection to use as a base for
720 * this collection.
722 public EnvironmentCollection(Collection<String> coll)
724 c = coll;
728 * Blocks queries containing a null object or an object which
729 * isn't of type <code>String</code>. All other queries
730 * are forwarded to the underlying collection.
732 * @param obj the object to look for.
733 * @return true if the object exists in the collection.
734 * @throws NullPointerException if the specified object is null.
735 * @throws ClassCastException if the specified object is not a String.
737 public boolean contains(Object obj)
739 if (obj == null)
740 throw new
741 NullPointerException("This collection does not support " +
742 "null values.");
743 if (!(obj instanceof String))
744 throw new
745 ClassCastException("This collection only supports Strings.");
746 return c.contains(obj);
750 * Blocks queries where the collection contains a null object or
751 * an object which isn't of type <code>String</code>. All other
752 * queries are forwarded to the underlying collection.
754 * @param coll the collection of objects to look for.
755 * @return true if the collection contains all elements in the collection.
756 * @throws NullPointerException if the collection is null.
757 * @throws NullPointerException if any collection entry is null.
758 * @throws ClassCastException if any collection entry is not a String.
760 public boolean containsAll(Collection<?> coll)
762 for (Object o: coll)
764 if (o == null)
765 throw new
766 NullPointerException("This collection does not support " +
767 "null values.");
768 if (!(o instanceof String))
769 throw new
770 ClassCastException("This collection only supports Strings.");
772 return c.containsAll(coll);
776 * This returns an iterator over the map elements, with the
777 * same provisions as for the collection and underlying map.
779 * @return an iterator over the map elements.
781 public Iterator<String> iterator()
783 return c.iterator();
787 * Blocks the removal of elements from the collection.
789 * @return true if the removal was sucessful.
790 * @throws NullPointerException if the collection is null.
791 * @throws NullPointerException if any collection entry is null.
792 * @throws ClassCastException if any collection entry is not a String.
794 public boolean remove(Object key)
796 if (key == null)
797 throw new
798 NullPointerException("This collection does not support " +
799 "null values.");
800 if (!(key instanceof String))
801 throw new
802 ClassCastException("This collection only supports Strings.");
803 return c.contains(key);
807 * Blocks the removal of all elements in the specified
808 * collection from the collection.
810 * @param coll the collection of elements to remove.
811 * @return true if the elements were removed.
812 * @throws NullPointerException if the collection is null.
813 * @throws NullPointerException if any collection entry is null.
814 * @throws ClassCastException if any collection entry is not a String.
816 public boolean removeAll(Collection<?> coll)
818 for (Object o: coll)
820 if (o == null)
821 throw new
822 NullPointerException("This collection does not support " +
823 "null values.");
824 if (!(o instanceof String))
825 throw new
826 ClassCastException("This collection only supports Strings.");
828 return c.removeAll(coll);
832 * Blocks the retention of all elements in the specified
833 * collection from the collection.
835 * @param coll the collection of elements to retain.
836 * @return true if the other elements were removed.
837 * @throws NullPointerException if the collection is null.
838 * @throws NullPointerException if any collection entry is null.
839 * @throws ClassCastException if any collection entry is not a String.
841 public boolean retainAll(Collection<?> coll)
843 for (Object o: coll)
845 if (o == null)
846 throw new
847 NullPointerException("This collection does not support " +
848 "null values.");
849 if (!(o instanceof String))
850 throw new
851 ClassCastException("This collection only supports Strings.");
853 return c.containsAll(coll);
857 * This simply calls the same method on the wrapped
858 * collection.
860 * @return the size of the underlying collection.
862 public int size()
864 return c.size();
867 } // class EnvironmentCollection<String>
870 * This is a specialised <code>HashMap</code>, which
871 * prevents the addition or querying of anything other than
872 * <code>String</code> objects.
874 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
876 static class EnvironmentMap
877 extends HashMap<String,String>
881 * Cache the entry set.
883 private transient Set<Map.Entry<String,String>> entries;
886 * Cache the key set.
888 private transient Set<String> keys;
891 * Cache the value collection.
893 private transient Collection<String> values;
896 * Constructs a new empty <code>EnvironmentMap</code>.
898 EnvironmentMap()
900 super();
904 * Constructs a new <code>EnvironmentMap</code> containing
905 * the contents of the specified map.
907 * @param m the map to be added to this.
908 * @throws NullPointerException if a key or value is null.
909 * @throws ClassCastException if a key or value is not a String.
911 EnvironmentMap(Map<String,String> m)
913 super(m);
917 * Blocks queries containing a null key or one which is not
918 * of type <code>String</code>. All other queries
919 * are forwarded to the superclass.
921 * @param key the key to look for in the map.
922 * @return true if the key exists in the map.
923 * @throws NullPointerException if the specified key is null.
925 public boolean containsKey(Object key)
927 if (key == null)
928 throw new
929 NullPointerException("This map does not support null keys.");
930 if (!(key instanceof String))
931 throw new
932 ClassCastException("This map only allows queries using Strings.");
933 return super.containsKey(key);
937 * Blocks queries using a null or non-<code>String</code> value.
938 * All other queries are forwarded to the superclass.
940 * @param value the value to look for in the map.
941 * @return true if the value exists in the map.
942 * @throws NullPointerException if the specified value is null.
944 public boolean containsValue(Object value)
946 if (value == null)
947 throw new
948 NullPointerException("This map does not support null values.");
949 if (!(value instanceof String))
950 throw new
951 ClassCastException("This map only allows queries using Strings.");
952 return super.containsValue(value);
956 * Returns a set view of the map entries, with the same
957 * provisions as for the underlying map.
959 * @return a set containing the map entries.
961 public Set<Map.Entry<String,String>> entrySet()
963 if (entries == null)
964 entries = super.entrySet();
965 return entries;
969 * Blocks queries containing a null or non-<code>String</code> key.
970 * All other queries are passed on to the superclass.
972 * @param key the key to retrieve the value for.
973 * @return the value associated with the given key.
974 * @throws NullPointerException if the specified key is null.
975 * @throws ClassCastException if the specified key is not a String.
977 public String get(Object key)
979 if (key == null)
980 throw new
981 NullPointerException("This map does not support null keys.");
982 if (!(key instanceof String))
983 throw new
984 ClassCastException("This map only allows queries using Strings.");
985 return super.get(key);
989 * Returns a set view of the keys, with the same
990 * provisions as for the underlying map.
992 * @return a set containing the keys.
994 public Set<String> keySet()
996 if (keys == null)
997 keys = new EnvironmentSet(super.keySet());
998 return keys;
1002 * Associates the given key to the given value. If the
1003 * map already contains the key, its value is replaced.
1004 * The map does not accept null keys or values, or keys
1005 * and values not of type {@link String}.
1007 * @param key the key to map.
1008 * @param value the value to be mapped.
1009 * @return the previous value of the key, or null if there was no mapping
1010 * @throws NullPointerException if a key or value is null.
1011 * @throws ClassCastException if a key or value is not a String.
1013 public String put(String key, String value)
1015 if (key == null)
1016 throw new NullPointerException("A new key is null.");
1017 if (value == null)
1018 throw new NullPointerException("A new value is null.");
1019 if (!(key instanceof String))
1020 throw new ClassCastException("A new key is not a String.");
1021 if (!(value instanceof String))
1022 throw new ClassCastException("A new value is not a String.");
1023 return super.put(key, value);
1027 * Removes a key-value pair from the map. The queried key may not
1028 * be null or of a type other than a <code>String</code>.
1030 * @param key the key of the entry to remove.
1031 * @return the removed value.
1032 * @throws NullPointerException if the specified key is null.
1033 * @throws ClassCastException if the specified key is not a String.
1035 public String remove(Object key)
1037 if (key == null)
1038 throw new
1039 NullPointerException("This map does not support null keys.");
1040 if (!(key instanceof String))
1041 throw new
1042 ClassCastException("This map only allows queries using Strings.");
1043 return super.remove(key);
1047 * Returns a collection view of the values, with the same
1048 * provisions as for the underlying map.
1050 * @return a collection containing the values.
1052 public Collection<String> values()
1054 if (values == null)
1055 values = new EnvironmentCollection(super.values());
1056 return values;
1062 * This is a specialised <code>Set</code>, providing
1063 * the necessary provisions for the collections used by the
1064 * environment variable map. Namely, it prevents
1065 * modifications and the use of queries with null
1066 * or non-<code>String</code> values.
1068 * @author Andrew John Hughes (gnu_andrew@member.fsf.org)
1070 private static class EnvironmentSet
1071 extends EnvironmentCollection
1072 implements Set<String>
1076 * Constructs a new environment set, which
1077 * wraps the elements of the supplied set.
1079 * @param set the set to use as a base for
1080 * this set.
1082 public EnvironmentSet(Set<String> set)
1084 super(set);
1088 * This simply calls the same method on the wrapped
1089 * collection.
1091 * @param obj the object to compare with.
1092 * @return true if the two objects are equal.
1094 public boolean equals(Object obj)
1096 return c.equals(obj);
1100 * This simply calls the same method on the wrapped
1101 * collection.
1103 * @return the hashcode of the collection.
1105 public int hashCode()
1107 return c.hashCode();
1110 } // class EnvironmentSet<String>
1112 } // class System