Skip to content

Commit

Permalink
Merge branch 'develop' into develop-issue#6284
Browse files Browse the repository at this point in the history
  • Loading branch information
ZZQ001010 authored Jul 8, 2021
2 parents 2566f85 + 81e2f43 commit 815af0c
Show file tree
Hide file tree
Showing 49 changed files with 1,835 additions and 385 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.pojo.builder;

import com.alibaba.nacos.api.naming.pojo.Instance;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
* Builder for {@link Instance}.
*
* @author xiweng.yy
*/
public class InstanceBuilder {

private String instanceId;

private String ip;

private Integer port;

private Double weight;

private Boolean healthy;

private Boolean enabled;

private Boolean ephemeral;

private String clusterName;

private String serviceName;

private Map<String, String> metadata = new HashMap<>();

private InstanceBuilder() {
}

public InstanceBuilder setInstanceId(String instanceId) {
this.instanceId = instanceId;
return this;
}

public InstanceBuilder setIp(String ip) {
this.ip = ip;
return this;
}

public InstanceBuilder setPort(Integer port) {
this.port = port;
return this;
}

public InstanceBuilder setWeight(Double weight) {
this.weight = weight;
return this;
}

public InstanceBuilder setHealthy(Boolean healthy) {
this.healthy = healthy;
return this;
}

public InstanceBuilder setEnabled(Boolean enabled) {
this.enabled = enabled;
return this;
}

public InstanceBuilder setEphemeral(Boolean ephemeral) {
this.ephemeral = ephemeral;
return this;
}

public InstanceBuilder setClusterName(String clusterName) {
this.clusterName = clusterName;
return this;
}

public InstanceBuilder setServiceName(String serviceName) {
this.serviceName = serviceName;
return this;
}

public InstanceBuilder setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
return this;
}

public InstanceBuilder addMetadata(String metaKey, String metaValue) {
this.metadata.put(metaKey, metaValue);
return this;
}

/**
* Build a new {@link Instance}.
*
* @return new instance
*/
public Instance build() {
Instance result = new Instance();
if (!Objects.isNull(instanceId)) {
result.setInstanceId(instanceId);
}
if (!Objects.isNull(ip)) {
result.setIp(ip);
}
if (!Objects.isNull(port)) {
result.setPort(port);
}
if (!Objects.isNull(weight)) {
result.setWeight(weight);
}
if (!Objects.isNull(healthy)) {
result.setHealthy(healthy);
}
if (!Objects.isNull(enabled)) {
result.setEnabled(enabled);
}
if (!Objects.isNull(ephemeral)) {
result.setEphemeral(ephemeral);
}
if (!Objects.isNull(clusterName)) {
result.setClusterName(clusterName);
}
if (!Objects.isNull(serviceName)) {
result.setServiceName(serviceName);
}
result.setMetadata(metadata);
return result;
}

