directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From elecha...@apache.org
Subject svn commit: r158322 - in directory/sandbox/trunk/asn1-new-codec: ./ conf/ src/java/org/apache/asn1/util/ src/test/org/apache/asn1/util/
Date Sun, 20 Mar 2005 09:31:14 GMT
Author: elecharny
Date: Sun Mar 20 01:31:11 2005
New Revision: 158322

URL: http://svn.apache.org/viewcvs?view=rev&rev=158322
Log:
Added the stack class that will be used by the new asn.1 codec.
Added the associated test class, and a log4j configuration file.
Modified the project.xml to set the dependency to log4j (hope it works !)

Added:
    directory/sandbox/trunk/asn1-new-codec/conf/
    directory/sandbox/trunk/asn1-new-codec/conf/log4j.conf
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/Stack.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackFullException.java
    directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackResizeException.java
    directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/
    directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/StackTest.java
Modified:
    directory/sandbox/trunk/asn1-new-codec/project.xml

Added: directory/sandbox/trunk/asn1-new-codec/conf/log4j.conf
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/conf/log4j.conf?view=auto&rev=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/conf/log4j.conf (added)
+++ directory/sandbox/trunk/asn1-new-codec/conf/log4j.conf Sun Mar 20 01:31:11 2005
@@ -0,0 +1,13 @@
+# Properties to initialise log4j
+
+####################################################
+# DEFAULT
+log4j.rootLogger=WARN, APACHE-DS
+
+####################################################
+
+log4j.appender.APACHE-DS=org.apache.log4j.ConsoleAppender
+log4j.appender.APACHE-DS.layout=org.apache.log4j.PatternLayout
+#log4j.appender.APACHE-DS.layout.ConversionPattern=%8r> [%-20.20C{1}] - %m%n
+log4j.appender.APACHE-DS.layout.ConversionPattern=%8r> [%5.5p] %m%n
+

Modified: directory/sandbox/trunk/asn1-new-codec/project.xml
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/project.xml?view=diff&r1=158321&r2=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/project.xml (original)
+++ directory/sandbox/trunk/asn1-new-codec/project.xml Sun Mar 20 01:31:11 2005
@@ -1,179 +1,148 @@
-<?xml version="1.0" encoding="ISO-8859-1"?>
-<project>
-  <groupId>directory-asn1</groupId>
-  <id>asn1-codec</id>
-  <name>New expermiental LDAP ASN.1 codec</name>
-
-
-    <package>org.apache.asn1</package>
-    <currentVersion>0.1-SNAPSHOT</currentVersion>
-
-    <organization>
-      <name>The Apache Directory Project</name>
-      <url>http://directory.apache.org</url>
-      <logo>http://directory.apache.org/images/apache-directory-logo.png</logo>
-    </organization>
-
-    <inceptionYear>2003</inceptionYear>
-    
-    <logo>http://directory.apache.org/images/apache-directory-logo.png</logo>
-    <url>http://directory.apache.org/subprojects/asn1</url>
-
-    <issueTrackingUrl>
-      http://issues.apache.org/jira/secure/BrowseProject.jspa?id=10517
-    </issueTrackingUrl>
-
-    <siteAddress>minotaur.apache.org</siteAddress>
-    
-    <siteDirectory>
-      /www/directory.apache.org/subprojects/asn1
-    </siteDirectory>
-    <distributionDirectory>
-      /www/cvs.apache.org/dist/directory
-    </distributionDirectory>
-
-    <gumpRepositoryId>directory</gumpRepositoryId>
-
-    <repository>
-      <connection>
-        scm:svn:http://svn.apache.org/repos/asf/directory:sandbox/trunk/${module.path}
-      </connection>
-
-      <url>
-        http://svn.apache.org/viewcvs.cgi/directory/asn1/trunk/${module.path}/?root=Apache-SVN
-      </url>
-      
-      <developerConnection>
-        https://svn.apache.org/repos/asf/directory/asn1/trunk/${module.path}
-      </developerConnection>
-    </repository>
-  
-    <shortDescription>Apache ASN.1 Tools</shortDescription>
-      
-    <description>
+<?xml version="1.0" encoding="UTF-8"?>
+<project>
+  <pomVersion>1</pomVersion>
+  <groupId>directory-asn1</groupId>
+  <id>asn1-codec</id>
+  <name>New expermiental LDAP ASN.1 codec</name>
+  <package>org.apache.asn1</package>
+  <currentVersion>0.1-SNAPSHOT</currentVersion>
+  <organization>
+    <name>The Apache Directory Project</name>
+    <url>http://directory.apache.org</url>
+    <logo>http://directory.apache.org/images/apache-directory-logo.png</logo>
+  </organization>
+  <inceptionYear>2003</inceptionYear>
+  <logo>http://directory.apache.org/images/apache-directory-logo.png</logo>
+  <url>http://directory.apache.org/subprojects/sandbox/new-asn1-codec</url>
+  <issueTrackingUrl>http://issues.apache.org/jira/secure/BrowseProject.jspa?id=10517</issueTrackingUrl>
+  <siteAddress>minotaur.apache.org</siteAddress>
+  <siteDirectory>/www/directory.apache.org/subprojects/asn1</siteDirectory>
+  <distributionDirectory>/www/cvs.apache.org/dist/directory</distributionDirectory>
+  <gumpRepositoryId>directory</gumpRepositoryId>
+  <repository>
+    <connection>scm:svn:http://svn.apache.org/repos/asf/directory:sandbox/trunk/${module.path}</connection>
+    <url>http://svn.apache.org/viewcvs.cgi/directory/asn1/trunk/${module.path}/?root=Apache-SVN</url>
+    <developerConnection>https://svn.apache.org/repos/asf/directory/asn1/trunk/${module.path}</developerConnection>
+  </repository>
+  <shortDescription>Apache ASN.1 Tools</shortDescription>
+  <description>
       This is an experimental implementation of a new LDAP ASN.1 codec
