empire-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From doeb...@apache.org
Subject svn commit: r1298026 - in /empire-db/trunk/empire-db/src: main/java/org/apache/empire/commons/ main/java/org/apache/empire/db/ test/java/org/apache/empire/commons/
Date Wed, 07 Mar 2012 17:27:34 GMT
Author: doebele
Date: Wed Mar  7 17:27:33 2012
New Revision: 1298026

URL: http://svn.apache.org/viewvc?rev=1298026&view=rev
Log:
EMPIREDB-123 
some corrections

Modified:
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Attributes.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
    empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java
    empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/AttributesTest.java

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Attributes.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Attributes.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Attributes.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Attributes.java Wed
Mar  7 17:27:33 2012
@@ -18,64 +18,269 @@
  */
 package org.apache.empire.commons;
 
-import java.util.LinkedHashMap;
-import java.util.Map;
+import java.io.Serializable;
+import java.util.AbstractSet;
+import java.util.ArrayList;
+import java.util.Iterator;
 
+import org.apache.empire.exceptions.InvalidArgumentException;
 import org.w3c.dom.Element;
 
 /**
  * This class holds a map of objects which are identified by a case insensitive key string.
  * 
  */
-public class Attributes extends LinkedHashMap<String, Object> 
+public class Attributes extends AbstractSet<Attributes.Attribute> implements Serializable

 {
-	private static final long serialVersionUID = 1L;
-
-	/**
-     * @param key the attribute
-     * @return the attribute value
-     */
-    public Object get(String key)
-    {   // Check Key
-        if (key==null || key.length()==0)
+	static final class Attribute implements Serializable 
+	{
+        private static final long serialVersionUID = 1L;
+	    private String name;
+        private String namespace;
+	    private Object value;
+	    
+	    public Attribute(String name, Object value, String namespace)
+        {   // check name
+	        if (name==null || (this.name=name.trim()).length()==0)
+	            throw new InvalidArgumentException("name", name);
+	        this.namespace = namespace;
+            this.value = value;
+        }
+        public Attribute(String name, Object value)
+        {   // check name
+            this(name, value, null);
+        }
+        public String getName()
+        {
+            return name;
+        }
+        public String getNamespace()
+        {
+            return namespace;
+        }
+        public Object getValue()
+        {
+            return value;
+        }
+        public void setValue(Object value)
+        {
+            this.value = value;
+        }
+        @Override
+        public boolean equals(Object other)
+        {   // check namespace
+            if (namespace!=null)
+            {
+                String otherNs = (other instanceof Attribute) ? ((Attribute)other).getNamespace()
: null;
+                if (!namespace.equalsIgnoreCase(otherNs))
+                    return false; // Namespace don't match
+            }
+            // check name
+            String otherName = (other instanceof Attribute) ? ((Attribute)other).getName()
: other.toString();
+            return name.equalsIgnoreCase(otherName);
+        }
+	}
+	
+	static final class EmptyIterator implements Iterator<Attribute>
+	{
+        public boolean hasNext()  {
+            return false;
+        }
+        public Attribute next() {
             return null;
-        // Get
-        return super.get(key.toLowerCase());
+        }
+        public void remove() {
+        }
+	}
+
+    private static final long serialVersionUID = 1L;
+    
+	private static final EmptyIterator emptyIterator = new EmptyIterator(); 
+	
+	protected ArrayList<Attributes.Attribute> attributes = null;
+	
+	protected ArrayList<Attributes.Attribute> list()
+	{
+	    if (attributes==null)
+	        attributes = new ArrayList<Attributes.Attribute>(2);
+	    return attributes;
+	}
+    
+    public Attributes()
+    {
+    }
+    
+    public Attributes(int size)
+    {
+        if (size>0)
+            attributes = new ArrayList<Attributes.Attribute>(size);
     }
 
     @Override
-    public Object get(Object key)
-    {   // Check Key
-        return get((key!=null ? key.toString() : null));
+    public Iterator<Attribute> iterator()
+    {
+        return (attributes!=null ? list().iterator() : emptyIterator);
     }
 
     @Override
-    public Object put(String key, Object v)
+    public int size()
+    {
+        return (attributes!=null ? attributes.size() : 0);
+    }
+    
+    @Override
+    public void clear()
+    {
+        if (attributes!=null)
+            attributes.clear();
+    }
+    
+    @Override
+    public boolean contains(Object item)
     {
-        if (key==null || key.length()==0)
+        if (item==null)
+            return false;
+        // find
+        String name = (item instanceof Attribute) ? ((Attribute)item).getName() : item.toString();
+        return (indexOf(name)>=0);
+    }
+    
+    @Override
+    public boolean add(Attribute a)
+    {
+        set(a.getName(), a.getValue());
+        return true;
+    }
+
+    @Override
+    public boolean remove(Object item)
+    {
+        if (item==null)
+            return false;
+        // find
+        String name = (item instanceof Attribute) ? ((Attribute)item).getName() : item.toString();
+        int i = indexOf(name);
+        if (i<0)
+            return false;
+        // remove
+        list().remove(i);
+        return true;
+    }
+
+    /**
+     * @param key the attribute
+     * @return the attribute value
+     */
+    public int indexOf(String name)
+    {   // Find an Entry
+        if (attributes==null)
+            return -1;
+        // Find it now
+        int size = attributes.size();
+        for (int i = 0; i < size; i++)
+        { // Search List for Index
+            Attribute a = attributes.get(i);
+            if (a.getName().equalsIgnoreCase(name))
+                return i;
+        }
+        return -1;
+    }
+
+	/**
+     * @param key the attribute
+     * @return the attribute value
+     */
+    public Object get(String name)
+    {   // check name
+        if (attributes==null || name==null || name.length()==0)
             return null;
-        return super.put(key.toLowerCase(), v);
+        // find
+        int i = indexOf(name);
+        if (i<0)
+            return null; // Not set
+        // found
+        return list().get(i).getValue();
     }
 
     /**
      * @param name the attribute
      * @param object the attribute value
      */
-    public void set(String name, Object object)
+    public Attribute set(String name, Object value)
+    {
+        // check name
+        if (name==null || name.length()==0)
+            return null;
+        // Find
+        int i = indexOf(name);
+        if (i<0)
+        {   // new attribute
+            Attribute a = new Attribute(name, value); 
+            list().add(a);
+            return a;
+        }
+        else
+        {   // existing attribute
+            Attribute a = list().get(i);
+            a.setValue(value);
+            return a;
+        }
+    }
+
+    @Override
+    public Object[] toArray()
     {
-        this.put(name, object);
+        return (isEmpty() ? new Object[0] : list().toArray());
     }
 
+    @Override
+    public String toString()
+    {
+        if (isEmpty())
+            return "{ /*empty*/ }";
+        // create string
+        StringBuilder b = new StringBuilder();
+        b.append("{");
+        boolean first = true;
+        for (Attribute a : list())
+        {
+            b.append((first) ? "\"" : ",\r\n \"");
+            String ns = a.getNamespace();
+            if (StringUtils.isNotEmpty(ns))
+            {   // append namespace
+                b.append(ns);
+                b.append(":");
+            }
+            b.append(a.getName());
+            b.append("\":\"");
+            b.append(a.getValue());
+            b.append("\"");
+            first = false;
+        }    
+        b.append("}");
+        return b.toString();
+    }
+    
     /**
      * @param element the XMLElement to which to append the options
      * @param flags options (currently unused)
      */
     public void addXml(Element element, long flags)
     {
+        if (isEmpty())
+            return;
         // add All Options
-    	for(Map.Entry<String,Object> entry:entrySet())
+    	for (Attribute a : list())
     	{
-    		element.setAttribute(entry.getKey(), String.valueOf(entry.getValue()));
+    	    Object value = a.getValue();
+    	    if (value==null)
+    	        continue;
+    	    // set xml attribute
+            String ns = a.getNamespace();
+            if (StringUtils.isNotEmpty(ns))
+                element.setAttributeNS(ns, a.getName(), String.valueOf(value));
+            else
+                element.setAttribute(a.getName(), String.valueOf(value));
     	}
     }
 }

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/commons/Options.java Wed Mar
 7 17:27:33 2012
@@ -26,6 +26,7 @@ import java.util.Iterator;
 import java.util.Set;
 import java.util.Map.Entry;
 
+import org.apache.empire.commons.Attributes.Attribute;
 import org.apache.empire.xml.XMLUtil;
 import org.w3c.dom.Element;
 
@@ -264,6 +265,25 @@ public class Options extends AbstractSet
         return list.toArray();
     }
 
+    @Override
+    public String toString()
+    {
+        StringBuilder b = new StringBuilder();
+        b.append("{");
+        boolean first = true;
+        for (OptionEntry e : list)
+        {
+            b.append((first) ? "\"" : ",\r\n \"");
+            b.append(StringUtils.toString(e.getValue()));
+            b.append("\":\"");
+            b.append(e.getText());
+            b.append("\"");
+            first = false;
+        }    
+        b.append("}");
+        return b.toString();
+    }
+
     /**
      * Adds all these options to the xml element
      * 

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumn.java Wed Mar  7
17:27:33 2012
@@ -128,7 +128,7 @@ public abstract class DBColumn extends D
      * 
      * @return true if column is a character based column
      */
-    public boolean isCharacterColumn()
+    public boolean isTextColumn()
     {
         DataType type = getDataType();
         return (type==DataType.TEXT || type ==DataType.CHAR || type==DataType.CLOB);

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBColumnExpr.java Wed Mar
 7 17:27:33 2012
@@ -113,7 +113,7 @@ public abstract class DBColumnExpr exten
      */
     public synchronized Object getAttribute(String name)
     {
-        if (attributes != null && attributes.containsKey(name))
+        if (attributes != null && attributes.contains(name))
             return attributes.get(name);
         // Otherwise ask expression
         DBColumn column = getUpdateColumn();

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBCommandExpr.java Wed Mar
 7 17:27:33 2012
@@ -250,7 +250,7 @@ public abstract class DBCommandExpr exte
         @Override
         public Object getAttribute(String name)
         {
-            if (attributes != null && attributes.containsKey(name))
+            if (attributes != null && attributes.contains(name))
                 return attributes.get(name);
             // Otherwise ask expression
             DBColumn column = expr.getUpdateColumn();

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBIndex.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBIndex.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBIndex.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBIndex.java Wed Mar  7 17:27:33
2012
@@ -81,7 +81,7 @@ public class DBIndex extends DBObject
      * The table must be added to the table's index list beforehand 
      * @param table
      */
-    protected void setTable(DBTable table)
+    void setTable(DBTable table)
     {
         if (table==null || !table.getIndexes().contains(this))
             throw new InvalidArgumentException("table", table);

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBQuery.java Wed Mar  7 17:27:33
2012
@@ -132,7 +132,7 @@ public class DBQuery extends DBRowSet
         @Override
         public Object getAttribute(String name)
         {
-            if (attributes != null && attributes.containsKey(name))
+            if (attributes != null && attributes.contains(name))
                 return attributes.get(name);
             // Otherwise ask expression
             DBColumn column = expr.getUpdateColumn();

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBTableColumn.java Wed Mar
 7 17:27:33 2012
@@ -103,7 +103,7 @@ public class DBTableColumn extends DBCol
         this.dataMode = other.dataMode;
         this.defValue = other.defValue;
         this.attributes = new Attributes();
-        this.attributes.putAll(other.attributes);
+        this.attributes.addAll(other.attributes);
         this.options = other.options;
         if (newTable != null)
         {
@@ -190,14 +190,21 @@ public class DBTableColumn extends DBCol
         // Negative size?
         if (size<0)
         {   // For Text-Columns set attribute "SINGLEBYTECHARS"
-            if (isCharacterColumn())
+            if (isTextColumn())
+            {
                 setAttribute(DBCOLATTR_SINGLEBYTECHARS, Boolean.TRUE);
+                
+                boolean test = attributes.contains(DBCOLATTR_SINGLEBYTECHARS);
+                if (test==false)
+                    test = true;
+                
+            }    
             else
                 throw new InvalidArgumentException("size", size);
             // Remove sign
             size = Math.abs(size);
         }
-        else if (attributes!=null && attributes.containsKey(DBCOLATTR_SINGLEBYTECHARS))
+        else if (attributes!=null && attributes.contains(DBCOLATTR_SINGLEBYTECHARS))
         {   // Remove single by chars attribute
             attributes.remove(DBCOLATTR_SINGLEBYTECHARS);
         }
@@ -234,7 +241,7 @@ public class DBTableColumn extends DBCol
      */
     public boolean isSingleByteChars()
     {
-        if (attributes==null || !attributes.containsKey(DBCOLATTR_SINGLEBYTECHARS))
+        if (attributes==null || !attributes.contains(DBCOLATTR_SINGLEBYTECHARS))
             return false;
         // Check Attribute
         return ObjectUtils.getBoolean(attributes.get(DBCOLATTR_SINGLEBYTECHARS));
@@ -245,7 +252,7 @@ public class DBTableColumn extends DBCol
      */
     public void setSingleByteChars(boolean singleByteChars)
     {
-        if (!isCharacterColumn())
+        if (!isTextColumn())
             throw new NotSupportedException(this, "setSingleByteChars");
         // set single byte
         setAttribute(DBCOLATTR_SINGLEBYTECHARS, singleByteChars);
@@ -277,7 +284,7 @@ public class DBTableColumn extends DBCol
     public boolean isReadOnly()
     {
         if (attributes!=null &&
-            attributes.containsKey(DBCOLATTR_READONLY))
+            attributes.contains(DBCOLATTR_READONLY))
             return true;
         // Check DataMode
         return (dataMode==DataMode.ReadOnly || dataMode==DataMode.AutoGenerated);

Modified: empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java (original)
+++ empire-db/trunk/empire-db/src/main/java/org/apache/empire/db/DBView.java Wed Mar  7 17:27:33
2012
@@ -101,7 +101,7 @@ public abstract class DBView extends DBR
                 return true;
             // Check ReadOnly attribute 
             if (attributes!=null &&
-                attributes.containsKey(DBCOLATTR_READONLY))
+                attributes.contains(DBCOLATTR_READONLY))
                 return true;
             // AUTOINC's are read only
             return isAutoGenerated();
@@ -129,7 +129,7 @@ public abstract class DBView extends DBR
         @Override
         public Object getAttribute(String name)
         {
-            if (attributes != null && attributes.containsKey(name))
+            if (attributes != null && attributes.contains(name))
                 return attributes.get(name);
             // Otherwise ask expression
             if (updateColumn==null)

Modified: empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/AttributesTest.java
URL: http://svn.apache.org/viewvc/empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/AttributesTest.java?rev=1298026&r1=1298025&r2=1298026&view=diff
==============================================================================
--- empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/AttributesTest.java
(original)
+++ empire-db/trunk/empire-db/src/test/java/org/apache/empire/commons/AttributesTest.java
Wed Mar  7 17:27:33 2012
@@ -46,39 +46,25 @@ public class AttributesTest
 		Attributes attributes = new Attributes();
 		Object val = attributes.get("unexisting");
 		assertEquals(null, val);
-		attributes.put("test", Integer.valueOf(123));
+		attributes.set("test", Integer.valueOf(123));
 		val = attributes.get("test");
 		assertEquals(Integer.valueOf(123), val);
 	}
 
 	/**
-	 * Test method for {@link org.apache.empire.commons.Attributes#get(java.lang.Object)}.
-	 */
-	@Test
-	public void testGetObject()
-	{
-		Attributes attributes = new Attributes();
-		Object val = attributes.get(new Object());
-		assertEquals(null, val);
-		attributes.put("123", Integer.valueOf(456));
-		val = attributes.get(Integer.valueOf(123));
-		assertEquals(Integer.valueOf(456), val);
-	}
-
-	/**
 	 * Test method for {@link org.apache.empire.commons.Attributes#put(java.lang.String, java.lang.Object)}.
 	 */
 	@Test
 	public void testPutStringObject()
 	{
 		Attributes attributes = new Attributes();
-		attributes.put(null, null);
+		attributes.set(null, null);
 		assertEquals(null, attributes.get(null));
-		attributes.put("", null);
+		attributes.set("", null);
 		assertEquals(null, attributes.get(""));
-		attributes.put("test", null);
+		attributes.set("test", null);
 		assertEquals(null, attributes.get("test"));
-		attributes.put("test", Integer.valueOf(456));
+		attributes.set("test", Integer.valueOf(456));
 		assertEquals(Integer.valueOf(456), attributes.get("test"));
 	}
 



Mime
View raw message