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

zhangliang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git


The following commit(s) were added to refs/heads/master by this push:
     new 59cc0a3aeb7 Remove InstanceTypeUtils (#33278)
59cc0a3aeb7 is described below

commit 59cc0a3aeb70f1e978ddc2369a527ae378dd2181
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Oct 17 17:37:27 2024 +0800

    Remove InstanceTypeUtils (#33278)
    
    * Remove InstanceTypeUtils
    
    * Remove InstanceTypeUtils
    
    * Remove InstanceTypeUtils
---
 .../infra/instance/metadata/InstanceType.java      | 27 ++++++++-
 .../infra/instance/metadata/InstanceTypeTest.java  | 18 +++---
 .../pipeline/core/job/id/PipelineJobIdUtils.java   |  8 +--
 .../data/pipeline/core/util/InstanceTypeUtils.java | 65 ----------------------
 4 files changed, 40 insertions(+), 78 deletions(-)

diff --git 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceType.java
 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceType.java
index 43886c1d693..1ff8cc95835 100644
--- 
a/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceType.java
+++ 
b/infra/common/src/main/java/org/apache/shardingsphere/infra/instance/metadata/InstanceType.java
@@ -17,10 +17,35 @@
 
 package org.apache.shardingsphere.infra.instance.metadata;
 
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
 /**
  * Instance type.
  */
+@RequiredArgsConstructor
+@Getter
 public enum InstanceType {
     
-    JDBC, PROXY
+    JDBC('j'),
+    
+    PROXY('p');
+    
+    private final char code;
+    
+    /**
+     * Value of instance type.
+     *
+     * @param code instance type code
+     * @return instance type
+     * @throws IllegalArgumentException unknown instance type code
+     */
+    public static InstanceType valueOf(final char code) {
+        for (InstanceType each : values()) {
+            if (each.code == code) {
+                return each;
+            }
+        }
+        throw new IllegalArgumentException(String.format("Unknown instance 
type code: '%s'.", code));
+    }
 }
diff --git 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtilsTest.java
 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceTypeTest.java
similarity index 67%
rename from 
kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtilsTest.java
rename to 
infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceTypeTest.java
index 07f4281ada3..5e17cae8ef9 100644
--- 
a/kernel/data-pipeline/core/src/test/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtilsTest.java
+++ 
b/infra/common/src/test/java/org/apache/shardingsphere/infra/instance/metadata/InstanceTypeTest.java
@@ -15,20 +15,24 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.data.pipeline.core.util;
+package org.apache.shardingsphere.infra.instance.metadata;
 
-import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
 import org.junit.jupiter.api.Test;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
 
-class InstanceTypeUtilsTest {
+class InstanceTypeTest {
     
     @Test
-    void assertEncodeAndDecode() {
-        for (InstanceType each : InstanceType.values()) {
-            
assertThat(InstanceTypeUtils.decode(InstanceTypeUtils.encode(each)), is(each));
-        }
+    void assertValueOfValidCode() {
+        assertThat(InstanceType.valueOf('j'), is(InstanceType.JDBC));
+        assertThat(InstanceType.valueOf('p'), is(InstanceType.PROXY));
+    }
+    
+    @Test
+    void assertValueOfInvalidCode() {
+        assertThrows(IllegalArgumentException.class, () -> 
InstanceType.valueOf('a'));
     }
 }
diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/id/PipelineJobIdUtils.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/id/PipelineJobIdUtils.java
index 459193309f3..f2f00960f65 100644
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/id/PipelineJobIdUtils.java
+++ 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/job/id/PipelineJobIdUtils.java
@@ -29,7 +29,6 @@ import 
org.apache.shardingsphere.data.pipeline.core.exception.job.PipelineJobNot
 import org.apache.shardingsphere.data.pipeline.core.job.api.PipelineAPIFactory;
 import org.apache.shardingsphere.data.pipeline.core.job.type.JobCodeRegistry;
 import org.apache.shardingsphere.data.pipeline.core.job.type.PipelineJobType;
-import org.apache.shardingsphere.data.pipeline.core.util.InstanceTypeUtils;
 import org.apache.shardingsphere.elasticjob.infra.pojo.JobConfigurationPOJO;
 import 
org.apache.shardingsphere.infra.exception.core.ShardingSpherePreconditions;
 import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
@@ -57,8 +56,7 @@ public final class PipelineJobIdUtils {
         String databaseName = instanceType == InstanceType.PROXY ? "" : 
contextKey.getDatabaseName();
         String databaseNameHex = 
Hex.encodeHexString(databaseName.getBytes(StandardCharsets.UTF_8), true);
         String databaseNameLengthHex = 
Hex.encodeHexString(Shorts.toByteArray((short) databaseNameHex.length()), true);
-        char encodedInstanceType = InstanceTypeUtils.encode(instanceType);
-        return 'j' + jobType.getCode() + PipelineJobId.CURRENT_VERSION + 
encodedInstanceType + databaseNameLengthHex + databaseNameHex;
+        return 'j' + jobType.getCode() + PipelineJobId.CURRENT_VERSION + 
instanceType.getCode() + databaseNameLengthHex + databaseNameHex;
     }
     
     /**
@@ -88,10 +86,10 @@ public final class PipelineJobIdUtils {
         verifyJobId(jobId);
         String formatVersion = jobId.substring(3, 5);
         
Preconditions.checkArgument(PipelineJobId.CURRENT_VERSION.equals(formatVersion),
 "Format version doesn't match, format version: " + formatVersion);
-        char instanceType = jobId.charAt(5);
+        char instanceTypeCode = jobId.charAt(5);
         short databaseNameLength = 
Shorts.fromByteArray(Hex.decodeHex(jobId.substring(6, 10)));
         String databaseName = new String(Hex.decodeHex(jobId.substring(10, 10 
+ databaseNameLength)), StandardCharsets.UTF_8);
-        return new PipelineContextKey(databaseName, 
InstanceTypeUtils.decode(instanceType));
+        return new PipelineContextKey(databaseName, 
InstanceType.valueOf(instanceTypeCode));
     }
     
     /**
diff --git 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtils.java
 
b/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtils.java
deleted file mode 100644
index 216e58a92d4..00000000000
--- 
a/kernel/data-pipeline/core/src/main/java/org/apache/shardingsphere/data/pipeline/core/util/InstanceTypeUtils.java
+++ /dev/null
@@ -1,65 +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.shardingsphere.data.pipeline.core.util;
-
-import lombok.AccessLevel;
-import lombok.NoArgsConstructor;
-import org.apache.shardingsphere.infra.instance.metadata.InstanceType;
-
-/**
- * Instance type utility class.
- */
-@NoArgsConstructor(access = AccessLevel.PRIVATE)
-public final class InstanceTypeUtils {
-    
-    /**
-     * Encode instance type.
-     *
-     * @param instanceType instance type
-     * @return encoded instance type
-     * @throws UnsupportedOperationException if instance type is unknown
-     */
-    public static char encode(final InstanceType instanceType) {
-        switch (instanceType) {
-            case PROXY:
-                return 'p';
-            case JDBC:
-                return 'j';
-            default:
-                throw new UnsupportedOperationException("Unknown instance 
type: " + instanceType);
-        }
-    }
-    
-    /**
-     * Decode instance type.
-     *
-     * @param instanceType instance type
-     * @return decoded instance type
-     * @throws UnsupportedOperationException if instance type is unknown
-     */
-    public static InstanceType decode(final char instanceType) {
-        switch (instanceType) {
-            case 'p':
-                return InstanceType.PROXY;
-            case 'j':
-                return InstanceType.JDBC;
-            default:
-                throw new UnsupportedOperationException("Unknown instance 
type: " + instanceType);
-        }
-    }
-}

Reply via email to