-    </description>
-
-    <mailingLists>
-      <mailingList>
-        <name>Apache Directory Developement - use [asn1] prefix</name>
-          <subscribe>
+    </description>
+  <mailingLists>
+    <mailingList>
+      <name>Apache Directory Developement - use [asn1] prefix</name>
+      <subscribe>
             dev-subscribe@directory.apache.org
-          </subscribe>
-          <unsubscribe>
+          </subscribe>
+      <unsubscribe>
             dev-unsubscribe@directory.apache.org
-          </unsubscribe>
-          <archive>
+          </unsubscribe>
+      <archive>
             http://mail-archives.apache.org/eyebrowse/ViewLists
-          </archive>
-        </mailingList>
-    </mailingLists>
-    
-    <developers>
-      <developer>
-        <name>Emmanuel Lécharny</name>
-        <id>ele</id>
-        <email>elecharny at apache.org</email>
-        <roles>
-          <role>Developer</role>
-        </roles>
-      </developer>
-    </developers>
-
-    <licenses>
-      <license>
-        <name>Apache 2.0 License</name>
-        <url>
+          </archive>
+    </mailingList>
+  </mailingLists>
+  <developers>
+    <developer>
+      <name>Emmanuel Lécharny</name>
+      <id>ele</id>
+      <email>elecharny at apache.org</email>
+    </developer>
+  </developers>
+  <licenses>
+    <license>
+      <name>Apache 2.0 License</name>
+      <url>
           http://cvs.apache.org/viewcvs.cgi/directory/LICENSE.txt?rev=1369&amp;root=Apache-SVN&amp;view=markup
