METAMODEL-244: Made ColumnNamingStrategy available to fixedwidth module
authorkaspersorensen <i.am.kasper.sorensen@gmail.com>
Fri, 22 Apr 2016 21:54:22 +0000 (14:54 -0700)
committerkaspersorensen <i.am.kasper.sorensen@gmail.com>
Fri, 22 Apr 2016 21:54:22 +0000 (14:54 -0700)
core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.java [new file with mode: 0644]
core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContext.java [new file with mode: 0644]
core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContextImpl.java [new file with mode: 0644]
core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java [new file with mode: 0644]
fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java

diff --git a/core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.java b/core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.java
new file mode 100644 (file)
index 0000000..284e851
--- /dev/null
@@ -0,0 +1,36 @@
+/**\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  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,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+package org.apache.metamodel.schema.builder;\r
+\r
+import org.apache.metamodel.util.AlphabeticSequence;\r
+\r
+public class AlphabeticColumnNamingStrategy implements ColumnNamingStrategy {\r
+\r
+    @Override\r
+    public String getColumnName(ColumnNamingContext ctx) {\r
+        final int columnIndex = ctx.getColumnIndex();\r
+        final AlphabeticSequence seq = new AlphabeticSequence("A");\r
+        // naive way to get to the right value is to iterate - to be optimized\r
+        for (int i = 0; i < columnIndex; i++) {\r
+            seq.next();\r
+        }\r
+        return seq.current();\r
+    }\r
+\r
+}\r
diff --git a/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContext.java b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContext.java
new file mode 100644 (file)
index 0000000..5bcfe56
--- /dev/null
@@ -0,0 +1,52 @@
+/**\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  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,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */package org.apache.metamodel.schema.builder;\r
+\r
+import org.apache.metamodel.schema.Table;\r
+\r
+/**\r
+ * Defines the context for naming a single column in a\r
+ * {@link ColumnNamingStrategy} session.\r
+ */\r
+public interface ColumnNamingContext {\r
+\r
+    /**\r
+     * Gets the index of the column being named.\r
+     * \r
+     * @return\r
+     */\r
+    public int getColumnIndex();\r
+\r
+    /**\r
+     * Gets the {@link Table} that the column is to pertain to. If the table is\r
+     * not yet available then this may return null.\r
+     * \r
+     * @return\r
+     */\r
+    public Table getTable();\r
+\r
+    /**\r
+     * Gets the intrinsic column name, if this is defined in the datastore\r
+     * itself. This may be in the form of a header or such. Sometimes intrinsic\r
+     * column names exist only for some columns and sometimes there may be\r
+     * duplicate names or other anomalies which are often discouraged.\r
+     * \r
+     * @return\r
+     */\r
+    public String getIntrinsicColumnName();\r
+}\r
diff --git a/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContextImpl.java b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContextImpl.java
new file mode 100644 (file)
index 0000000..cd04f98
--- /dev/null
@@ -0,0 +1,64 @@
+/**\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  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,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+package org.apache.metamodel.schema.builder;\r
+\r
+import org.apache.metamodel.schema.Table;\r
+\r
+public class ColumnNamingContextImpl implements ColumnNamingContext {\r
+\r
+    private final int columnIndex;\r
+    private final Table table;\r
+    private final String intrinsicColumnName;\r
+\r
+    /**\r
+     * \r
+     * @param table\r
+     * @param intrinsicColumnName\r
+     * @param columnIndex\r
+     */\r
+    public ColumnNamingContextImpl(Table table, String intrinsicColumnName, int columnIndex) {\r
+        this.table = table;\r
+        this.intrinsicColumnName = intrinsicColumnName;\r
+        this.columnIndex = columnIndex;\r
+    }\r
+\r
+    /**\r
+     * \r
+     * @param columnIndex\r
+     */\r
+    public ColumnNamingContextImpl(int columnIndex) {\r
+        this(null, null, columnIndex);\r
+    }\r
+\r
+    @Override\r
+    public int getColumnIndex() {\r
+        return columnIndex;\r
+    }\r
+\r
+    @Override\r
+    public Table getTable() {\r
+        return table;\r
+    }\r
+\r
+    @Override\r
+    public String getIntrinsicColumnName() {\r
+        return intrinsicColumnName;\r
+    }\r
+\r
+}\r
diff --git a/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java
new file mode 100644 (file)
index 0000000..626daaf
--- /dev/null
@@ -0,0 +1,37 @@
+/**\r
+ * Licensed to the Apache Software Foundation (ASF) under one\r
+ * or more contributor license agreements.  See the NOTICE file\r
+ * distributed with this work for additional information\r
+ * regarding copyright ownership.  The ASF licenses this file\r
+ * to you under the Apache License, Version 2.0 (the\r
+ * "License"); you may not use this file except in compliance\r
+ * with the License.  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,\r
+ * software distributed under the License is distributed on an\r
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\r
+ * KIND, either express or implied.  See the License for the\r
+ * specific language governing permissions and limitations\r
+ * under the License.\r
+ */\r
+package org.apache.metamodel.schema.builder;\r
+\r
+/**\r
+ * A strategy that defines how columns are logically named. Such strategies are\r
+ * mostly used when a particular datastore is not itself intrinsically\r
+ * specifying the column name.\r
+ */\r
+public interface ColumnNamingStrategy {\r
+\r
+    /**\r
+     * Provides the name to apply for a given column.\r
+     * \r
+     * @param ctx\r
+     *            the context of the column naming taking place. This contains\r
+     *            column index, intrinsic name etc. if available.\r
+     * @return the name to provide to the column.\r
+     */\r
+    public String getColumnName(ColumnNamingContext ctx);\r
+}\r
index 86a038a..3b90b32 100644 (file)
@@ -23,6 +23,8 @@ import java.util.Arrays;
 import java.util.List;
 
 import org.apache.metamodel.data.DataSet;
