1+ /***** BEGIN LICENSE BLOCK *****
2+ * Version: CPL 1.0/GPL 2.0/LGPL 2.1
3+ *
4+ * The contents of this file are subject to the Common Public
5+ * License Version 1.0 (the "License"); you may not use this file
6+ * except in compliance with the License. You may obtain a copy of
7+ * the License at http://www.eclipse.org/legal/cpl-v10.html
8+ *
9+ * Software distributed under the License is distributed on an "AS
10+ * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
11+ * implied. See the License for the specific language governing
12+ * rights and limitations under the License.
13+ *
14+ * Alternatively, the contents of this file may be used under the terms of
15+ * either of the GNU General Public License Version 2 or later (the "GPL"),
16+ * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
17+ * in which case the provisions of the GPL or the LGPL are applicable instead
18+ * of those above. If you wish to allow use of your version of this file only
19+ * under the terms of either the GPL or the LGPL, and not to allow others to
20+ * use your version of this file under the terms of the CPL, indicate your
21+ * decision by deleting the provisions above and replace them with the notice
22+ * and other provisions required by the GPL or the LGPL. If you do not delete
23+ * the provisions above, a recipient may use your version of this file under
24+ * the terms of any one of the CPL, the GPL or the LGPL.
25+ ***** END LICENSE BLOCK *****/
26+
27+ package org .jruby .java .util ;
28+
29+ import org .jruby .runtime .builtin .IRubyObject ;
30+
31+ import java .util .Collection ;
32+ import java .util .Map ;
33+ import java .util .Set ;
34+
35+ /**
36+ * A java.lang.Map that defers all methods to System properties.
37+ */
38+ public class SystemPropertiesMap implements Map {
39+ protected String stringFromObject (Object o ) {
40+ if (o instanceof String ) {
41+ return (String )o ;
42+ } else if (o instanceof IRubyObject ) {
43+ return (String )((IRubyObject )o ).toJava (String .class );
44+ }
45+ return null ;
46+ }
47+
48+ public int size () {
49+ return System .getProperties ().size ();
50+ }
51+
52+ public boolean isEmpty () {
53+ return false ;
54+ }
55+
56+ public boolean containsKey (Object o ) {
57+ String key = stringFromObject (o );
58+ if (key != null ) {
59+ return System .getProperty (key ) != null ;
60+ }
61+ return false ;
62+ }
63+
64+ public boolean containsValue (Object o ) {
65+ String value = stringFromObject (o );
66+ if (value != null ) {
67+ return System .getProperties ().containsValue (value );
68+ }
69+ return false ; //To change body of implemented methods use File | Settings | File Templates.
70+ }
71+
72+ public Object get (Object o ) {
73+ String key = stringFromObject (o );
74+ if (key != null ) {
75+ return System .getProperty (key );
76+ }
77+ return null ; //To change body of implemented methods use File | Settings | File Templates.
78+ }
79+
80+ public Object put (Object s , Object s1 ) {
81+ String key = stringFromObject (s );
82+ String value = stringFromObject (s1 );
83+ if (key != null ) {
84+ if (value == null ) {
85+ return System .clearProperty (key );
86+ }
87+ return System .setProperty (key , value );
88+ }
89+ return null ;
90+ }
91+
92+ public Object remove (Object o ) {
93+ String key = stringFromObject (o );
94+ if (key != null ) {
95+ return System .clearProperty (key );
96+ }
97+ return null ; //To change body of implemented methods use File | Settings | File Templates.
98+ }
99+
100+ public void putAll (Map map ) {
101+ for (Map .Entry entry : (Set <Entry >)map .entrySet ()) {
102+ String key = stringFromObject (entry .getKey ());
103+ String value = stringFromObject (entry .getValue ());
104+ if (key != null ) {
105+ if (value == null ) {
106+ System .clearProperty (key );
107+ } else {
108+ System .setProperty (key , value );
109+ }
110+ }
111+ }
112+ }
113+
114+ public void clear () {
115+ // ignored
116+ }
117+
118+ public Set <Object > keySet () {
119+ return System .getProperties ().keySet ();
120+ }
121+
122+ public Collection <Object > values () {
123+ return System .getProperties ().values ();
124+ }
125+
126+ public Set <Entry <Object , Object >> entrySet () {
127+ return System .getProperties ().entrySet ();
128+ }
129+ }
0 commit comments