-        </url>
-        <distribution>repo</distribution>
-      </license>
-    </licenses>
-
-    <dependencies>
-      <dependency>
-        <groupId>junit</groupId>
-        <artifactId>junit</artifactId>
-        <version>3.8.1</version>
-        <url>http://junit.org</url>
-      </dependency>
-    </dependencies>        
-  
-    <build>
-      <nagEmailAddress>dev@directory.apache.org</nagEmailAddress>
-      <sourceDirectory>${basedir}/src/java</sourceDirectory>
-      <unitTestSourceDirectory>${basedir}/src/test</unitTestSourceDirectory>
-      <integrationUnitTestSourceDirectory/>
-      <aspectSourceDirectory/>
-
-      <unitTest>
-        <includes>
-          <include>**/*Test*</include>
-          <include>**/*TestCase*</include>
-        </includes>
-              
-        <excludes>
-            <exclude>**/testutils/*</exclude>
-            <exclude>**/Abstract*</exclude>
-        </excludes>
-        
-        <resources>
-          <resource>
-            <directory>${basedir}/src/test</directory>
-            <includes> 
-              <include>**/*.dtd</include>
-              <include>**/*.ldif</include>
-              <include>**/*.properties</include>
-              <include>**/*.x*</include>
-              <include>**/*.mf</include>
-              <include>**/*.jar</include>
-            </includes>
-          </resource>
-
-          <resource>
-            <directory>${basedir}/src/test</directory>
-            <includes> 
-              <include>**/*.dtd</include>
-              <include>**/*.ldif</include>
-              <include>**/*.properties</include>
-              <include>**/*.x*</include>
-              <include>**/*.mf</include>
-              <include>**/*.jar</include>
-            </includes>
-          </resource>
-        </resources>
-      </unitTest>
-    
-      <resources>
-
-        <resource>
-          <directory>${basedir}/src/java</directory>
-          <includes>
-            <include>**/*.dtd</include>
-            <include>**/*.properties</include>
-            <include>**/*.x*</include>
-          </includes>
-        </resource>
-
-        <resource>
-          <directory>${basedir}/../</directory>
-          <targetPath>META-INF</targetPath>
-          <includes>
-            <include>LICENSE.txt</include>
-            <include>NOTICE.txt</include>
-          </includes>
-        </resource>
-
-      </resources>
-      <jars></jars>
-    </build>
-    
-    <reports>
-    	<report>maven-jxr-plugin</report>
-    	<report>maven-license-plugin</report>
-        <!-- report>maven-changelog-plugin</report -->
-        <report>maven-javadoc-plugin</report>
-        <report>maven-clover-plugin</report>
-    	<report>maven-tasklist-plugin</report>
-    </reports>
-</project>
\ No newline at end of file
+        </url>
+      <distribution>repo</distribution>
+    </license>
+  </licenses>
+  <dependencies>
+    <dependency>
+      <groupId>junit</groupId>
+      <artifactId>junit</artifactId>
+      <version>3.8.1</version>
+      <type>jar</type>
+      <url>http://junit.org</url>
+    </dependency>
+    <dependency>
+      <groupId>log4j</groupId>
+      <artifactId>log4j</artifactId>
+      <version>1.2.8</version>
+      <type>jar</type>
+      <url>http://logging.apache.org/log4j</url>
+    </dependency>
+  </dependencies>
+  <build>
+    <nagEmailAddress>dev@directory.apache.org</nagEmailAddress>
+    <sourceDirectory>${basedir}/src/java</sourceDirectory>
+    <unitTestSourceDirectory>${basedir}/src/test</unitTestSourceDirectory>
+    <unitTest>
+      <includes>
+        <include>**/*Test*</include>
+        <include>**/*TestCase*</include>
+      </includes>
+      <excludes>
+        <exclude>**/testutils/*</exclude>
+        <exclude>**/Abstract*</exclude>
+      </excludes>
+      <resources>
+        <resource>
+          <directory>${basedir}/src/test</directory>
+          <includes>
+            <include>**/*.dtd</include>
+            <include>**/*.ldif</include>
+            <include>**/*.properties</include>
+            <include>**/*.x*</include>
+            <include>**/*.mf</include>
+            <include>**/*.jar</include>
+          </includes>
+          <filtering>false</filtering>
+        </resource>
+        <resource>
+          <directory>${basedir}/src/test</directory>
+          <includes>
+            <include>**/*.dtd</include>
+            <include>**/*.ldif</include>
+            <include>**/*.properties</include>
+            <include>**/*.x*</include>
+            <include>**/*.mf</include>
+            <include>**/*.jar</include>
+          </includes>
+          <filtering>false</filtering>
+        </resource>
+      </resources>
+    </unitTest>
+    <resources>
+      <resource>
+        <directory>${basedir}/src/java</directory>
+        <includes>
+          <include>**/*.dtd</include>
+          <include>**/*.properties</include>
+          <include>**/*.x*</include>
+        </includes>
+        <filtering>false</filtering>
+      </resource>
+      <resource>
+        <directory>${basedir}/../</directory>
+        <targetPath>META-INF</targetPath>
+        <includes>
+          <include>LICENSE.txt</include>
+          <include>NOTICE.txt</include>
+        </includes>
+        <filtering>false</filtering>
+      </resource>
+    </resources>
+    <jars />
+  </build>
+  <reports>
+    <report>maven-jxr-plugin</report>
+    <report>maven-license-plugin</report>
+    <!-- report>maven-changelog-plugin</report -->
+    <report>maven-javadoc-plugin</report>
+    <report>maven-clover-plugin</report>
+    <report>maven-tasklist-plugin</report>
+  </reports>
+</project>
+

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/Stack.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/Stack.java?view=auto&rev=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/Stack.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/Stack.java Sun Mar
20 01:31:11 2005
@@ -0,0 +1,455 @@
+/*
+ *   Copyright 2005 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.asn1.util;
+
+import org.apache.log4j.Logger;
+
+import java.util.EmptyStackException;
+
+
+/**
+ * A simple stack implementation. &lt;b&gt; It is NOT synchronized. &lt;/b&gt;
+ *
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class Stack
+{
+    //~ Static fields/initializers -----------------------------------------------------------------
+
+    /** The class logger */
+    private static final Logger log = Logger.getLogger( Stack.class );
+
+    /** A static to avoid String construction when invocing the logger */
+    private static final boolean DEBUG = log.isDebugEnabled();
+
+    /** A static to avoid String construction when invocing the logger  */
+    private static final boolean INFO = log.isInfoEnabled();
+
+    /** Constant means that the stack will be increased by a constant size
+     * (capacity = N, N + K, N + 2K, N + 3K, ...) 
+     **/
+    private static final int CONSTANT = 0;
+
+    /** Linear means that the stack will be increased by a increasing size
+     * (capacity = N, 2N, 3N, 4N, ...)
+     */
+    private static final int LINEAR = 1;
+
+    /** Geometric means that the stack will be increased by a increased size
+     *  (capacity = 1N, 2N, 4N, 8N ...)
+     **/
+    private static final int QUADRATIC = 2;
+
+    //~ Instance fields ----------------------------------------------------------------------------
+
+    /** Elements container */
+    private Object[] elements;
+
+    /** Current stack's capacity */
+    private int capacity;
+
+    /** Max stack capacity */
+    private int maxCapacity;
+
+    /** Size by which the stack will be increased if needed */
+    private int increment;
+
+    /** Current top of the stack. -1 if the stack is empty.*/
+    private int top;
+
+    /** Initial increment size, in case we choose a Linear strategy */
+    private int initialIncrement;
+
+    /** Strategy used to increase the stack capacity */
+    private int strategy = LINEAR;
+
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a new Stack object with a default initial capacity of 16 elements,
+     * a default increment of 16, no maximum size, and a Constant strategy.
+     */
+    public Stack()
+    {
+        elements         = new Object[16];
+        capacity         = 16;
+        maxCapacity      = 0;
+        increment        = 16;
+        top              = -1;
+        initialIncrement = increment;
+        strategy         = CONSTANT;
+    }
+
+    /**
+     * Creates a new Stack object whith an initial capacity.
+     * 
+     * @param capacity Initial capacity of the stack
+    */
+    public Stack(  int capacity )
+    {
+        elements         = new Object[capacity];
+        this.capacity    = capacity;
+        maxCapacity      = 0;
+        increment        = 16;
+        top              = -1;
+        initialIncrement = increment;
+        strategy         = CONSTANT;
+    }
+
+    /**
+     * Creates a new Stack object whith an initial capacity and a maximum capacity.
+     * 
+     * @param capacity Initial capacity of the stack
+     * @param maxCapacity Maximum stack capacity
+    */
+    public Stack(  int capacity, int maxCapacity )
+    {
+        elements         = new Object[capacity];
+        this.capacity    = capacity;
+        increment        = 16;
+        this.maxCapacity = maxCapacity;
+        top              = -1;
+        initialIncrement = increment;
+        strategy         = CONSTANT;
+    }
+
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Returns the current stack's size 
+     *
+     * @return  The current stacks size.
+     */
+    public int getSize()
+    {
+
+        return top + 1;
+    }
+
+    /**
+     * Returns the current stack's capacity 
+     *
+     * @return  The current stack's capacity.
+     */
+    public int getCapacity()
+    {
+
+        return capacity;
+    }
+
+    /**
+     * Tells if the stack is empty. 
+     *
+     * @return  wcode>true</code> if the stack is empty.
+     */
+    public boolean isEmpty()
+    {
+
+        return ( top == -1 );
+    }
+
+    /**
+     * Return the top of the stack, without removing it from the stack.   
+     *
+     * @return  The top element, or <code>null</code> if the stack is empty.
+     */
+    public Object peekStack()
+    {
+
+        if ( top == -1 )
+        {
+
+            return null;
+        }
+
+        return elements[top];
+    }
+
+    /**
+     * Return the top of the stack, and remove the element from it.
+     * Note that the stack element holder is nullified, to avoid a memeory
+     * leak.
+     *
+     * @return  The top element, or <code>null</code> if the stack is empty.
+     * @exception EmptyStackException Thrown if the stack is empty.
+     */
+    public Object popStack() throws EmptyStackException
+    {
+
+        if ( top == -1 )
+        {
+            throw new EmptyStackException();
+        }
+
+        Object object = elements[top];
+        elements[top--] = null;
+
+        return object;
+    }
+
+    /**
+     * Add an element on top of the stack. This is not a copy, it's a reference.
+     * 
+     * If the stack is full, we try to increase its capacity using the predefined
+     * strategy (one of Constant, Linear or Quadratic). If the stack's capacity 
+     * reach its limit, an exception is thrown.
+     *
+     * @param  object  The element to put on top of the stack.
+     * @exception StackFullException Thrown if the stack capacity has reached its limit
+     */
+    public void pushStack( Object object ) throws StackFullException
+    {
+
+        if ( top == ( capacity - 1 ) )
+        {
+
+            switch ( strategy )
+            {
+
+                case QUADRATIC :
+
+                    // Beware that it can lead to an out of memory very quickly !!
+                    increment += increment;
+
+                    break;
+
+                case LINEAR :
+                    increment += initialIncrement;
+
+                    break;
+
+                case CONSTANT :
+                    break;
+            }
+
+            int newCapacity = capacity + increment;
+
+            if ( ( newCapacity >= maxCapacity ) && ( maxCapacity != 0 ) )
+            {
+                // We have reach the limit! we have to check that it's not already
+                // the case for the current capacity, and if so, throw an exception
+
+                if ( capacity == maxCapacity )
+                {
+                    log.error( "The stack cannot be increased, it has reached its limit"
);
+                    throw new StackFullException( "Stack limit reached" );
+                }
+                else
+                {
+
+                    log.warn( "The stack's capacity limit has been reached." );
+                    newCapacity = maxCapacity;
+                }
+            }
+
+            if ( INFO )
+            {
+                log.info( "Stack is increased. Old capacity = " + capacity + ", new capacity
= " +
+                    ( capacity + increment ) );
+            }
+
+            Object[] newElements = new Object[newCapacity];
+            System.arraycopy( elements, 0, newElements, 0, capacity );
+            elements = newElements;
+            capacity = newCapacity;
+        }
+
+        elements[++top] = object;
+    }
+
+    /**
+     * Drain unused elements from the stack resizing it.
+     *
+     * @param newCapacity The new stack's capacity
+     * @exception StackResizeException Thrown if we ask a resize that exceed the maximum
capacity
+     */
+    public void resize( int newCapacity ) throws StackResizeException
+    {
+
+        if ( newCapacity < capacity )
+        {
+
+            if ( newCapacity < ( top + 1 ) )
+            {
+
+                // We are asked to release objects on the stack...
+                int nbDelete = ( top + 1 ) - newCapacity;
+
+                for ( int i = 0; i < nbDelete; i++ )
+                {
+
+                    // we just have to pop objects.
+                    popStack();
+                }
+
+                capacity = newCapacity;
+            }
+            else
+            {
+
+                // we just have to change the capacity
+                capacity = newCapacity;
+            }
+
+
+            // if we drain the stack, we will have to reset the increment
+            increment = initialIncrement;
+        }
+        else
+        {
+            // we have to increase the stack capaity, but we can't
+            // increase it above its maxCapacity
+
+            if ( newCapacity > maxCapacity )
+            {
+                log.error(
+                    "An attempt to resize (" + newCapacity +
+                    ") a stack above its maximul capacity (" + maxCapacity + ") has been
done" );
+                throw new StackResizeException(
+                    "Cannot resize a stack above its maximum capacity" );
+            }
+            else
+            {
+                this.capacity = newCapacity;
+            }
+        }
+    }
+
+    /**
+     * Get the current value of the increment used to increase the stack size 
+     *
+     * @return Returns the increment.
+     */
+    public int getIncrement()
+    {
+
+        return increment;
+    }
+
+    /**
+     * Set an increment value.
+     *
+     * @param increment The increment to set.
+     */
+    public void setIncrement( int increment )
+    {
+        this.increment = increment;
+    }
+
+    /**
+     * Get the maximum capacity of the current stack.
+     *
+     * @return Returns the maxCapacity.
+     */
+    public int getMaxCapacity()
+    {
+
+        return maxCapacity;
+    }
+
+    /**
+     * Set a maximum capacity of the current stack.
+     *
+     * @param maxCapacity The maxCapacity to set.
+     */
+    public void setMaxCapacity( int maxCapacity )
+    {
+        this.maxCapacity = maxCapacity;
+    }
+
+    /**
+     * Tells if the current implemented strategy to increase the stack capacity
+     * is Constant.
+     *
+     * @return Returns <code>true</code> if the strategy is constant.
+     */
+    public boolean isStrategyConstant()
+    {
+
+        return strategy == CONSTANT;
+    }
+
+    /**
+     * Tells if the current implemented strategy to increase the stack capacity
+     * is Linear.
+     *
+     * @return Returns <code>true</code> if the strategy is linear.
+     */
+    public boolean isStrategyLinear()
+    {
+
+        return strategy == LINEAR;
+    }
+
+    /**
+     * Tells if the current implemented strategy to increase the stack capacity
+     * is Quadratic.
+     *
+     * @return Returns <code>true</code> if the strategy is quadratic.
+     */
+    public boolean isStrategyQuadratic()
+    {
+
+        return strategy == QUADRATIC;
+    }
+
+    /**
+     * Set a Constant increase strategy. Each time the stack will have to grow, 
+     * a constant number of empty elems will be created. It's slower, but safer
+     * from a memory consomption point of vue.
+     * 
+     * @param increment Initial size of the increment  
+     */
+    public void setStrategyConstant( int increment )
+    {
+        strategy         = CONSTANT;
+        this.increment   = increment;
+        initialIncrement = increment;
+    }
+
+    /**
+     * Set a Constant increase strategy. Each time the stack will have to grow, 
+     * N*size empty elems will be created, where N will be incremented at each 
+     * invocation. It's faster than the constant strategy, but eat much more memory.
+     * 
+     * When setting a Linear strategy, it could be wize to evaluate the estimated
+     * average stack size, and create this stack with this number of elements, then 
+     * knowing the variance could help to set an adapted increment value.  
+     * 
+     * @param increment Initial size of the increment 
+    */
+    public void setStrategyLinear( int increment )
+    {
+        strategy         = LINEAR;
+        this.increment   = increment;
+        initialIncrement = increment;
+    }
+
+    /**
+     * Set a Quadratic increase strategy. This is experimental, and use to compare
+     * results against Constant and Linear startegy. It eats the memory very quickly... 
+     * Set a quadratic strategy
+     * 
+     * @param increment Initial size of the increment 
+     */
+    public void setStrategyQuadratic( int increment )
+    {
+        strategy         = QUADRATIC;
+        this.increment   = increment;
+        initialIncrement = increment;
+    }
+} // end class Stack

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackFullException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackFullException.java?view=auto&rev=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackFullException.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackFullException.java
Sun Mar 20 01:31:11 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2005 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.asn1.util;
+
+/**
+ * An exception that is raised if the stack is full.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class StackFullException extends Exception
+{
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a StackFullException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public StackFullException(  String message )
+    {
+        super(  message );
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackResizeException.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackResizeException.java?view=auto&rev=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackResizeException.java
(added)
+++ directory/sandbox/trunk/asn1-new-codec/src/java/org/apache/asn1/util/StackResizeException.java
Sun Mar 20 01:31:11 2005
@@ -0,0 +1,37 @@
+/*
+ *   Copyright 2005 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.asn1.util;
+
+/**
+ * An exception that is raised if the stack resize could not be done.
+ * 
+ * @author <a href="mailto:directory-dev@incubator.apache.org">Apache Directory Project</a>
+ */
+public class StackResizeException extends Exception
+{
+    //~ Constructors -------------------------------------------------------------------------------
+
+    /**
+     * Creates a StackResizeException
+     * 
+     * @param message A message with meaning to a human
+     */
+    public StackResizeException(  String message )
+    {
+        super(  message );
+    }
+}

