geronimo-scm mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jdil...@apache.org
Subject svn commit: r410877 - in /geronimo/sandbox/gshell/trunk/gshell-core/src: main/java/org/apache/geronimo/gshell/command/ test/java/org/apache/geronimo/gshell/command/
Date Thu, 01 Jun 2006 15:45:41 GMT
Author: jdillon
Date: Thu Jun  1 08:45:41 2006
New Revision: 410877

URL: http://svn.apache.org/viewvc?rev=410877&view=rev
Log:
Variables overhaul; added nesting, cloaking, immutability, defaults

Added:
    geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
  (with props)
Modified:
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/Variables.java
    geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesMap.java

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/Variables.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/Variables.java?rev=410877&r1=410876&r2=410877&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/Variables.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/Variables.java
Thu Jun  1 08:45:41 2006
@@ -25,13 +25,35 @@
  */
 public interface Variables
 {
-    void set(String name, Object value);
-    
+    void set(String name, Object value) throws ImmutableVariableException;
+
+    void set(String name, Object value, boolean mutable) throws ImmutableVariableException;
+
     Object get(String name);
-    
-    void unset(String name);
+
+    Object get(String name, Object _default);
+
+    boolean isMutable(String name);
+
+    boolean isCloaked(String name);
+
+    void unset(String name) throws ImmutableVariableException;
     
     boolean contains(String name);
     
-    Iterator names();
+    Iterator<String> names();
+
+    Variables parent();
+
+    //
+    // Exceptions
+    //
+
+    class ImmutableVariableException
+        extends RuntimeException
+    {
+        public ImmutableVariableException(final String name) {
+            super("Variable is immutable: " + name);
+        }
+    }
 }

Modified: geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesMap.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesMap.java?rev=410877&r1=410876&r2=410877&view=diff
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesMap.java
(original)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/main/java/org/apache/geronimo/gshell/command/VariablesMap.java
Thu Jun  1 08:45:41 2006
@@ -19,6 +19,10 @@
 import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