public static InstanceBuilder newBuilder() {
return new InstanceBuilder();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.spi.generator;

/**
* Generator SPI for Instance Id.
*
* @author xiweng.yy
*/
public interface IdGenerator {

/**
* Generate instance id.
*
* @return instance id
*/
String generateInstanceId();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright 1999-2020 Alibaba Group Holding Ltd.
*
* Licensed 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 com.alibaba.nacos.api.naming.pojo.builder;

import com.alibaba.nacos.api.naming.pojo.Instance;
import org.junit.Test;

import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

public class InstanceBuilderTest {

private static final String SERVICE_NAME = "testService";

private static final String CLUSTER_NAME = "testCluster";

private static final String INSTANCE_ID = "ID";

private static final String IP = "127.0.0.1";

private static final int PORT = 8848;

private static final double WEIGHT = 2.0;

private static final boolean HEALTHY = false;

private static final boolean ENABLED = false;

private static final boolean EPHEMERAL = false;

private static final String META_KEY = "key";

private static final String META_VALUE = "value";

@Test
public void testBuildFullInstance() {
InstanceBuilder builder = InstanceBuilder.newBuilder();
Instance actual = builder.setServiceName(SERVICE_NAME).setClusterName(CLUSTER_NAME).setInstanceId(INSTANCE_ID)
.setIp(IP).setPort(PORT).setWeight(WEIGHT).setHealthy(HEALTHY).setEnabled(ENABLED)
.setEphemeral(EPHEMERAL).addMetadata(META_KEY, META_VALUE).build();
assertThat(actual.getServiceName(), is(SERVICE_NAME));
assertThat(actual.getClusterName(), is(CLUSTER_NAME));
assertThat(actual.getInstanceId(), is(INSTANCE_ID));
assertThat(actual.getIp(), is(IP));
assertThat(actual.getPort(), is(PORT));
assertThat(actual.getWeight(), is(WEIGHT));
assertThat(actual.isHealthy(), is(HEALTHY));
assertThat(actual.isEnabled(), is(ENABLED));
assertThat(actual.isEphemeral(), is(EPHEMERAL));
assertThat(actual.getMetadata().size(), is(1));
assertThat(actual.getMetadata().get(META_KEY), is(META_VALUE));
}

@Test
public void testBuildEmptyInstance() {
InstanceBuilder builder = InstanceBuilder.newBuilder();
Instance actual = builder.build();
assertNull(actual.getServiceName());
assertNull(actual.getClusterName());
assertNull(actual.getInstanceId());
assertNull(actual.getIp());
assertThat(actual.getPort(), is(0));
assertThat(actual.getWeight(), is(1.0));
assertTrue(actual.isHealthy());
assertTrue(actual.isEnabled());
assertTrue(actual.isEphemeral());
assertTrue(actual.getMetadata().isEmpty());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@

package com.alibaba.nacos.common.utils;

import com.google.common.base.Strings;
import com.google.common.collect.Sets;

import java.util.HashSet;

/**
* Value Convert Utils.
*
Expand All @@ -25,6 +30,10 @@ public final class ConvertUtils {

private static final String NULL_STR = "null";

public static final HashSet<String> TRUE_SET = Sets.newHashSet("y", "yes", "on", "true", "t");

public static final HashSet<String> FALSE_SET = Sets.newHashSet("n", "no", "off", "false", "f");

/**
* Convert String value to int value if parameter value is legal. And it automatically defaults to 0 if parameter
* value is null or blank str.
Expand Down Expand Up @@ -114,11 +123,7 @@ public static boolean toBoolean(String val, boolean defaultValue) {
if (StringUtils.isBlank(val)) {
return defaultValue;
}
try {
return Boolean.parseBoolean(val);
} catch (NumberFormatException exception) {
return defaultValue;
}
return Boolean.parseBoolean(val);
}

// The following utility functions are extracted from <link>org.apache.commons.lang3</link>
Expand Down Expand Up @@ -189,72 +194,13 @@ public static boolean toBoolean(final String str) {
*/
@SuppressWarnings("all")
public static Boolean toBooleanObject(String str) {
if (str == "true") {
return Boolean.TRUE;
} else if (str == null) {
return null;
String formatStr = Strings.nullToEmpty(str).toLowerCase();

if (TRUE_SET.contains(formatStr)) {
return true;
} else if (FALSE_SET.contains(formatStr)) {
return false;
} else {
char ch0;
char ch1;
char ch2;
char ch3;
switch (str.length()) {
case 1:
ch0 = str.charAt(0);
if (ch0 == 'y' || ch0 == 'Y' || ch0 == 't' || ch0 == 'T') {
return Boolean.TRUE;
}

if (ch0 != 'n' && ch0 != 'N' && ch0 != 'f' && ch0 != 'F') {
break;
}

return Boolean.FALSE;
case 2:
ch0 = str.charAt(0);
ch1 = str.charAt(1);
if ((ch0 == 'o' || ch0 == 'O') && (ch1 == 'n' || ch1 == 'N')) {
return Boolean.TRUE;
}

if ((ch0 == 'n' || ch0 == 'N') && (ch1 == 'o' || ch1 == 'O')) {
return Boolean.FALSE;
}
break;
case 3:
ch0 = str.charAt(0);
ch1 = str.charAt(1);
ch2 = str.charAt(2);
if ((ch0 == 'y' || ch0 == 'Y') && (ch1 == 'e' || ch1 == 'E') && (ch2 == 's' || ch2 == 'S')) {
return Boolean.TRUE;
}

if ((ch0 == 'o' || ch0 == 'O') && (ch1 == 'f' || ch1 == 'F') && (ch2 == 'f' || ch2 == 'F')) {
return Boolean.FALSE;
}
break;
case 4:
ch0 = str.charAt(0);
ch1 = str.charAt(1);
ch2 = str.charAt(2);
ch3 = str.charAt(3);
if ((ch0 == 't' || ch0 == 'T') && (ch1 == 'r' || ch1 == 'R') && (ch2 == 'u' || ch2 == 'U') && (
ch3 == 'e' || ch3 == 'E')) {
return Boolean.TRUE;
}
break;
case 5:
ch0 = str.charAt(0);
ch1 = str.charAt(1);
ch2 = str.charAt(2);
ch3 = str.charAt(3);
char ch4 = str.charAt(4);
if ((ch0 == 'f' || ch0 == 'F') && (ch1 == 'a' || ch1 == 'A') && (ch2 == 'l' || ch2 == 'L') && (
ch3 == 's' || ch3 == 'S') && (ch4 == 'e' || ch4 == 'E')) {
return Boolean.FALSE;
}
}

return null;
}
}
Expand Down
Loading

0 comments on commit 815af0c

Please sign in to comment.