METAMODEL-244: Scoped the column naming session and state into an object
authorKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Sun, 24 Apr 2016 20:32:15 +0000 (13:32 -0700)
committerKasper Sørensen <i.am.kasper.sorensen@gmail.com>
Sun, 24 Apr 2016 20:32:15 +0000 (13:32 -0700)
core/src/main/java/org/apache/metamodel/schema/builder/AlphabeticColumnNamingStrategy.java
core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingSession.java [new file with mode: 0644]
core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingStrategy.java
core/src/main/java/org/apache/metamodel/schema/builder/DelegatingIntrinsicSwitchColumnNamingStrategy.java
core/src/main/java/org/apache/metamodel/schema/builder/UniqueColumnNamingStrategy.java
fixedwidth/src/main/java/org/apache/metamodel/fixedwidth/FixedWidthDataContext.java

index 83679a3..91d1b85 100644 (file)
@@ -21,12 +21,23 @@ package org.apache.metamodel.schema.builder;
 import org.apache.metamodel.util.AlphabeticSequence;\r
 \r
 public class AlphabeticColumnNamingStrategy implements ColumnNamingStrategy {\r
-    \r
-    private final AlphabeticSequence seq = new AlphabeticSequence();\r
+\r
+    private static final long serialVersionUID = 1L;\r
 \r
     @Override\r
-    public String getNextColumnName(ColumnNamingContext ctx) {\r
-        return seq.next();\r
+    public ColumnNamingSession startColumnNamingSession() {\r
+        return new ColumnNamingSession() {\r
+            private final AlphabeticSequence seq = new AlphabeticSequence();\r
+\r
+            @Override\r
+            public String getNextColumnName(ColumnNamingContext ctx) {\r
+                return seq.next();\r
+            }\r
+\r
+            @Override\r
+            public void close() {\r
+            }\r
+        };\r
     }\r
 \r
 }\r
diff --git a/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingSession.java b/core/src/main/java/org/apache/metamodel/schema/builder/ColumnNamingSession.java
new file mode 100644 (file)
index 0000000..2c6f7b2
--- /dev/null
@@ -0,0 +1,47 @@
+/**
+ * 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 java.io.Closeable;
+
+import org.apache.metamodel.schema.Column;
+import org.apache.metamodel.schema.Table;
+
+/**
+ * Represents a 'session' in which a single {@link Table}'s {@link Column}s are
+ * named.
+ */
+public interface ColumnNamingSession extends Closeable {
+
+    /**
+     * 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 getNextColumnName(ColumnNamingContext ctx);
+
+    /**
+     * Ends the column naming session.
+     */
+    @Override
+    public void close();
+}
index 4923c58..2219fb7 100644 (file)
  */\r
 package org.apache.metamodel.schema.builder;\r
 \r
+import java.io.Serializable;\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 getNextColumnName(ColumnNamingContext ctx);\r
+public interface ColumnNamingStrategy extends Serializable {\r
+    \r
+    public ColumnNamingSession startColumnNamingSession();\r
 }\r
index 99cdaf5..612f81d 100644 (file)
@@ -25,6 +25,7 @@ package org.apache.metamodel.schema.builder;
  */\r
 public class DelegatingIntrinsicSwitchColumnNamingStrategy implements ColumnNamingStrategy {\r
 \r
+    private static final long serialVersionUID = 1L;\r
     private final ColumnNamingStrategy intrinsicStrategy;\r
     private final ColumnNamingStrategy nonIntrinsicStrategy;\r
 \r
@@ -35,12 +36,25 @@ public class DelegatingIntrinsicSwitchColumnNamingStrategy implements ColumnNami
     }\r
 \r
     @Override\r
-    public String getNextColumnName(ColumnNamingContext ctx) {\r
-        final String intrinsicColumnName = ctx.getIntrinsicColumnName();\r
-        if (intrinsicColumnName == null || intrinsicColumnName.isEmpty()) {\r
-            return nonIntrinsicStrategy.getNextColumnName(ctx);\r
-        }\r
-        return intrinsicStrategy.getNextColumnName(ctx);\r
-    }\r
+    public ColumnNamingSession startColumnNamingSession() {\r
+        final ColumnNamingSession intrinsicSession = intrinsicStrategy.startColumnNamingSession();\r
+        final ColumnNamingSession nonIntrinsicSession = nonIntrinsicStrategy.startColumnNamingSession();\r
+        return new ColumnNamingSession() {\r
+\r
+            @Override\r
+            public String getNextColumnName(ColumnNamingContext ctx) {\r
+                final String intrinsicColumnName = ctx.getIntrinsicColumnName();\r
+                if (intrinsicColumnName == null || intrinsicColumnName.isEmpty()) {\r
+                    return nonIntrinsicSession.getNextColumnName(ctx);\r
+                }\r
+                return intrinsicSession.getNextColumnName(ctx);\r
+            }\r
 \r
+            @Override\r
+            public void close() {\r
+                intrinsicSession.close();\r
+                nonIntrinsicSession.close();\r
+            }\r
+        };\r
+    }\r
 }\r
