--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// ObjectLens.java\r
+// Since: 2009/05/12 19:52:38\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import java.lang.reflect.Field;\r
+import java.lang.reflect.Method;\r
+import java.lang.reflect.Modifier;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+import java.util.regex.Matcher;\r
+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
+ public ObjectLens(Class< ? > targetType)\r
+ {\r
+ createBindRules(targetType);\r
+ }\r
+\r
+ private static void createBindRules(Class< ? > targetType)\r
+ {\r
+ List<Setter> setterContainer = new ArrayList<Setter>();\r
+\r
+ // look for all super classes\r
+ for (Class< ? > eachClass = targetType; eachClass != null; eachClass = eachClass.getSuperclass())\r
+ {\r
+ // scan public fields\r
+ for (Field eachField : eachClass.getFields())\r
+ {\r
+ int fieldModifier = eachField.getModifiers();\r
+ if (Modifier.isPublic(fieldModifier) || !Modifier.isTransient(fieldModifier))\r
+ {\r
+ Class< ? > fieldType = eachField.getType();\r
+ String paramName = getCanonicalParameterName(eachField.getName());\r
+ SetterType setterType = SetterType.SETTER;\r
+\r
+ if (TypeInformation.isArray(fieldType))\r
+ {\r
+ // ignore the array field\r
+ continue;\r
+ }\r
+ else if (TypeInformation.isMap(fieldType))\r
+ {\r
+ setterType = SetterType.PUTTER;\r
+ }\r
+ if (TypeInformation.isCollection(fieldType))\r
+ {\r
+ setterType = SetterType.ADDER;\r
+ }\r
+\r
+ setterContainer.add(new Setter(eachClass, paramName, eachField, setterType));\r
+\r
+ }\r
+\r
+ }\r
+\r
+ // scan methods\r
+ for (Method eachMethod : eachClass.getMethods())\r
+ {\r
+ String methodName = eachMethod.getName();\r
+ String parametrName = pickPropertyName(methodName);\r
+ if (methodName.startsWith("add"))\r
+ {\r
+ // adder\r
+ String paramName = getCanonicalParameterName(methodName.substring(3));\r
+ setterContainer.add(ParameterSetter.newSetter(paramName, eachMethod, SetterType.ADDER));\r
+\r
+ }\r
+ else if (methodName.startsWith("set"))\r
+ {\r
+ // setter\r
+ String paramName = getCanonicalParameterName(methodName.substring(3));\r
+\r
+ }\r
+ else if (methodName.startsWith("get"))\r
+ {\r
+ // we cannot use any getter that requires some arguments\r
+ Class< ? >[] parameterType = eachMethod.getParameterTypes();\r
+ if (parameterType.length != 0)\r
+ continue;\r
+\r
+ }\r
+ else if (methodName.startsWith("put"))\r
+ {\r
+\r
+ }\r
+ else if (methodName.startsWith("append"))\r
+ {\r
+ // appender for a large text value split into chunks\r
+\r
+ }\r
+\r
+ }\r
+\r
+ }\r
+\r
+ }\r
+\r
+ static private Pattern propertyNamePattern = Pattern.compile("^(set|get|add|put|append)((\\S)(\\S*))?");\r
+\r
+ public static String pickPropertyName(String methodName)\r
+ {\r
+ Matcher m = null;\r
+ m = propertyNamePattern.matcher(methodName);\r
+ if (!m.matches())\r
+ return null;\r
+ else\r
+ {\r
+ if (m.group(2) != null)\r
+ return m.group(3).toLowerCase() + m.group(4);\r
+ else\r
+ return "";\r
+ }\r
+ }\r
+\r
+ public static String getCanonicalParameterName(String paramName)\r
+ {\r
+ paramName = paramName.replaceAll("\\s", "_");\r
+ paramName = paramName.replaceAll("-", "_");\r
+ return paramName.toLowerCase();\r
+ }\r
+\r
+ private enum SetterType {\r
+ ADDER, SETTER, PUTTER, APPENDER\r
+ };\r
+\r
+ private static class Getter\r
+ {\r
+ Field target;\r
+ Method setter;\r
+ String paremterName;\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// ParameterSetter.java\r
+// Since: 2009/05/12 19:59:19\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import java.lang.reflect.Field;\r
+import java.lang.reflect.Method;\r
+\r
+import org.xerial.core.XerialException;\r
+import org.xerial.util.reflect.ReflectionUtil;\r
+\r
+/**\r
+ * Object setter base class.\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public abstract class ParameterSetter\r
+{\r
+ private final Class< ? > targetClass;\r
+ private final String parameterName;\r
+\r
+ public ParameterSetter(Class< ? > targetClass, String parameterName)\r
+ {\r
+ this.targetClass = targetClass;\r
+ this.parameterName = parameterName;\r
+ }\r
+\r
+ public abstract void bind(Object object, Object value) throws XerialException;\r
+\r
+ public Class< ? > getTargetClass()\r
+ {\r
+ return targetClass;\r
+ }\r
+\r
+ public String getParameterName()\r
+ {\r
+ return parameterName;\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj)\r
+ {\r
+ ParameterSetter other = ParameterSetter.class.cast(obj);\r
+ if (other == null)\r
+ return false;\r
+ return parameterName.equals(other.parameterName);\r
+ }\r
+\r
+ @Override\r
+ public int hashCode()\r
+ {\r
+ return parameterName.hashCode();\r
+ }\r
+\r
+ public static ParameterSetter newSetter(Class< ? > targetClass, String parameterName, Field targetField)\r
+ {\r
+ return new FieldSetter(targetClass, parameterName, targetField);\r
+ }\r
+\r
+ public static ParameterSetter newSetter(Class< ? > targetClass, String parameterName, Method setterMethod)\r
+ {\r
+ return new MethodSetter(targetClass, parameterName, setterMethod);\r
+ }\r
+\r
+ private static class FieldSetter extends ParameterSetter\r
+ {\r
+ private final Field targetField;\r
+\r
+ public FieldSetter(Class< ? > targetClass, String parameterName, Field targetField)\r
+ {\r
+ super(targetClass, parameterName);\r
+ this.targetField = targetField;\r
+ }\r
+\r
+ @Override\r
+ public void bind(Object object, Object value) throws XerialException\r
+ {\r
+ ReflectionUtil.setFieldValue(object, targetField, value);\r
+ }\r
+\r
+ }\r
+\r
+ private static class MethodSetter extends ParameterSetter\r
+ {\r
+ private final Method setterMethod;\r
+\r
+ public MethodSetter(Class< ? > targetClass, String parameterName, Method setterMethod)\r
+ {\r
+ super(targetClass, parameterName);\r
+ this.setterMethod = setterMethod;\r
+ }\r
+\r
+ @Override\r
+ public void bind(Object object, Object value) throws XerialException\r
+ {\r
+ ReflectionUtil.setValue(object, setterMethod, value);\r
+ }\r
+\r
+ }\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// RelationSetter.java\r
+// Since: 2009/05/12 20:14:43\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.lens;\r
+\r
+import org.xerial.core.XerialException;\r
+\r
+/**\r
+ * RelationSetter is for setting a node tuple to an object\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public abstract class RelationSetter\r
+{\r
+ public abstract void bind(Object object, Object value) throws XerialException;\r
+\r
+}\r
//--------------------------------------\r
package org.xerial.lens.impl;\r
\r
-import java.lang.reflect.Field;\r
-import java.lang.reflect.Method;\r
-import java.lang.reflect.Modifier;\r
-import java.util.ArrayList;\r
import java.util.HashMap;\r
-import java.util.List;\r
import java.util.Map;\r
import java.util.Set;\r
\r
-import org.xerial.util.bean.BeanUtil;\r
-import org.xerial.util.bean.TypeInformation;\r
-\r
public class LensGenerator\r
{\r
private static Map<Class< ? >, Set<FieldSetter>> classSetterTable = new HashMap<Class< ? >, Set<FieldSetter>>();\r
\r
- private static void createBindRules(Class< ? > targetType)\r
- {\r
- List<FieldSetter> setterContainer = new ArrayList<FieldSetter>();\r
-\r
- // look for all super classes\r
- for (Class< ? > eachClass = targetType; eachClass != null; eachClass = eachClass.getSuperclass())\r
- {\r
- // scan public fields\r
- for (Field eachField : eachClass.getFields())\r
- {\r
- int fieldModifier = eachField.getModifiers();\r
- if (Modifier.isPublic(fieldModifier) || !Modifier.isTransient(fieldModifier))\r
- {\r
- Class< ? > fieldType = eachField.getType();\r
- String paramName = getCanonicalParameterName(eachField.getName());\r
- SetterType setterType = SetterType.SETTER;\r
-\r
- if (TypeInformation.isArray(fieldType))\r
- {\r
- // ignore the array field\r
- continue;\r
- }\r
- else if (TypeInformation.isMap(fieldType))\r
- {\r
- setterType = SetterType.PUTTER;\r
- }\r
- if (TypeInformation.isCollection(fieldType))\r
- {\r
- setterType = SetterType.ADDER;\r
- }\r
-\r
- setterContainer.add(new FieldSetter(paramName, eachField, setterType));\r
-\r
- }\r
-\r
- }\r
-\r
- // scan methods\r
- for (Method eachMethod : eachClass.getMethods())\r
- {\r
- String methodName = eachMethod.getName();\r
- String parametrName = BeanUtil.pickPropertyName(methodName);\r
- if (methodName.startsWith("add"))\r
- {\r
- // adder\r
- String paramName = getCanonicalParameterName(methodName.substring(3));\r
- setterContainer.add(new FieldSetter(paramName, eachMethod, SetterType.ADDER));\r
-\r
- }\r
- else if (methodName.startsWith("set"))\r
- {\r
- // setter\r
- String paramName = getCanonicalParameterName(methodName.substring(3));\r
-\r
- }\r
- else if (methodName.startsWith("get"))\r
- {\r
- // we cannot use any getter that requires some arguments\r
- Class< ? >[] parameterType = eachMethod.getParameterTypes();\r
- if (parameterType.length != 0)\r
- continue;\r
-\r
- }\r
- else if (methodName.startsWith("put"))\r
- {\r
-\r
- }\r
- else if (methodName.startsWith("append"))\r
- {\r
- // appender for a large text value split into chunks\r
-\r
- }\r
-\r
- }\r
-\r
- }\r
-\r
- }\r
-\r
- public static String getCanonicalParameterName(String paramName)\r
- {\r
- paramName = paramName.replaceAll("\\s", "_");\r
- paramName = paramName.replaceAll("-", "_");\r
- return paramName.toLowerCase();\r
- }\r
-\r
- private enum SetterType {\r
- ADDER, SETTER, GETTER, PUTTER, APPENDER\r
- };\r
-\r
- private static class FieldSetter\r
- {\r
- SetterType type;\r
- Field target;\r
- Method seetter;\r
- String parameterName;\r
-\r
- private FieldSetter(String parameterName, Method seetter, SetterType type)\r
- {\r
- this.parameterName = parameterName;\r
- this.seetter = seetter;\r
- this.target = null;\r
- this.type = type;\r
- }\r
-\r
- private FieldSetter(String parameterName, Field target, SetterType type)\r
- {\r
- this.parameterName = parameterName;\r
- this.seetter = null;\r
- this.target = target;\r
- this.type = type;\r
- }\r
-\r
- @Override\r
- public boolean equals(Object obj)\r
- {\r
- FieldSetter other = FieldSetter.class.cast(obj);\r
- return parameterName.equals(other.seetter);\r
- }\r
-\r
- @Override\r
- public int hashCode()\r
- {\r
- return parameterName.hashCode();\r
- }\r
- }\r
-\r
}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// Cell.java\r
+// Since: 2009/05/13 9:20:19\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.relation;\r
+\r
+/**\r
+ * Table element\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+/**\r
+ * @author leo\r
+ * \r
+ */\r
+public interface Cell\r
+{\r
+ /**\r
+ * Returns true if this cell is a singleton node\r
+ * \r
+ * @return\r
+ */\r
+ public boolean isAtom();\r
+\r
+ /**\r
+ * Returns true if this cell is a node tuple\r
+ * \r
+ * @return\r
+ */\r
+ public boolean isTuple();\r
+\r
+ /**\r
+ * Get the number of elements contained in this cell. When this cell is an\r
+ * atom, the size will be 1. When a tuple, the returned size is the tuple\r
+ * size.\r
+ * \r
+ * @return\r
+ */\r
+ public int size();\r
+\r
+ /**\r
+ * Get the cell at the specified index\r
+ * \r
+ * @param index\r
+ * @return\r
+ */\r
+ Cell get(TupleIndex index);\r
+\r
+ /**\r
+ * If the cell at the specified index is a node, then return the node,\r
+ * otherwise return null\r
+ * \r
+ * @param index\r
+ * @return node\r
+ */\r
+ Node getNode(TupleIndex index);\r
+\r
+ /**\r
+ * if this cell is node, then return node. otherwise return null\r
+ * \r
+ * @return\r
+ */\r
+ Node getNode();\r
+\r
+ /**\r
+ * If this cell is tuple, then return tuple. otherwise return null\r
+ * \r
+ * @return\r
+ */\r
+ Tuple getTuple();\r
+\r
+ /**\r
+ * Accept the visitor\r
+ * \r
+ * @param visitor\r
+ */\r
+ void accept(CellVisitor visitor);\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// CellVisitor.java\r
+// Since: 2009/05/13 9:32:39\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.relation;\r
+\r
+/**\r
+ * Visitor interface for traversing Tuple\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public interface CellVisitor\r
+{\r
+ public void visitNode(Node node);\r
+\r
+ public void visitTuple(Tuple tuple);\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// Node.java\r
+// Since: 2009/05/13 9:18:34\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.relation;\r
+\r
+/**\r
+ * Node is a relation element.\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public class Node implements Cell\r
+{\r
+\r
+ public boolean isAtom()\r
+ {\r
+ return true;\r
+ }\r
+\r
+ public boolean isTuple()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ /**\r
+ * \r
+ * @return\r
+ */\r
+ public int size()\r
+ {\r
+ return 1;\r
+ }\r
+\r
+ public void accept(CellVisitor visitor)\r
+ {\r
+ visitor.visitNode(this);\r
+ }\r
+\r
+ public Cell get(TupleIndex index)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ public Node getNode(TupleIndex index)\r
+ {\r
+ // TODO Auto-generated method stub\r
+ return null;\r
+ }\r
+\r
+ public Node getNode()\r
+ {\r
+ return this;\r
+ }\r
+\r
+ public Tuple getTuple()\r
+ {\r
+ return null;\r
+ }\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// Tuple.java\r
+// Since: 2009/05/13 9:19:34\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.relation;\r
+\r
+import java.util.ArrayList;\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
+\r
+ public Tuple()\r
+ {\r
+ this.tuple = EMPTY_TUPLE;\r
+ }\r
+\r
+ public boolean isAtom()\r
+ {\r
+ return false;\r
+ }\r
+\r
+ public boolean isTuple()\r
+ {\r
+ return true;\r
+ }\r
+\r
+ public int size()\r
+ {\r
+ return tuple.size();\r
+ }\r
+\r
+ public void accept(CellVisitor visitor)\r
+ {\r
+ visitor.visitTuple(this);\r
+ }\r
+\r
+ public Cell get(TupleIndex index)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ public Node getNode(TupleIndex index)\r
+ {\r
+ return null;\r
+ }\r
+\r
+ public Node getNode()\r
+ {\r
+ return null;\r
+ }\r
+\r
+ public Tuple getTuple()\r
+ {\r
+ return this;\r
+ }\r
+\r
+}\r
--- /dev/null
+/*--------------------------------------------------------------------------\r
+ * Copyright 2009 Taro L. Saito\r
+ *\r
+ * Licensed under the Apache License, Version 2.0 (the "License");\r
+ * you may not use this file except in compliance with the License.\r
+ * You may obtain a copy of the License at\r
+ *\r
+ * http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ * Unless required by applicable law or agreed to in writing, software\r
+ * distributed under the License is distributed on an "AS IS" BASIS,\r
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ * See the License for the specific language governing permissions and\r
+ * limitations under the License.\r
+ *--------------------------------------------------------------------------*/\r
+//--------------------------------------\r
+// XerialJ\r
+//\r
+// TupleIndex.java\r
+// Since: 2009/05/13 9:29:15\r
+//\r
+// $URL$\r
+// $Author$\r
+//--------------------------------------\r
+package org.xerial.relation;\r
+\r
+import org.xerial.core.XerialError;\r
+import org.xerial.core.XerialErrorCode;\r
+\r
+/**\r
+ * Dot-separated number for specifying a position in a node tuple, e.g., 1, 1.1,\r
+ * 1.2, ...etc.\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
+public class TupleIndex implements Comparable<TupleIndex>\r
+{\r
+ private int[] index;\r
+\r
+ /**\r
+ * Create an index with a single level, e.g., 1, 2, 3, ...\r
+ * \r
+ * @param baseIndex\r
+ */\r
+ public TupleIndex(int baseIndex)\r
+ {\r
+ this.index = new int[1];\r
+ this.index[0] = baseIndex;\r
+ }\r
+\r
+ /**\r
+ * Create an index based on a parent index, e.g, 1.1, 1.2, 1.1.1, 1.1.2,\r
+ * ..etc.\r
+ * \r
+ * @param parent\r
+ * @param childIndex\r
+ */\r
+ public TupleIndex(TupleIndex parent, int childIndex)\r
+ {\r
+ if (parent == null)\r
+ {\r
+ this.index = new int[1];\r
+ this.index[0] = childIndex;\r
+ }\r
+ else\r
+ {\r
+ this.index = new int[parent.size() + 1];\r
+ for (int i = 0; i < parent.size(); ++i)\r
+ index[i] = parent.get(i);\r
+ index[parent.size()] = childIndex;\r
+ }\r
+ }\r
+\r
+ public static TupleIndex parse(String indexStr)\r
+ {\r
+ if (indexStr == null)\r
+ throw new NullPointerException("indexStr");\r
+\r
+ String[] component = indexStr.split("\\.");\r
+ if (component == null)\r
+ throw new XerialError(XerialErrorCode.INVALID_INPUT, "invalid format:" + indexStr);\r
+\r
+ int[] index = new int[component.length];\r
+ for (int i = 0; i < component.length; ++i)\r
+ index[i] = Integer.parseInt(component[i]);\r
+ return new TupleIndex(index);\r
+ }\r
+\r
+ private TupleIndex(int[] index)\r
+ {\r
+ this.index = index;\r
+ }\r
+\r
+ public TupleIndex parent()\r
+ {\r
+ if (index.length <= 1)\r
+ return null;\r
+\r
+ int[] newIndex = new int[index.length - 1];\r
+ for (int i = 0; i < index.length - 1; ++i)\r
+ newIndex[i] = index[i];\r
+ return new TupleIndex(newIndex);\r
+ }\r
+\r
+ public TupleIndex tail()\r
+ {\r
+ if (index.length < 2)\r
+ return null;\r
+\r
+ int[] newIndex = new int[index.length - 1];\r
+ for (int i = 1; i < index.length; ++i)\r
+ newIndex[i - 1] = index[i];\r
+ return new TupleIndex(newIndex);\r
+ }\r
+\r
+ public TupleIndex sibling()\r
+ {\r
+ int[] newIndex = new int[index.length];\r
+ for (int i = 0; i < index.length; ++i)\r
+ newIndex[i] = index[i];\r
+ newIndex[index.length - 1]++;\r
+ return new TupleIndex(newIndex);\r
+ }\r
+\r
+ /**\r
+ * nested level\r
+ * \r
+ * @return nested level\r
+ */\r
+ public int size()\r
+ {\r
+ return index.length;\r
+ }\r
+\r
+ public boolean hasParent()\r
+ {\r
+ return index.length > 1;\r
+ }\r
+\r
+ public boolean hasTail()\r
+ {\r
+ return index.length > 1;\r
+ }\r
+\r
+ /**\r
+ * Get the index on the specified level\r
+ * \r
+ * @param level\r
+ * @return\r
+ */\r
+ public int get(int level)\r
+ {\r
+ return index[level];\r
+ }\r
+\r
+ @Override\r
+ public String toString()\r
+ {\r
+ StringBuilder buf = new StringBuilder();\r
+ for (int i = 0; i < index.length; i++)\r
+ {\r
+ if (i != 0)\r
+ buf.append(".");\r
+ buf.append(index[i]);\r
+ }\r
+ return buf.toString();\r
+ }\r
+\r
+ @Override\r
+ public boolean equals(Object obj)\r
+ {\r
+ TupleIndex other = TupleIndex.class.cast(obj);\r
+ if (this.index.length != other.index.length)\r
+ return false;\r
+\r
+ for (int i = 0; i < this.index.length; ++i)\r
+ {\r
+ if (this.index[i] != other.index[i])\r
+ return false;\r
+ }\r
+ return true;\r
+\r
+ }\r
+\r
+ public int compareTo(TupleIndex other)\r
+ {\r
+ int i1 = 0;\r
+ int i2 = 0;\r
+ int cmp = 0;\r
+ while (i1 < this.index.length && i2 < other.index.length)\r
+ {\r
+ int e1 = this.index[i1];\r
+ int e2 = other.index[i2];\r
+ cmp = e1 - e2;\r
+ if (cmp != 0)\r
+ return cmp;\r
+\r
+ i1++;\r
+ i2++;\r
+ }\r
+ if (i1 < this.index.length)\r
+ return 1;\r
+ else\r
+ {\r
+ if (i2 < other.index.length)\r
+ return -1;\r
+ else\r
+ return 0;\r
+ }\r
+ }\r
+\r
+ @Override\r
+ public int hashCode()\r
+ {\r
+ int hash = 3;\r
+ for (int i = 0; i < index.length; ++i)\r
+ hash += 137 * index[i];\r
+ return hash / 1987;\r
+ }\r
+\r
+}\r
//--------------------------------------
// XerialJ
//
-// Setter.java
+// ParameterSetter.java
// Since: Aug 9, 2007 9:42:31 AM
//
// $URL$
\r
import java.util.List;\r
\r
+/**\r
+ * TreeNode\r
+ * \r
+ * @author leo\r
+ * \r
+ */\r
public interface TreeNode\r
{\r
public List<TreeNode> getChildren();\r
//--------------------------------------\r
package org.xerial.lens.impl;\r
\r
-import static org.junit.Assert.assertEquals;\r
-\r
import org.junit.After;\r
import org.junit.Before;\r
import org.junit.Test;\r
@Test\r
public void testGetCanonicalParameterName()\r
{\r
- assertEquals("param", LensGenerator.getCanonicalParameterName("Param"));\r
- assertEquals("param_1", LensGenerator.getCanonicalParameterName("Param 1"));\r
- assertEquals("param_1", LensGenerator.getCanonicalParameterName("Param-1"));\r
+ // assertEquals("param", LensGenerator.getCanonicalParameterName("Param"));\r
+ //assertEquals("param_1", LensGenerator.getCanonicalParameterName("Param 1"));\r
+ //assertEquals("param_1", LensGenerator.getCanonicalParameterName("Param-1"));\r
}\r
\r
}\r