+import java.util.List;
+import java.util.Set;
+import java.util.HashSet;
+import java.util.Collections;
 
 /**
  * ???
@@ -28,43 +32,143 @@
 public class VariablesMap
     implements Variables
 {
-    private final Map map;
-    
-    public VariablesMap(final Map map) {
-        assert map != null;
-        
+    private final Map<String,Object> map;
+
+    private final Variables parent;
+
+    private final Set<String> immutables = new HashSet<String>();
+
+    public VariablesMap(final Map<String,Object> map, final Variables parent) {
+        if (map == null) {
+            throw new IllegalArgumentException("Map is null");
+        }
+        if (parent == null) {
+            throw new IllegalArgumentException("Parent is null");
+        }
+
         this.map = map;
+        this.parent = parent;
+    }
+
+    public VariablesMap(final Variables parent) {
+        if (parent == null) {
+            throw new IllegalArgumentException("Parent is null");
+        }
+
+        this.map = new HashMap<String,Object>();
+        this.parent = parent;
     }
-    
+
+    public VariablesMap(final Map<String,Object> map) {
+        if (map == null) {
+            throw new IllegalArgumentException("Map is null");
+        }
+
+        this.map = map;
+        this.parent = null;
+    }
+
     public VariablesMap() {
-        this(new HashMap());
+        this(new HashMap<String,Object>());
     }
-    
+
     public void set(final String name, final Object value) {
+        set(name, value, true);
+    }
+
+    public void set(final String name, final Object value, boolean mutable) {
         assert name != null;
-        
+
+        ensureMutable(name);
+
         map.put(name, value);
+
+        if (!mutable) {
+            immutables.add(name);
+        }
     }
-    
+
     public Object get(final String name) {
         assert name != null;
-        
-        return map.get(name);
+
+        Object value = map.get(name);
+        if (value == null && parent != null) {
+            value = parent.get(name);
+        }
+
+        return value;
+    }
+
+    public Object get(final String name, final Object _default) {
+        Object value = get(name);
+        if (value == null) {
+            return _default;
+        }
+
+        return value;
     }
-    
+
     public void unset(final String name) {
         assert name != null;
-        
+
+        ensureMutable(name);
+
         map.remove(name);
     }
-    
+
     public boolean contains(final String name) {
         assert name != null;
-        
+
         return map.containsKey(name);
     }
-    
-    public Iterator names() {
-        return map.keySet().iterator();
+
+    public boolean isMutable(final String name) {
+        assert name != null;
+
+        boolean mutable = true;
+
+        // First ask out parent if there is one, if they are immutable, then so are we
+        if (parent != null) {
+            mutable = parent.isMutable(name);
+        }
+
+        if (mutable) {
+            mutable = !immutables.contains(name);
+        }
+
+        return mutable;
+    }
+
+    private void ensureMutable(final String name) {
+        assert name != null;
+
+        if (!isMutable(name)) {
+            throw new ImmutableVariableException(name);
+        }
+    }
+
+    public boolean isCloaked(final String name) {
+        assert name != null;
+
+        int count = 0;
+
+        Variables vars = this;
+        while (vars != null && count < 2) {
+            if (vars.contains(name)) {
+                count++;
+            }
+
+            vars = vars.parent();
+        }
+
+        return count > 1;
+    }
+
+    public Iterator<String> names() {
+        return Collections.unmodifiableSet(map.keySet()).iterator();
+    }
+
+    public Variables parent() {
+        return parent;
     }
 }

Added: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java?rev=410877&view=auto
==============================================================================
--- geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
(added)
+++ geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
Thu Jun  1 08:45:41 2006
@@ -0,0 +1,229 @@
+/*
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.geronimo.gshell.command;
+
+import junit.framework.TestCase;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Unit tests for the {@link VariablesMap} class.
+ *
+ * @version $Id$
+ */
+public class VariablesMapTest
+    extends TestCase
+{
+    public void testConstructorArgsNull() throws Exception {
+        try {
+            new VariablesMap((Map<String,Object>)null);
+            fail("Accepted a null value");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+
+        try {
+            new VariablesMap((Variables)null);
+            fail("Accepted a null value");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+
+        try {
+            new VariablesMap(new HashMap<String,Object>(), null);
+            fail("Accepted a null value");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+
+        // Happy day
+        new VariablesMap(new HashMap<String,Object>(), new VariablesMap());
+    }
+
+    /*
+    public void testSetNameIsNull() throws Exception {
+        try {
+            new VariablesMap().set(null, null);
+            fail("Accepted a null value");
+        }
+        catch (IllegalArgumentException expected) {
+            // ignore
+        }
+    }
+    */
+
+    public void testSet() throws Exception {
+        VariablesMap vars = new VariablesMap();
+        String name = "a";
+        Object value = new Object();
+
+        assertFalse(vars.contains(name));
+        vars.set(name, value);
+        assertTrue(vars.contains(name));
+
+        Object obj = vars.get(name);
+        assertEquals(value, obj);
+
+        String str = vars.names().next();
+        assertEquals(name, str);
+    }
+
+    public void testSetAsImmutable() throws Exception {
+        VariablesMap vars = new VariablesMap();
+        String name = "a";
+        Object value = new Object();
+
+        assertTrue(vars.isMutable(name));
+        vars.set(name, value, false);
+        assertFalse(vars.isMutable(name));
+
+        try {
+            vars.set(name, value);
+            fail("Set an immutable variable");
+        }
+        catch (Variables.ImmutableVariableException expected) {
+            // ignore
+        }
+    }
+
+    public void testSetAsImmutableInParent() throws Exception {
+        Variables parent = new VariablesMap();
+        VariablesMap vars = new VariablesMap(parent);
+        String name = "a";
+        Object value = new Object();
+
+        parent.set(name, value, false);
+        assertFalse(parent.isMutable(name));
+        assertFalse(vars.isMutable(name));
+
+        try {
+            vars.set(name, value);
+            fail("Set an immutable variable");
+        }
+        catch (Variables.ImmutableVariableException expected) {
+            // ignore
+        }
+    }
+
+    public void testGet() throws Exception {
+        VariablesMap vars = new VariablesMap();
+        String name = "a";
+        Object value = new Object();
+
+        Object obj1 = vars.get(name);
+        assertNull(obj1);
+
+        vars.set(name, value);
+        Object obj2 = vars.get(name);
+        assertSame(value, obj2);
+    }
+
+    public void testGetUsingDefault() throws Exception {
+        VariablesMap vars = new VariablesMap();
+        String name = "a";
+        Object value = new Object();
+
+        Object obj1 = vars.get(name);
+        assertNull(obj1);
+
+        Object obj2 = vars.get(name, value);
+        assertSame(value, obj2);
+    }
+
+    public void testGetCloaked() throws Exception {
+        Variables parent = new VariablesMap();
+        VariablesMap vars = new VariablesMap(parent);
+        String name = "a";
+        Object value = new Object();
+
+        parent.set(name, value);
+        Object obj1 = vars.get(name);
+        assertEquals(value, obj1);
+
+        Object value2 = new Object();
+        vars.set(name, value2);
+
+        Object obj2 = vars.get(name);
+        assertSame(value2, obj2);
+        assertNotSame(value, obj2);
+    }
+
+    public void testUnsetAsImmutable() throws Exception {
+        VariablesMap vars = new VariablesMap();
+        String name = "a";
+        Object value = new Object();
+
+        assertTrue(vars.isMutable(name));
+        vars.set(name, value, false);
+        assertFalse(vars.isMutable(name));
+
+        try {
+            vars.unset(name);
+            fail("Unset an immutable variable");
+        }
+        catch (Variables.ImmutableVariableException expected) {
+            // ignore
+        }
+    }
+
+    public void testUnsetAsImmutableInParent() throws Exception {
+        Variables parent = new VariablesMap();
+        VariablesMap vars = new VariablesMap(parent);
+        String name = "a";
+        Object value = new Object();
+
+        parent.set(name, value, false);
+        assertFalse(parent.isMutable(name));
+        assertFalse(vars.isMutable(name));
+
+        try {
+            vars.unset(name);
+            fail("Unset an immutable variable");
+        }
+        catch (Variables.ImmutableVariableException expected) {
+            // ignore
+        }
+    }
+
+    public void testCloaking() throws Exception {
+        Variables parent = new VariablesMap();
+        VariablesMap vars = new VariablesMap(parent);
+        String name = "a";
+        Object value = new Object();
+
+        parent.set(name, value);
+        assertFalse(parent.isCloaked(name));
+        assertFalse(vars.isCloaked(name));
+
+        vars.set(name, new Object());
+        assertTrue(vars.isCloaked(name));
+    }
+
+    public void testParent() throws Exception {
+        Variables parent = new VariablesMap();
+        assertNull(parent.parent());
+
+        VariablesMap vars = new VariablesMap(parent);
+        assertNotNull(vars.parent());
+
+        assertEquals(parent, vars.parent());
+    }
+}

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Propchange: geronimo/sandbox/gshell/trunk/gshell-core/src/test/java/org/apache/geronimo/gshell/command/VariablesMapTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain



Mime
View raw message