index 9371059..17e7830 100644 (file)
@@ -29,22 +29,34 @@ import java.util.Set;
  */\r
 public class UniqueColumnNamingStrategy implements ColumnNamingStrategy {\r
 \r
-    private final Set<String> names = new HashSet<>();\r
+    private static final long serialVersionUID = 1L;\r
 \r
     @Override\r
-    public String getNextColumnName(ColumnNamingContext ctx) {\r
-        final String intrinsicName = ctx.getIntrinsicColumnName();\r
-        boolean unique = names.add(intrinsicName);\r
-        if (unique) {\r
-            return intrinsicName;\r
-        }\r
-\r
-        String newName = null;\r
-        for (int i = 2; !unique; i++) {\r
-            newName = intrinsicName + i;\r
-            unique = names.add(newName);\r
-        }\r
-        return newName;\r
+    public ColumnNamingSession startColumnNamingSession() {\r
+        return new ColumnNamingSession() {\r
+\r
+            private final Set<String> names = new HashSet<>();\r
+\r
+            @Override\r
+            public String getNextColumnName(ColumnNamingContext ctx) {\r
+                final String intrinsicName = ctx.getIntrinsicColumnName();\r
+                boolean unique = names.add(intrinsicName);\r
+                if (unique) {\r
+                    return intrinsicName;\r
+                }\r
+\r
+                String newName = null;\r
+                for (int i = 2; !unique; i++) {\r
+                    newName = intrinsicName + i;\r
+                    unique = names.add(newName);\r
+                }\r
+                return newName;\r
+            }\r
+\r
+            @Override\r
+            public void close() {\r
+            }\r
+        };\r
     }\r
 \r
 }\r
index 0a326cc..25a8457 100644 (file)
@@ -34,6 +34,7 @@ import org.apache.metamodel.schema.Schema;
 import org.apache.metamodel.schema.Table;
 import org.apache.metamodel.schema.TableType;
 import org.apache.metamodel.schema.builder.ColumnNamingContextImpl;
+import org.apache.metamodel.schema.builder.ColumnNamingSession;
 import org.apache.metamodel.schema.builder.ColumnNamingStrategy;
 import org.apache.metamodel.util.FileHelper;
 import org.apache.metamodel.util.FileResource;
@@ -134,10 +135,12 @@ public class FixedWidthDataContext extends QueryPostprocessDataContext {
             }
             final ColumnNamingStrategy columnNamingStrategy = _configuration.getColumnNamingStrategy();
             if (columnNames != null) {
-                for (int i = 0; i < columnNames.length; i++) {
-                    final String intrinsicColumnName = hasColumnHeader ? columnNames[i] : null;
-                    columnNames[i] = columnNamingStrategy.getNextColumnName(new ColumnNamingContextImpl(table,
-                            intrinsicColumnName, i));
+                try (final ColumnNamingSession columnNamingSession = columnNamingStrategy.startColumnNamingSession()) {
+                    for (int i = 0; i < columnNames.length; i++) {
+                        final String intrinsicColumnName = hasColumnHeader ? columnNames[i] : null;
+                        columnNames[i] = columnNamingSession.getNextColumnName(new ColumnNamingContextImpl(table,
+                                intrinsicColumnName, i));
+                    }
                 }
             }
         } finally {