OSDN Git Service

git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3282 ae02f08e...
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 13 May 2009 10:03:22 +0000 (10:03 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Wed, 13 May 2009 10:03:22 +0000 (10:03 +0000)
src/main/java/org/xerial/lens/ObjectLens.java
src/main/java/org/xerial/relation/FD.java [new file with mode: 0644]
src/main/java/org/xerial/relation/Node.java
src/main/java/org/xerial/relation/Tuple.java
src/main/java/org/xerial/relation/schema/RelationalSchema.java [new file with mode: 0644]
src/test/java/org/xerial/lens/LensTest.java

index e171fcb..58782bf 100644 (file)
@@ -33,7 +33,6 @@ import java.util.regex.Matcher;
 import java.util.regex.Pattern;\r
 \r
 import org.xerial.util.bean.TypeInformation;\r
-import org.xerial.util.bean.impl.Setter;\r
 \r
 public class ObjectLens\r
 {\r
@@ -44,7 +43,7 @@ public class ObjectLens
 \r
     private static void createBindRules(Class< ? > targetType)\r
     {\r
-        List<Setter> setterContainer = new ArrayList<Setter>();\r
+        List<ParameterSetter> setterContainer = new ArrayList<ParameterSetter>();\r
 \r
         // look for all super classes\r
         for (Class< ? > eachClass = targetType; eachClass != null; eachClass = eachClass.getSuperclass())\r
@@ -73,7 +72,7 @@ public class ObjectLens
                         setterType = SetterType.ADDER;\r
                     }\r
 \r
-                    setterContainer.add(new Setter(eachClass, paramName, eachField, setterType));\r
+                    setterContainer.add(ParameterSetter.newSetter(eachClass, paramName, eachField));\r
 \r
                 }\r
 \r
@@ -88,7 +87,7 @@ public class ObjectLens
                 {\r
                     // adder\r
                     String paramName = getCanonicalParameterName(methodName.substring(3));\r
-                    setterContainer.add(ParameterSetter.newSetter(paramName, eachMethod, SetterType.ADDER));\r
+                    setterContainer.add(ParameterSetter.newSetter(eachClass, paramName, eachMethod));\r
 \r
                 }\r
                 else if (methodName.startsWith("set"))\r
@@ -132,7 +131,7 @@ public class ObjectLens
         else\r
         {\r
             if (m.group(2) != null)\r
-                return m.group(3).toLowerCase() + m.group(4);\r
+                return getCanonicalParameterName(m.group(3) + m.group(4));\r
             else\r
                 return "";\r
         }\r
@@ -140,8 +139,8 @@ public class ObjectLens
 \r
     public static String getCanonicalParameterName(String paramName)\r
     {\r
-        paramName = paramName.replaceAll("\\s", "_");\r
-        paramName = paramName.replaceAll("-", "_");\r
+        paramName = paramName.replaceAll("\\s", "");\r
+        paramName = paramName.replaceAll("-", "");\r
         return paramName.toLowerCase();\r
     }\r
 \r
diff --git a/src/main/java/org/xerial/relation/FD.java b/src/main/java/org/xerial/relation/FD.java
new file mode 100644 (file)
index 0000000..e899d3e
--- /dev/null
@@ -0,0 +1,59 @@
+/*--------------------------------------------------------------------------
+ *  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
+//
+// FD.java
+// Since: May 13, 2009 5:39:16 PM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.relation;
+
+/**
+ * Functional dependency that holds in the relation
+ * 
+ * @author leo
+ * 
+ */
+public enum FD {
+    ONE_TO_ONE("."), ONE_OR_MORE("+"), ZERO_OR_ONE("?"), ZERO_OR_MORE("*");
+
+    private final String symbol;
+
+    private FD(String displaySymbol)
+    {
+        this.symbol = displaySymbol;
+    }
+
+    public boolean isOneToOne()
+    {
+        return this == ONE_TO_ONE;
+    }
+
+    public boolean isOneToMany()
+    {
+        return this != ONE_TO_ONE;
+    }
+
+    @Override
+    public String toString()
+    {
+        return symbol;
+    }
+
+}
index f8c82e1..7a1e4b6 100644 (file)
 //--------------------------------------\r
 package org.xerial.relation;\r
 \r
+import org.xerial.core.XerialError;\r
+import org.xerial.core.XerialErrorCode;\r
+\r
 /**\r
- * Node is a relation element.\r
+ * Node is an element ({@link Cell}) of a relation.\r
  * \r
  * @author leo\r
  * \r
  */\r
 public class Node implements Cell\r
 {\r
+    public static final int INVALID_ID = -1;\r
+    public static final String NULL_TEXT = null;\r
+\r
+    public final int nodeID;\r
+    public final String nodeName;\r
+    public final String nodeValue;\r
+\r
+    private Node(String nodeName, int nodeID, String nodeValue)\r
+    {\r
+        this.nodeID = nodeID;\r
+        this.nodeName = nodeName;\r
+        this.nodeValue = nodeValue;\r
+    }\r
+\r
+    /**\r
+     * Builder class for {@link Node}\r
+     * \r
+     * @author leo\r
+     * \r
+     */\r
+    public static class NodeBuilder\r
+    {\r
+        private int nodeID = INVALID_ID;\r
+        private String nodeValue = NULL_TEXT;\r
+        private final String nodeName;\r
+\r
+        public NodeBuilder(String nodeName)\r
+        {\r
+            this.nodeName = nodeName;\r
+        }\r
+\r
+        public NodeBuilder(Node node)\r
+        {\r
+            this.nodeID = node.nodeID;\r
+            this.nodeName = node.nodeName;\r
+            this.nodeValue = node.nodeValue;\r
+        }\r
+\r
+        public NodeBuilder nodeID(int nodeID)\r
+        {\r
+            this.nodeID = nodeID;\r
+            return this;\r
+        }\r
+\r
+        public NodeBuilder nodeValue(String nodeValue)\r
+        {\r
+            this.nodeValue = nodeValue;\r
+            return this;\r
+        }\r
+\r
+        public Node build()\r
+        {\r
+            return new Node(nodeName, nodeID, nodeValue);\r
+        }\r
+\r
+    }\r
 \r
     public boolean isAtom()\r
     {\r
@@ -43,7 +102,32 @@ public class Node implements Cell
         return false;\r
     }\r
 \r
+    public static Node newNode(String nodeName, int nodeID)\r
+    {\r
+        return new Node(nodeName, nodeID, null);\r
+    }\r
+\r
+    public static Node newNodeWithValue(String nodeName, int nodeID, String nodeValue)\r
+    {\r
+        return new Node(nodeName, nodeID, nodeValue);\r
+    }\r
+\r
+    @Override\r
+    public String toString()\r
+    {\r
+        StringBuilder builder = new StringBuilder();\r
+        builder.append(String.format("%s(%d)", nodeName, nodeID));\r
+        if (nodeValue != null)\r
+        {\r
+            builder.append("=\"");\r
+            builder.append(nodeValue);\r
+            builder.append("\"");\r
+        }\r
+        return builder.toString();\r
+    }\r
+\r
     /**\r
+     * Always return 1\r
      * \r
      * @return\r
      */\r
@@ -59,13 +143,18 @@ public class Node implements Cell
 \r
     public Cell get(TupleIndex index)\r
     {\r
-        return null;\r
+        if (index.size() == 0 && index.get(0) == 0)\r
+            return this;\r
+        else\r
+            return null;\r
     }\r
 \r
     public Node getNode(TupleIndex index)\r
     {\r
-        // TODO Auto-generated method stub\r
-        return null;\r
+        if (!(index.size() == 1 && index.get(0) == 0))\r
+            throw new XerialError(XerialErrorCode.INVALID_STATE);\r
+        else\r
+            return this;\r
     }\r
 \r
     public Node getNode()\r
@@ -75,7 +164,7 @@ public class Node implements Cell
 \r
     public Tuple getTuple()\r
     {\r
-        return null;\r
+        throw new XerialError(XerialErrorCode.UNSUPPORTED);\r
     }\r
 \r
 }\r
index 8ae4a60..7e4306c 100644 (file)
 package org.xerial.relation;\r
 \r
 import java.util.ArrayList;\r
+import java.util.Collection;\r
+import java.util.Collections;\r
+import java.util.Comparator;\r
+import java.util.Iterator;\r
+import java.util.List;\r
 \r
+import org.xerial.core.XerialError;\r
+import org.xerial.core.XerialErrorCode;\r
+\r
+/**\r
+ * Tuple is a list of {@link Cell}s. Tuple class allows Non-1NF representation\r
+ * of the data.\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
 public class Tuple implements Cell\r
 {\r
-    private final static ArrayList<Cell> EMPTY_TUPLE = new ArrayList<Cell>(0);\r
 \r
-    private final ArrayList<Cell> tuple;\r
+    private final List<Cell> nodeList;\r
 \r
     public Tuple()\r
     {\r
-        this.tuple = EMPTY_TUPLE;\r
+        this.nodeList = new ArrayList<Cell>();\r
     }\r
 \r
-    public boolean isAtom()\r
+    public Tuple(Tuple other)\r
     {\r
-        return false;\r
+        this(other.nodeList);\r
     }\r
 \r
-    public boolean isTuple()\r
+    public Tuple(int tupleSize)\r
     {\r
-        return true;\r
+        this.nodeList = new ArrayList<Cell>(tupleSize);\r
+    }\r
+\r
+    public Tuple(List<Cell> nodeList)\r
+    {\r
+        this.nodeList = new ArrayList<Cell>(nodeList.size());\r
+        for (Cell each : nodeList)\r
+        {\r
+            this.nodeList.add(each);\r
+        }\r
+    }\r
+\r
+    //    public static Tuple emptyTuple(Schema schema)\r
+    //    {\r
+    //        List<Cell> nodeList = new ArrayList<Cell>(schema.size());\r
+    //        for (int i = 0; i < schema.size(); i++)\r
+    //        {\r
+    //            Schema subSchema = schema.get(i);\r
+    //            if (subSchema.isAtom())\r
+    //                nodeList.add(null);\r
+    //            else\r
+    //                nodeList.add(emptyTuple(subSchema));\r
+    //        }\r
+    //        return new Tuple(nodeList);\r
+    //    }\r
+\r
+    public void add(Cell node)\r
+    {\r
+        nodeList.add(node);\r
+    }\r
+\r
+    public void set(int index, Cell node)\r
+    {\r
+        nodeList.set(index, node);\r
+    }\r
+\r
+    public void set(TupleIndex index, Cell node)\r
+    {\r
+        if (!index.hasTail())\r
+        {\r
+            set(index.get(0), node);\r
+            return;\r
+        }\r
+\r
+        // nested node\r
+        Cell target = get(index.get(0));\r
+        if (target == null || !target.isTuple())\r
+            throw new XerialError(XerialErrorCode.INVALID_STATE, String.format(\r
+                    "set to invalid element: index = %s in %s", index, this));\r
+\r
+        ((Tuple) target).set(index.tail(), node);\r
     }\r
 \r
     public int size()\r
     {\r
-        return tuple.size();\r
+        return nodeList.size();\r
     }\r
 \r
-    public void accept(CellVisitor visitor)\r
+    public void clear()\r
     {\r
-        visitor.visitTuple(this);\r
+        nodeList.clear();\r
+    }\r
+\r
+    public boolean isEmpty()\r
+    {\r
+        return nodeList.isEmpty();\r
+    }\r
+\r
+    public void sort(Comparator<Cell> comparator)\r
+    {\r
+        Collections.sort(nodeList, comparator);\r
+    }\r
+\r
+    public Iterator<Cell> iterator()\r
+    {\r
+        return nodeList.iterator();\r
+    }\r
+\r
+    public Cell get(int index)\r
+    {\r
+        return nodeList.get(index);\r
+    }\r
+\r
+    private static <T> String join(Collection<T> c, String concatinator)\r
+    {\r
+        if (c == null)\r
+            return "";\r
+        int size = c.size();\r
+        if (size == 0)\r
+            return "";\r
+\r
+        Iterator<T> it = c.iterator();\r
+        StringBuilder buf = new StringBuilder();\r
+        for (int i = 0; it.hasNext() && i < size - 1; i++)\r
+        {\r
+            Object data = it.next();\r
+            if (data != null)\r
+                buf.append(data.toString());\r
+            else\r
+                buf.append("null");\r
+            buf.append(concatinator);\r
+        }\r
+        Object lastData = it.next();\r
+        if (lastData != null)\r
+            buf.append(lastData.toString());\r
+        else\r
+            buf.append("null");\r
+        return buf.toString();\r
+    }\r
+\r
+    @Override\r
+    public String toString()\r
+    {\r
+        return String.format("[%s]", join(nodeList, ", "));\r
+    }\r
+\r
+    public boolean addAll(List<Cell> relationFragment)\r
+    {\r
+        return nodeList.addAll(relationFragment);\r
+    }\r
+\r
+    public Node getNode()\r
+    {\r
+        throw new XerialError(XerialErrorCode.UNSUPPORTED);\r
+    }\r
+\r
+    public List<Cell> getNodeList()\r
+    {\r
+        return nodeList;\r
+    }\r
+\r
+    public boolean isAtom()\r
+    {\r
+        return true;\r
+    }\r
+\r
+    public boolean isTuple()\r
+    {\r
+        return true;\r
     }\r
 \r
     public Cell get(TupleIndex index)\r
     {\r
-        return null;\r
+        Cell cell = nodeList.get(index.get(0));\r
+        if (index.hasTail())\r
+            return cell.get(index.tail());\r
+        else\r
+            return cell;\r
+    }\r
+\r
+    public Node getNode(int index)\r
+    {\r
+        Cell node = get(index);\r
+        if (node.isAtom())\r
+            return Node.class.cast(node);\r
+        else\r
+            throw new XerialError(XerialErrorCode.MISSING_ELEMENT, "node is not found: " + index);\r
     }\r
 \r
     public Node getNode(TupleIndex index)\r
     {\r
-        return null;\r
+        Cell node = get(index);\r
+        if (node == null)\r
+            return null;\r
+\r
+        if (node.isAtom())\r
+            return Node.class.cast(node);\r
+        else\r
+            throw new XerialError(XerialErrorCode.MISSING_ELEMENT, "node is not found: " + index);\r
+\r
     }\r
 \r
-    public Node getNode()\r
+    public void accept(CellVisitor visitor)\r
     {\r
-        return null;\r
+        visitor.visitTuple(this);\r
     }\r
 \r
     public Tuple getTuple()\r
diff --git a/src/main/java/org/xerial/relation/schema/RelationalSchema.java b/src/main/java/org/xerial/relation/schema/RelationalSchema.java
new file mode 100644 (file)
index 0000000..d85f84c
--- /dev/null
@@ -0,0 +1,36 @@
+/*--------------------------------------------------------------------------
+ *  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
+//
+// RelationalSchema.java
+// Since: May 13, 2009 6:03:45 PM
+//
+// $URL$
+// $Author$
+//--------------------------------------
+package org.xerial.relation.schema;
+
+/**
+ * non-1NF relational schema
+ * 
+ * @author leo
+ * 
+ */
+public class RelationalSchema
+{
+
+}
index 7ed29f7..4be32a7 100644 (file)
@@ -54,7 +54,7 @@ public class LensTest
     {
         private HashedArrayList<Coordinate, Gene> sequenceTable;
 
-        public void add(Coordinate coordinate, Gene gene)
+        public void addCoordinate_Gene(Coordinate coordinate, Gene gene)
         {
             sequenceTable.put(coordinate, gene);
         }