+import org.apache.metamodel.schema.builder.AlphabeticColumnNamingStrategy;
+import org.apache.metamodel.schema.builder.ColumnNamingStrategy;
 import org.apache.metamodel.util.BaseObject;
 import org.apache.metamodel.util.FileHelper;
 
@@ -42,6 +44,7 @@ public final class FixedWidthConfiguration extends BaseObject implements
        private final int[] valueWidths;
        private final int columnNameLineNumber;
        private final boolean failOnInconsistentLineWidth;
+       private final ColumnNamingStrategy columnNamingStrategy;
 
        public FixedWidthConfiguration(int fixedValueWidth) {
                this(DEFAULT_COLUMN_NAME_LINE, FileHelper.DEFAULT_ENCODING,
@@ -64,6 +67,7 @@ public final class FixedWidthConfiguration extends BaseObject implements
                this.fixedValueWidth = fixedValueWidth;
                this.columnNameLineNumber = columnNameLineNumber;
                this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+               this.columnNamingStrategy = null;
                this.valueWidths = new int[0];
        }
 
@@ -73,6 +77,7 @@ public final class FixedWidthConfiguration extends BaseObject implements
                this.fixedValueWidth = -1;
                this.columnNameLineNumber = columnNameLineNumber;
                this.failOnInconsistentLineWidth = failOnInconsistentLineWidth;
+               this.columnNamingStrategy = null;
                this.valueWidths = valueWidths;
        }
 
@@ -84,6 +89,17 @@ public final class FixedWidthConfiguration extends BaseObject implements
        public int getColumnNameLineNumber() {
                return columnNameLineNumber;
        }
+       
+       /**
+        * Gets a {@link ColumnNamingStrategy} to use if needed.
+        * @return
+        */
+       public ColumnNamingStrategy getColumnNamingStrategy() {
+           if (columnNamingStrategy == null) {
+               return new AlphabeticColumnNamingStrategy();
+           }
+        return columnNamingStrategy;
+    }
 
        /**
         * Gets the file encoding to use for reading the file.
index 4876847..fcc31ed 100644 (file)
@@ -33,7 +33,8 @@ import org.apache.metamodel.schema.MutableTable;
 import org.apache.metamodel.schema.Schema;
 import org.apache.metamodel.schema.Table;
 import org.apache.metamodel.schema.TableType;
-import org.apache.metamodel.util.AlphabeticSequence;
+import org.apache.metamodel.schema.builder.ColumnNamingContextImpl;
+import org.apache.metamodel.schema.builder.ColumnNamingStrategy;
 import org.apache.metamodel.util.FileHelper;
 import org.apache.metamodel.util.FileResource;
 import org.apache.metamodel.util.Resource;
@@ -127,11 +128,12 @@ public class FixedWidthDataContext extends QueryPostprocessDataContext {
                 }
                 columnNames = reader.readLine();
             } else {
+                final ColumnNamingStrategy columnNamingStrategy = _configuration.getColumnNamingStrategy();
                 columnNames = reader.readLine();
                 if (columnNames != null) {
-                    AlphabeticSequence sequence = new AlphabeticSequence();
                     for (int i = 0; i < columnNames.length; i++) {
-                        columnNames[i] = sequence.next();
+                        columnNames[i] = columnNamingStrategy.getColumnName(new ColumnNamingContextImpl(table, null,
+                                i));
                     }
                 }
             }
@@ -179,11 +181,11 @@ public class FixedWidthDataContext extends QueryPostprocessDataContext {
         final Reader fileReader = FileHelper.getReader(inputStream, _configuration.getEncoding());
         final FixedWidthReader reader;
         if (_configuration.isConstantValueWidth()) {
-            reader = new FixedWidthReader(fileReader, _configuration.getFixedValueWidth(),
-                    _configuration.isFailOnInconsistentLineWidth());
+            reader = new FixedWidthReader(fileReader, _configuration.getFixedValueWidth(), _configuration
+                    .isFailOnInconsistentLineWidth());
         } else {
-            reader = new FixedWidthReader(fileReader, _configuration.getValueWidths(),
-                    _configuration.isFailOnInconsistentLineWidth());
+            reader = new FixedWidthReader(fileReader, _configuration.getValueWidths(), _configuration
+                    .isFailOnInconsistentLineWidth());
         }
         return reader;
     }