You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by gg...@apache.org on 2022/11/06 20:28:43 UTC

[commons-configuration] branch master updated: Use a builder instead of a constructor

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-configuration.git


The following commit(s) were added to refs/heads/master by this push:
     new 38ec00fb Use a builder instead of a constructor
38ec00fb is described below

commit 38ec00fb7a2742b7c2da61ed60b551af58ce710b
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Sun Nov 6 15:28:38 2022 -0500

    Use a builder instead of a constructor
---
 .../configuration2/AbstractConfiguration.java      |  7 +--
 .../AbstractHierarchicalConfiguration.java         |  1 +
 .../BaseHierarchicalConfiguration.java             |  1 +
 .../commons/configuration2/INIConfiguration.java   | 55 +++++++++++++++++-----
 .../configuration2/TestINIConfiguration.java       |  9 +++-
 5 files changed, 57 insertions(+), 16 deletions(-)

diff --git a/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java b/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
index 66b52ea8..300605fc 100644
--- a/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/AbstractConfiguration.java
@@ -91,6 +91,7 @@ import org.apache.commons.lang3.StringUtils;
  *
  */
 public abstract class AbstractConfiguration extends BaseEventSource implements Configuration {
+
     /** The list delimiter handler. */
     private ListDelimiterHandler listDelimiterHandler;
 
@@ -127,7 +128,7 @@ public abstract class AbstractConfiguration extends BaseEventSource implements C
     }
 
     /**
-     * Returns the {@code ListDelimiterHandler} used by this instance.
+     * Gets the {@code ListDelimiterHandler} used by this instance.
      *
      * @return the {@code ListDelimiterHandler}
      * @since 2.0
@@ -163,7 +164,7 @@ public abstract class AbstractConfiguration extends BaseEventSource implements C
     }
 
     /**
-     * Returns the {@code ConversionHandler} used by this instance.
+     * Gets the {@code ConversionHandler} used by this instance.
      *
      * @return the {@code ConversionHandler}
      * @since 2.0
@@ -212,7 +213,7 @@ public abstract class AbstractConfiguration extends BaseEventSource implements C
     }
 
     /**
-     * Returns the {@code ConfigurationInterpolator} object that manages the lookup objects for resolving variables.
+     * Gets the {@code ConfigurationInterpolator} object that manages the lookup objects for resolving variables.
      * Unless a custom interpolator has been set or the instance has been modified, the returned interpolator will
      * resolve values from this configuration instance and support the
      * {@link ConfigurationInterpolator#getDefaultPrefixLookups() default prefix lookups}.
diff --git a/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java b/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
index 6e1a51f6..91d4f84a 100644
--- a/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/AbstractHierarchicalConfiguration.java
@@ -153,6 +153,7 @@ import org.apache.commons.configuration2.tree.QueryResult;
  */
 public abstract class AbstractHierarchicalConfiguration<T> extends AbstractConfiguration
     implements Cloneable, NodeKeyResolver<T>, HierarchicalConfiguration<T> {
+
     /** The model for managing the data stored in this configuration. */
     private NodeModel<T> model;
 
diff --git a/src/main/java/org/apache/commons/configuration2/BaseHierarchicalConfiguration.java b/src/main/java/org/apache/commons/configuration2/BaseHierarchicalConfiguration.java
index 0e0100b9..da9dc62d 100644
--- a/src/main/java/org/apache/commons/configuration2/BaseHierarchicalConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/BaseHierarchicalConfiguration.java
@@ -51,6 +51,7 @@ import org.apache.commons.lang3.ObjectUtils;
  *
  */
 public class BaseHierarchicalConfiguration extends AbstractHierarchicalConfiguration<ImmutableNode> implements InMemoryNodeModelSupport {
+
     /** A listener for reacting on changes caused by sub configurations. */
     private final EventListener<ConfigurationEvent> changeListener;
 
diff --git a/src/main/java/org/apache/commons/configuration2/INIConfiguration.java b/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
index 4d3745eb..90e1fe87 100644
--- a/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
+++ b/src/main/java/org/apache/commons/configuration2/INIConfiguration.java
@@ -196,6 +196,7 @@ import org.apache.commons.configuration2.tree.TrackedNodeModel;
  * @since 1.6
  */
 public class INIConfiguration extends BaseHierarchicalConfiguration implements FileBasedConfiguration {
+
     /**
      * The default characters that signal the start of a comment line.
      */
@@ -247,25 +248,57 @@ public class INIConfiguration extends BaseHierarchicalConfiguration implements F
     public INIConfiguration() {
     }
 
+    /**
+     * Creates a new instance of {@code INIConfiguration} with the content of the specified
+     * {@code HierarchicalConfiguration}.
+     *
+     * @param c the configuration to be copied
+     * @since 2.0
+     */
+    public INIConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
+        super(c);
+    }
+
     /**
      * Create a new empty INI Configuration with option to allow inline comments on the section line.
      *
      * @param sectionInLineCommentsAllowed when true inline comments on the section line are allowed
-     * @since 2.9.0
      */
-    public INIConfiguration(boolean sectionInLineCommentsAllowed) {
+    private INIConfiguration(final boolean sectionInLineCommentsAllowed) {
         this.sectionInLineCommentsAllowed = sectionInLineCommentsAllowed;
     }
 
     /**
-     * Creates a new instance of {@code INIConfiguration} with the content of the specified
-     * {@code HierarchicalConfiguration}.
+     * Creates a new builder.
      *
-     * @param c the configuration to be copied
-     * @since 2.0
+     * @return a new builder.
+     * @since 2.9.0
      */
-    public INIConfiguration(final HierarchicalConfiguration<ImmutableNode> c) {
-        super(c);
+    public static Builder builder() {
+        return new Builder();
+    }
+
+    /**
+     * Builds instances of INIConfiguration.
+     *
+     * @since 2.9.0
+     */
+    public static class Builder {
+
+        /**
+         * The flag for decision, whether inline comments on the section line are allowed.
+         */
+        private boolean sectionInLineCommentsAllowed;
+
+        public Builder setSectionInLineCommentsAllowed(final boolean sectionInLineCommentsAllowed) {
+            this.sectionInLineCommentsAllowed = sectionInLineCommentsAllowed;
+            return this;
+        }
+
+        public INIConfiguration build() {
+            return new INIConfiguration(sectionInLineCommentsAllowed);
+        }
+
     }
 
     /**
@@ -445,7 +478,7 @@ public class INIConfiguration extends BaseHierarchicalConfiguration implements F
             line = line.trim();
             if (!isCommentLine(line)) {
                 if (isSectionLine(line)) {
-                    int length = sectionInLineCommentsAllowed ? line.indexOf("]") : line.length() - 1;
+                    final int length = sectionInLineCommentsAllowed ? line.indexOf("]") : line.length() - 1;
                     final String section = line.substring(1, length);
                     sectionBuilder = sectionBuilders.get(section);
                     if (sectionBuilder == null) {
@@ -761,7 +794,7 @@ public class INIConfiguration extends BaseHierarchicalConfiguration implements F
      * @param line The line to check.
      * @return true if the entire line is a section
      */
-    private static boolean isStrictSection(String line) {
+    private static boolean isStrictSection(final String line) {
         return line.startsWith("[") && line.endsWith("]");
     }
 
@@ -771,7 +804,7 @@ public class INIConfiguration extends BaseHierarchicalConfiguration implements F
      * @param line The line to check.
      * @return true if the line contains a section
      */
-    private static boolean isNonStrictSection(String line) {
+    private static boolean isNonStrictSection(final String line) {
         return line.startsWith("[") && line.contains("]");
     }
 
diff --git a/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java b/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
index 04778c43..87133862 100644
--- a/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
+++ b/src/test/java/org/apache/commons/configuration2/TestINIConfiguration.java
@@ -63,13 +63,14 @@ import org.junit.jupiter.params.provider.MethodSource;
 
 /**
  * Test class for {@code INIConfiguration}.
- *
  */
 public class TestINIConfiguration {
+
     /**
      * A thread class for testing concurrent access to the global section.
      */
     private static class GlobalSectionTestThread extends Thread {
+
         /** The configuration. */
         private final INIConfiguration config;
 
@@ -198,7 +199,11 @@ public class TestINIConfiguration {
      * @throws ConfigurationException if an error occurs
      */
     private static INIConfiguration setUpConfig(final String data, boolean inLineCommentsAllowed) throws ConfigurationException {
-        final INIConfiguration instance = new INIConfiguration(inLineCommentsAllowed);
+        // @formatter:off
+        final INIConfiguration instance = INIConfiguration.builder()
+                .setSectionInLineCommentsAllowed(inLineCommentsAllowed)
+                .build();
+        // @formatter:on
         instance.setListDelimiterHandler(new DefaultListDelimiterHandler(','));
         load(instance, data);
         return instance;