Clean up CompactionDirectives default impl (#1950)

* Created CompactionDirectivesBuilder class for builder logic


Co-authored-by: Keith Turner <kturner@apache.org>
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectives.java b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectives.java
index e3370eb..6991fc6 100644
--- a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectives.java
+++ b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectives.java
@@ -32,17 +32,24 @@
   CompactionServiceId getService();
 
   /**
+   * Required for CompactionDirectives
+   *
    * @since 2.1.0
    */
-  public static interface Builder {
-    Builder setService(CompactionServiceId service);
+  interface ServiceBuilder {
+    Builder toService(CompactionServiceId service);
 
-    Builder setService(String compactionServiceId);
+    Builder toService(String compactionServiceId);
+  }
 
+  /**
+   * @since 2.1.0
+   */
+  interface Builder {
     CompactionDirectives build();
   }
 
-  public static Builder builder() {
-    return CompactionsDirectiveImpl.DEFAULT_BUILDER;
+  static ServiceBuilder builder() {
+    return new CompactionDirectivesBuilder();
   }
 }
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesBuilder.java b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesBuilder.java
new file mode 100644
index 0000000..c542699
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesBuilder.java
@@ -0,0 +1,48 @@
+/*
+ * 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.accumulo.core.spi.compaction;
+
+import java.util.Objects;
+
+/**
+ * This class intentionally package private.
+ */
+class CompactionDirectivesBuilder
+    implements CompactionDirectives.Builder, CompactionDirectives.ServiceBuilder {
+
+  private CompactionServiceId service;
+
+  @Override
+  public CompactionDirectives.Builder toService(CompactionServiceId service) {
+    this.service = Objects.requireNonNull(service, "CompactionServiceId cannot be null");
+    return this;
+  }
+
+  @Override
+  public CompactionDirectives.Builder toService(String compactionServiceId) {
+    this.service = CompactionServiceId.of(compactionServiceId);
+    return this;
+  }
+
+  @Override
+  public CompactionDirectives build() {
+    return new CompactionDirectivesImpl(service);
+  }
+
+}
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesImpl.java b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesImpl.java
new file mode 100644
index 0000000..fe2a833
--- /dev/null
+++ b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionDirectivesImpl.java
@@ -0,0 +1,42 @@
+/*
+ * 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.accumulo.core.spi.compaction;
+
+/**
+ * This class intentionally package private. It is immutable and provides default allocation for
+ * {@code CompactionDirectives}.
+ */
+class CompactionDirectivesImpl implements CompactionDirectives {
+
+  private final CompactionServiceId service;
+
+  public CompactionDirectivesImpl(CompactionServiceId service) {
+    this.service = service;
+  }
+
+  @Override
+  public CompactionServiceId getService() {
+    return service;
+  }
+
+  @Override
+  public String toString() {
+    return "service=" + service;
+  }
+}
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionsDirectiveImpl.java b/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionsDirectiveImpl.java
deleted file mode 100644
index ae0e0ca..0000000
--- a/core/src/main/java/org/apache/accumulo/core/spi/compaction/CompactionsDirectiveImpl.java
+++ /dev/null
@@ -1,85 +0,0 @@
-/*
- * 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.accumulo.core.spi.compaction;
-
-import java.util.Objects;
-
-import org.apache.accumulo.core.spi.compaction.CompactionDirectives.Builder;
-
-import com.google.common.base.Preconditions;
-
-/**
- * This class intentionally package private. This implementation is odd because it supports zero
- * object allocations for {@code CompactionDirectives.builder().build()}.
- */
-class CompactionsDirectiveImpl implements Builder, CompactionDirectives {
-
-  private static final CompactionDirectives DEFAULT =
-      new CompactionsDirectiveImpl().setService(CompactionServiceId.of("default")).build();
-
-  static final Builder DEFAULT_BUILDER = new Builder() {
-    @Override
-    public Builder setService(CompactionServiceId service) {
-      return new CompactionsDirectiveImpl().setService(service);
-    }
-
-    @Override
-    public Builder setService(String compactionServiceId) {
-      return new CompactionsDirectiveImpl().setService(compactionServiceId);
-    }
-
-    @Override
-    public CompactionDirectives build() {
-      return DEFAULT;
-    }
-  };
-
-  boolean built = false;
-  private CompactionServiceId service;
-
-  @Override
-  public Builder setService(CompactionServiceId service) {
-    Objects.requireNonNull(service);
-    Preconditions.checkState(!built);
-    this.service = service;
-    return this;
-  }
-
-  @Override
-  public Builder setService(String compactionServiceId) {
-    return setService(CompactionServiceId.of(compactionServiceId));
-  }
-
-  @Override
-  public CompactionServiceId getService() {
-    Preconditions.checkState(built);
-    return service;
-  }
-
-  @Override
-  public CompactionDirectives build() {
-    built = true;
-    return this;
-  }
-
-  @Override
-  public String toString() {
-    return "service=" + service;
-  }
-}
diff --git a/core/src/main/java/org/apache/accumulo/core/spi/compaction/SimpleCompactionDispatcher.java b/core/src/main/java/org/apache/accumulo/core/spi/compaction/SimpleCompactionDispatcher.java
index c985714..e572373 100644
--- a/core/src/main/java/org/apache/accumulo/core/spi/compaction/SimpleCompactionDispatcher.java
+++ b/core/src/main/java/org/apache/accumulo/core/spi/compaction/SimpleCompactionDispatcher.java
@@ -72,11 +72,11 @@
   public void init(InitParameters params) {
     services = new EnumMap<>(CompactionKind.class);
 
-    var defaultService = CompactionDirectives.builder().build();
+    var defaultService = CompactionDirectives.builder().toService("default").build();
 
     if (params.getOptions().containsKey("service")) {
       defaultService =
-          CompactionDirectives.builder().setService(params.getOptions().get("service")).build();
+          CompactionDirectives.builder().toService(params.getOptions().get("service")).build();
     }
 
     for (CompactionKind ctype : CompactionKind.values()) {
@@ -84,7 +84,7 @@
       if (service == null)
         services.put(ctype, defaultService);
       else
-        services.put(ctype, CompactionDirectives.builder().setService(service).build());
+        services.put(ctype, CompactionDirectives.builder().toService(service).build());
     }
 
     if (params.getOptions().isEmpty()) {
@@ -94,7 +94,7 @@
       params.getOptions().forEach((k, v) -> {
         if (k.startsWith("service.user.")) {
           String type = k.substring("service.user.".length());
-          tmpUS.put(type, CompactionDirectives.builder().setService(v).build());
+          tmpUS.put(type, CompactionDirectives.builder().toService(v).build());
         }
       });