OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3285 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 14 May 2009 03:17:34 +0000 (03:17 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Thu, 14 May 2009 03:17:34 +0000 (03:17 +0000)
src/main/java/org/xerial/lens/ParameterSetter.java
src/main/java/org/xerial/relation/CellVisitor.java
src/main/java/org/xerial/relation/DataType.java [new file with mode: 0644]
src/main/java/org/xerial/relation/LNode.java [new file with mode: 0644]
src/main/java/org/xerial/relation/NodeBase.java
src/main/java/org/xerial/relation/Tuple.java
src/main/java/org/xerial/relation/TupleCell.java
src/main/java/org/xerial/relation/schema/RelationalSchema.java
src/main/java/org/xerial/relation/schema/SchemaNode.java
src/test/java/org/xerial/relation/schema/SchemaNodeTest.java [new file with mode: 0644]

index b09d4f0..9fc3df0 100644 (file)
@@ -92,6 +92,9 @@ public abstract class ParameterSetter
         {\r
             super(targetClass, parameterName);\r
             this.targetField = targetField;\r
+\r
+            if (!targetField.isAccessible())\r
+                targetField.setAccessible(true);\r
         }\r
 \r
         @Override\r
index 02febb3..4eca1dc 100644 (file)
@@ -34,6 +34,6 @@ public interface CellVisitor<NodeType>
 {\r
     public void visitNode(NodeType node);\r
 \r
-    public void visitTuple(IndexAccess<NodeType> tuple);\r
+    public void visitTuple(Tuple<NodeType> tuple);\r
 \r
 }\r
diff --git a/src/main/java/org/xerial/relation/DataType.java b/src/main/java/org/xerial/relation/DataType.java
new file mode 100644 (file)
index 0000000..7c8faf3
--- /dev/null
@@ -0,0 +1,37 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  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.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// DataType.java
+// Since: May 14, 2009 11:26:50 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.relation;
+
+/**
+ * Definition of the data types for schema node
+ * 
+ * @author leo
+ * 
+ */
+public enum DataType {
+
+    BOOL, BYTE, I16, I32, I64, STRING, DOUBLE, BINARY;
+
+}
diff --git a/src/main/java/org/xerial/relation/LNode.java b/src/main/java/org/xerial/relation/LNode.java
new file mode 100644 (file)
index 0000000..b5857c8
--- /dev/null
@@ -0,0 +1,99 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  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.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// LNode.java
+// Since: May 14, 2009 11:36:59 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.relation;
+
+/**
+ * Light-weight node that holds only a node ID and node value.
+ * 
+ * @author leo
+ * 
+ */
+public class LNode extends NodeBase<LNode>
+{
+    private final static int INVALID_ID = -1;
+    private final static String NULL_TEXT = null;
+
+    public final int nodeID;
+    public final String nodeValue;
+
+    public LNode(int nodeID, String nodeValue)
+    {
+        this.nodeID = nodeID;
+        this.nodeValue = nodeValue;
+    }
+
+    /**
+     * Builder class for {@link Node}
+     * 
+     * @author leo
+     * 
+     */
+    public static class NodeBuilder
+    {
+        private int nodeID = INVALID_ID;
+        private String nodeValue = NULL_TEXT;
+
+        public NodeBuilder()
+        {}
+
+        public NodeBuilder(Node node)
+        {
+            this.nodeID = node.nodeID;
+            this.nodeValue = node.nodeValue;
+        }
+
+        public NodeBuilder nodeID(int nodeID)
+        {
+            this.nodeID = nodeID;
+            return this;
+        }
+
+        public NodeBuilder nodeValue(String nodeValue)
+        {
+            this.nodeValue = nodeValue;
+            return this;
+        }
+
+        public LNode build()
+        {
+            return new LNode(nodeID, nodeValue);
+        }
+
+    }
+
+    @Override
+    public String toString()
+    {
+        StringBuilder buf = new StringBuilder();
+        buf.append(nodeID);
+        if (nodeValue != null)
+        {
+            buf.append(":");
+            buf.append(nodeValue);
+        }
+        return buf.toString();
+    }
+
+}
index 76e5efb..80660b0 100644 (file)
@@ -35,7 +35,7 @@ import org.xerial.core.XerialErrorCode;
  * \r
  * @param <NodeType>\r
  */\r
-public abstract class NodeBase<NodeType> implements TupleCell<NodeType, IndexAccess<NodeType>>\r
+public abstract class NodeBase<NodeType> implements TupleCell<NodeType>\r
 {\r
     protected NodeBase()\r
     {}\r
index 60645d4..41a2089 100644 (file)
@@ -41,14 +41,14 @@ import org.xerial.core.XerialErrorCode;
  * @author leo\r
  * \r
  */\r
-public class Tuple<NodeType> implements IndexAccess<NodeType>\r
+public class Tuple<NodeType> implements TupleCell<NodeType>, Iterable<TupleCell<NodeType>>\r
 {\r
 \r
-    private final List<TupleCell<NodeType, Tuple<NodeType>>> nodeList;\r
+    private final List<TupleCell<NodeType>> nodeList;\r
 \r
     public Tuple()\r
     {\r
-        this.nodeList = new ArrayList<TupleCell<NodeType, Tuple<NodeType>>>();\r
+        this.nodeList = new ArrayList<TupleCell<NodeType>>();\r
     }\r
 \r
     public Tuple(Tuple<NodeType> other)\r
@@ -58,13 +58,13 @@ public class Tuple<NodeType> implements IndexAccess<NodeType>
 \r
     public Tuple(int tupleSize)\r
     {\r
-        this.nodeList = new ArrayList<TupleCell<NodeType, Tuple<NodeType>>>(tupleSize);\r
+        this.nodeList = new ArrayList<TupleCell<NodeType>>(tupleSize);\r
     }\r
 \r
-    public Tuple(List<TupleCell<NodeType, Tuple<NodeType>>> nodeList)\r
+    public Tuple(List<TupleCell<NodeType>> nodeList)\r
     {\r
-        this.nodeList = new ArrayList<TupleCell<NodeType, Tuple<NodeTpe>>>(nodeList.size());\r
-        for (TupleCell<NodeType, Tuple<NodeType>> each : nodeList)\r
+        this.nodeList = new ArrayList<TupleCell<NodeType>>(nodeList.size());\r
+        for (TupleCell<NodeType> each : nodeList)\r
         {\r
             this.nodeList.add(each);\r
         }\r
@@ -232,14 +232,14 @@ public class Tuple<NodeType> implements IndexAccess<NodeType>
 \r
     }\r
 \r
-    public void accept(CellVisitor visitor)\r
+    public Tuple<NodeType> getTuple()\r
     {\r
-        visitor.visitTuple(this);\r
+        return this;\r
     }\r
 \r
-    public Tuple<NodeType> getTuple()\r
+    public void accept(CellVisitor<NodeType> visitor)\r
     {\r
-        return this;\r
+        visitor.visitTuple(this);\r
     }\r
 \r
 }\r
index 580572b..c26fc51 100644 (file)
@@ -37,14 +37,14 @@ package org.xerial.relation;
 public interface TupleCell<NodeType>\r
 {\r
     /**\r
-     * Returns true if this cell is a singleton node\r
+     * Returns true if this cell is a singleton\r
      * \r
      * @return\r
      */\r
     public boolean isNode();\r
 \r
     /**\r
-     * Returns true if this cell is a node tuple\r
+     * Returns true if this cell is a tuple\r
      * \r
      * @return\r
      */\r
@@ -84,6 +84,13 @@ public interface TupleCell<NodeType>
     NodeType getNode();\r
 \r
     /**\r
+     * If this cell is tuple, then return tuple. otherwise return null\r
+     * \r
+     * @return\r
+     */\r
+    Tuple<NodeType> getTuple();\r
+\r
+    /**\r
      * Accept the visitor\r
      * \r
      * @param visitor\r
index a9676b8..497bd36 100644 (file)
@@ -32,7 +32,21 @@ import org.xerial.relation.Tuple;
  * @author leo
  * 
  */
-public class RelationalSchema extends Tuple<SchemaNode>
+public class RelationalSchema
 {
+    public final String name;
+    public final Tuple<SchemaNode> schema;
+
+    public RelationalSchema(String name, Tuple<SchemaNode> schema)
+    {
+        this.name = name;
+        this.schema = schema;
+    }
+
+    @Override
+    public String toString()
+    {
+        return String.format("schema %s %s", name, schema);
+    }
 
 }
index e02f84e..51b7c37 100644 (file)
@@ -24,6 +24,9 @@
 //--------------------------------------\r
 package org.xerial.relation.schema;\r
 \r
+import java.io.Serializable;\r
+\r
+import org.xerial.relation.DataType;\r
 import org.xerial.relation.NodeBase;\r
 \r
 /**\r
@@ -32,13 +35,20 @@ import org.xerial.relation.NodeBase;
  * @author leo\r
  * \r
  */\r
-public class SchemaNode extends NodeBase<SchemaNode>\r
+public class SchemaNode extends NodeBase<SchemaNode> implements Serializable\r
 {\r
-    private String name;\r
+    /**\r
+     * \r
+     */\r
+    private static final long serialVersionUID = 1L;\r
+\r
+    public final String name;\r
+    public final DataType type;\r
 \r
-    public SchemaNode(String name)\r
+    public SchemaNode(String name, DataType type)\r
     {\r
         this.name = name;\r
+        this.type = type;\r
     }\r
 \r
     public String getName()\r
@@ -46,4 +56,10 @@ public class SchemaNode extends NodeBase<SchemaNode>
         return name;\r
     }\r
 \r
+    @Override\r
+    public String toString()\r
+    {\r
+        return String.format("%s %s", name, type.name().toLowerCase());\r
+    }\r
+\r
 }\r
diff --git a/src/test/java/org/xerial/relation/schema/SchemaNodeTest.java b/src/test/java/org/xerial/relation/schema/SchemaNodeTest.java
new file mode 100644 (file)
index 0000000..7194b20
--- /dev/null
@@ -0,0 +1,80 @@
+/*--------------------------------------------------------------------------
+ *  Copyright 2009 Taro L. Saito
+ *
+ *  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.
+ *--------------------------------------------------------------------------*/
+//--------------------------------------
+// XerialJ
+//
+// SchemaNodeTest.java
+// Since: May 14, 2009 11:51:53 AM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.relation.schema;
+
+import static org.junit.Assert.*;
+
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.lang.reflect.Field;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.xerial.relation.DataType;
+
+public class SchemaNodeTest
+{
+
+    @Before
+    public void setUp() throws Exception
+    {}
+
+    @After
+    public void tearDown() throws Exception
+    {}
+
+    @Test
+    public void objectSerialization() throws Exception
+    {
+        ByteArrayOutputStream buf = new ByteArrayOutputStream();
+        ObjectOutputStream out = new ObjectOutputStream(buf);
+
+        SchemaNode s = new SchemaNode("id", DataType.I64);
+        out.writeObject(s);
+        out.close();
+
+        ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(buf.toByteArray()));
+        SchemaNode s2 = (SchemaNode) in.readObject();
+
+        assertEquals(s.name, s2.name);
+        assertEquals(s.type, s2.type);
+    }
+
+    @Test
+    public void overcomeFinal() throws Exception
+    {
+        SchemaNode s = new SchemaNode("id", DataType.I64);
+        Field f = s.getClass().getField("name");
+        f.setAccessible(true);
+        f.set(s, "hello");
+
+        assertEquals("hello", s.name);
+
+    }
+
+}