You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@metamodel.apache.org by ka...@apache.org on 2016/05/16 03:53:59 UTC

[24/42] metamodel git commit: METAMODEL-244: Made ColumnNamingStrategy available to fixedwidth module

METAMODEL-244: Made ColumnNamingStrategy available to fixedwidth module

Project: http://git-wip-us.apache.org/repos/asf/metamodel/repo
Commit: http://git-wip-us.apache.org/repos/asf/metamodel/commit/3d0666ca
Tree: http://git-wip-us.apache.org/repos/asf/metamodel/tree/3d0666ca
Diff: http://git-wip-us.apache.org/repos/asf/metamodel/diff/3d0666ca

Branch: refs/heads/5.x
Commit: 3d0666cabce5e6df40ec341bb1d21b74343d1e3f
Parents: 820b1f2
Author: kaspersorensen <i....@gmail.com>
Authored: Fri Apr 22 14:54:22 2016 -0700
Committer: kaspersorensen <i....@gmail.com>
Committed: Fri Apr 22 14:54:22 2016 -0700

----------------------------------------------------------------------
 .../builder/AlphabeticColumnNamingStrategy.java | 36 +++++++++++
 .../schema/builder/ColumnNamingContext.java     | 52 ++++++++++++++++
 .../schema/builder/ColumnNamingContextImpl.java | 64 ++++++++++++++++++++
 .../schema/builder/ColumnNamingStrategy.java    | 37 +++++++++++
 .../fixedwidth/FixedWidthConfiguration.java     | 16 +++++
 .../fixedwidth/FixedWidthDataContext.java       | 16 ++---
 6 files changed, 214 insertions(+), 7 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.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
index 0000000..284e851
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.java
@@ -0,0 +1,36 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.metamodel.schema.builder;
+
+import org.apache.metamodel.util.AlphabeticSequence;
+
+public class AlphabeticColumnNamingStrategy implements ColumnNamingStrategy {
+
+    @Override
+    public String getColumnName(ColumnNamingContext ctx) {
+        final int columnIndex = ctx.getColumnIndex();
+        final AlphabeticSequence seq = new AlphabeticSequence("A");
+        // naive way to get to the right value is to iterate - to be optimized
+        for (int i = 0; i < columnIndex; i++) {
+            seq.next();
+        }
+        return seq.current();
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContext.java
----------------------------------------------------------------------
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
index 0000000..5bcfe56
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContext.java
@@ -0,0 +1,52 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */package org.apache.metamodel.schema.builder;
+
+import org.apache.metamodel.schema.Table;
+
+/**
+ * Defines the context for naming a single column in a
+ * {@link ColumnNamingStrategy} session.
+ */
+public interface ColumnNamingContext {
+
+    /**
+     * Gets the index of the column being named.
+     * 
+     * @return
+     */
+    public int getColumnIndex();
+
+    /**
+     * Gets the {@link Table} that the column is to pertain to. If the table is
+     * not yet available then this may return null.
+     * 
+     * @return
+     */
+    public Table getTable();
+
+    /**
+     * Gets the intrinsic column name, if this is defined in the datastore
+     * itself. This may be in the form of a header or such. Sometimes intrinsic
+     * column names exist only for some columns and sometimes there may be
+     * duplicate names or other anomalies which are often discouraged.
+     * 
+     * @return
+     */
+    public String getIntrinsicColumnName();
+}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContextImpl.java
----------------------------------------------------------------------
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
index 0000000..cd04f98
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingContextImpl.java
@@ -0,0 +1,64 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.metamodel.schema.builder;
+
+import org.apache.metamodel.schema.Table;
+
+public class ColumnNamingContextImpl implements ColumnNamingContext {
+
+    private final int columnIndex;
+    private final Table table;
+    private final String intrinsicColumnName;
+
+    /**
+     * 
+     * @param table
+     * @param intrinsicColumnName
+     * @param columnIndex
+     */
+    public ColumnNamingContextImpl(Table table, String intrinsicColumnName, int columnIndex) {
+        this.table = table;
+        this.intrinsicColumnName = intrinsicColumnName;
+        this.columnIndex = columnIndex;
+    }
+
+    /**
+     * 
+     * @param columnIndex
+     */
+    public ColumnNamingContextImpl(int columnIndex) {
+        this(null, null, columnIndex);
+    }
+
+    @Override
+    public int getColumnIndex() {
+        return columnIndex;
+    }
+
+    @Override
+    public Table getTable() {
+        return table;
+    }
+
+    @Override
+    public String getIntrinsicColumnName() {
+        return intrinsicColumnName;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java
----------------------------------------------------------------------
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
index 0000000..626daaf
--- /dev/null
+++ b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java
@@ -0,0 +1,37 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you 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.
+ */
+package org.apache.metamodel.schema.builder;
+
+/**
+ * A strategy that defines how columns are logically named. Such strategies are
+ * mostly used when a particular datastore is not itself intrinsically
+ * specifying the column name.
+ */
+public interface ColumnNamingStrategy {
+
+    /**
+     * Provides the name to apply for a given column.
+     * 
+     * @param ctx
+     *            the context of the column naming taking place. This contains
+     *            column index, intrinsic name etc. if available.
+     * @return the name to provide to the column.
+     */
+    public String getColumnName(ColumnNamingContext ctx);
+}

http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
index 86a038a..3b90b32 100644
--- a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthConfiguration.java
@@ -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.

http://git-wip-us.apache.org/repos/asf/metamodel/blob/3d0666ca/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
----------------------------------------------------------------------
diff --git a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
index 4876847..fcc31ed 100644
--- a/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
+++ b/fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java
@@ -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;
     }