Added: directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/StackTest.java
URL: http://svn.apache.org/viewcvs/directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/StackTest.java?view=auto&rev=158322
==============================================================================
--- directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/StackTest.java (added)
+++ directory/sandbox/trunk/asn1-new-codec/src/test/org/apache/asn1/util/StackTest.java Sun
Mar 20 01:31:11 2005
@@ -0,0 +1,295 @@
+/*
+ *   Copyright 2005 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.asn1.util;
+
+import junit.framework.Assert;
+import junit.framework.TestCase;
+
+import org.apache.asn1.util.StackFullException;
+
+import org.apache.log4j.PropertyConfigurator;
+
+
+/**
+ * Test the class Stack
+ */
+public class StackTest extends TestCase
+{
+    //~ Methods ------------------------------------------------------------------------------------
+
+    /**
+     * Test stack creation
+     */
+    public void testStackCreate()
+    {
+
+        Stack stack = new Stack();
+
+        // The stack should be empty
+        Assert.assertEquals( 0, stack.getSize() );
+
+        try
+        {
+
+            // let's try to add an element
+            stack.pushStack( this );
+
+            Assert.assertEquals( 1, stack.getSize() );
+        }
+        catch ( StackFullException sfe )
+        {
+
+            // should never occurs...
+            Assert.fail();
+        }
+    }
+
+    /**
+     * Test stack creation with a size
+     */
+    public void testStackCreateSize()
+    {
+        PropertyConfigurator.configure( "conf/log4j.conf" );
+
+        // 2 elements stack's size
+        Stack stack = new Stack( 2 );
+        stack.setMaxCapacity( 3 );
+
+        // The stack should be empty
+        Assert.assertEquals( 0, stack.getSize() );
+
+        try
+        {
+
+            // let's try to add two elements
+            stack.pushStack( this );
+            stack.pushStack( this );
+
+            Assert.assertEquals( 2, stack.getSize() );
+
+            stack.pushStack( this );
+        }
+        catch ( StackFullException sfe )
+        {
+
+            // should never occurs...
+            Assert.fail();
+        }
+
+        // The stack is full, so we must catch an exception
+        try
+        {
+            stack.pushStack( this );
+            Assert.fail( "Stack should be full" );
+        }
+        catch ( StackFullException sfe )
+        {
+            Assert.assertTrue( true );
+        }
+    }
+
+    /**
+     * Test stack resizing
+     */
+    public void testStackResize()
+    {
+
+        // Create an empty stack with an initial capacity
+        // of 2 elements and a maximum capacity of 48 elements
+        Stack stack = new Stack( 2, 48 );
+
+        // We also set the increment strategy to linear and the
+        // increment initial value to 1;
+        stack.setStrategyLinear( 1 );
+
+        // The stack should be empty
+        Assert.assertEquals( 0, stack.getSize() );
+
+        // its capacity should be 16
+        Assert.assertEquals( 2, stack.getCapacity() );
+
+        // its maximum capacity should be 48
+        Assert.assertEquals( 48, stack.getMaxCapacity() );
+
+        // let's use
+
+        try
+        {
+
+            // let's push 8 elements
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+
+            // The stack's size should be 8
+            Assert.assertEquals( 8, stack.getSize() );
+
+            // resize the stack to 32
+            try
+            {
+                stack.resize( 32 );
+            }
+            catch ( StackResizeException sre )
+            {
+
+                // Should not happens
+                Assert.fail();
+            }
+
+            // The capacity must have changed
+            Assert.assertEquals( 32, stack.getCapacity() );
+        }
+        catch ( StackFullException sfe )
+        {
+
+            // should never occurs...
+            Assert.fail();
+        }
+    }
+
+    /**
+     * Test stack resizing above maxCapacity
+     */
+    public void testStackResizeAboveMaxCapacity()
+    {
+
+        // Create an empty stack with an initial capacity
+        // of 2 elements and a maximum capacity of 48 elements
+        Stack stack = new Stack( 2, 48 );
+
+        // resize the stack to 64
+        try
+        {
+            stack.resize( 64 );
+            Assert.fail();
+        }
+        catch ( StackResizeException sre )
+        {
+            Assert.assertTrue( sre.getMessage(), true );
+        }
+    }
+
+    /**
+     * Test stack resizing below the current stack size
+     */
+    public void testStackResizeBelowCurrentSize()
+    {
+
+        // Create an empty stack with an initial capacity
+        // of 2 elements and a maximum capacity of 48 elements
+        Stack stack = new Stack( 2, 48 );
+
+        // We also set the increment strategy to linear and the
+        // increment initial value to 1;
+        stack.setStrategyLinear( 1 );
+
+        // The stack should be empty
+        Assert.assertEquals( 0, stack.getSize() );
+
+        // its capacity should be 16
+        Assert.assertEquals( 2, stack.getCapacity() );
+
+        // its maximum capacity should be 48
+        Assert.assertEquals( 48, stack.getMaxCapacity() );
+
+        // let's use
+
+        try
+        {
+
+            // let's push 8 elements
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+
+            // The stack's size should be 8
+            Assert.assertEquals( 8, stack.getSize() );
+
+            // resize the stack to 32
+            try
+            {
+                stack.resize( 4 );
+            }
+            catch ( StackResizeException sre )
+            {
+
+                // Should not happens
+                Assert.fail();
+            }
+
+            // The capacity must have changed
+            Assert.assertEquals( 4, stack.getCapacity() );
+
+            // so does the increment, that should have been reset
+            Assert.assertEquals( 1, stack.getIncrement() );
+
+            // And the elements above the new capacity must have been
+            // discarded from the stack
+            Assert.assertEquals( 4, stack.getSize() );
+        }
+        catch ( StackFullException sfe )
+        {
+
+            // should never occurs...
+            Assert.fail();
+        }
+    }
+
+    /**
+     * Test stack overflow
+     */
+    public void testStackOverflow()
+    {
+
+        // Create an empty stack with an initial capacity
+        // of 2 elements and a maximum capacity of 10 elements
+        Stack stack = new Stack( 2, 10 );
+
+        // resize the stack to 64
+        try
+        {
+
+            // let's push 11 elements
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            stack.pushStack( this );
+            Assert.fail();
+        }
+        catch ( StackFullException sre )
+        {
+            Assert.assertTrue( sre.getMessage(), true );
+        }
+    }
+}



Mime
View raw message