Skip to content

Commit 701e7c7

Browse files
feature: 新增服务工厂类 ServiceFactory
1 parent e668234 commit 701e7c7

File tree

9 files changed

+569
-0
lines changed

9 files changed

+569
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2021-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package icu.easyj.core.factory;
17+
18+
import java.util.ArrayList;
19+
import java.util.HashMap;
20+
import java.util.List;
21+
22+
import javax.annotation.Nonnull;
23+
import javax.annotation.Nullable;
24+
25+
/**
26+
* 简易服务工厂
27+
*
28+
* @param <S> 服务类
29+
* @author wangliang181230
30+
* @since 0.8.0
31+
*/
32+
@SuppressWarnings("unused")
33+
public class ServiceFactory<S> {
34+
35+
public static final String DEFAULT_SERVICE_CODE = "default";
36+
37+
38+
private final Class<S> serviceClass;
39+
private final HashMap<String, ServiceGroup<S>> serviceMap = new HashMap<>();
40+
private final List<ServiceGroup<S>> serviceList = new ArrayList<>();
41+
42+
43+
public ServiceFactory(Class<S> serviceClass) {
44+
this.serviceClass = serviceClass;
45+
}
46+
47+
public ServiceFactory(Class<S> serviceClass, List<S> serviceList) {
48+
this(serviceClass);
49+
50+
this.addAll(serviceList);
51+
}
52+
53+
/**
54+
* 获取服务类
55+
*
56+
* @return 服务类
57+
*/
58+
public Class<S> getServiceClass() {
59+
return serviceClass;
60+
}
61+
62+
63+
// get ---------------------------------------------------------------
64+
65+
/**
66+
* 获取默认实现类
67+
*
68+
* @return 默认实现类
69+
*/
70+
@Nullable
71+
public S getDefault() {
72+
return get(DEFAULT_SERVICE_CODE);
73+
}
74+
75+
/**
76+
* 获取默认实现类
77+
*
78+
* @return 默认实现类
79+
*/
80+
@Nonnull
81+
public List<S> getDefaultList() {
82+
return getList(DEFAULT_SERVICE_CODE);
83+
}
84+
85+
@Nullable
86+
public S get(String code) {
87+
ServiceGroup<S> serviceGroup = serviceMap.get(code);
88+
if (serviceGroup == null) {
89+
return null;
90+
}
91+
return serviceGroup.getFirst();
92+
}
93+
94+
@Nonnull
95+
public List<S> getList(String code) {
96+
ServiceGroup<S> serviceGroup = serviceMap.get(code);
97+
if (serviceGroup == null) {
98+
return new ArrayList<>();
99+
}
100+
return serviceGroup.getAllService();
101+
}
102+
103+
@Nullable
104+
public ServiceGroup<S> getGroup(String code) {
105+
return serviceMap.get(code);
106+
}
107+
108+
109+
// add ---------------------------------------------------------------
110+
111+
public void add(S service) {
112+
ServiceInfo<S> serviceInfo = ServiceInfo.of(service);
113+
this.serviceMap.computeIfAbsent(serviceInfo.getCode(), ServiceGroup::new).add(serviceInfo);
114+
115+
if (serviceInfo.isDefault() && !DEFAULT_SERVICE_CODE.equals(serviceInfo.getCode())) {
116+
this.serviceMap.computeIfAbsent(DEFAULT_SERVICE_CODE, ServiceGroup::new).add(serviceInfo);
117+
}
118+
}
119+
120+
public void addAll(List<S> serviceList) {
121+
if (serviceList == null || serviceList.isEmpty()) {
122+
return;
123+
}
124+
125+
HashMap<String, List<ServiceInfo<S>>> map = new HashMap<>();
126+
for (S service : serviceList) {
127+
ServiceInfo<S> serviceInfo = ServiceInfo.of(service);
128+
map.computeIfAbsent(serviceInfo.getCode(), k -> new ArrayList<>()).add(serviceInfo);
129+
130+
if (serviceInfo.isDefault() && !DEFAULT_SERVICE_CODE.equals(serviceInfo.getCode())) {
131+
map.computeIfAbsent(DEFAULT_SERVICE_CODE, k -> new ArrayList<>()).add(serviceInfo);
132+
}
133+
}
134+
135+
map.forEach((code, serviceInfoList) -> {
136+
ServiceGroup<S> group = this.serviceMap.computeIfAbsent(code, ServiceGroup::new);
137+
group.addAll(serviceInfoList);
138+
});
139+
}
140+
141+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
/*
2+
* Copyright 2021-2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package icu.easyj.core.factory;
17+
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.Objects;
21+
22+
import javax.annotation.Nonnull;
23+
import javax.annotation.Nullable;
24+
25+
import icu.easyj.core.util.CollectionUtils;
26+
import icu.easyj.core.util.StringUtils;
27+
28+
/**
29+
* code相同的服务组
30+
*
31+
* @param <S> 服务类型
32+
* @author wangliang181230
33+
* @since 0.8.0
34+
*/
35+
@SuppressWarnings("unused")
36+
public class ServiceGroup<S> {
37+
38+
@Nonnull
39+
private final String code;
40+
41+
/**
42+
* 已排好序的 服务列表
43+
*/
44+
@Nonnull
45+
private final List<ServiceInfo<S>> sortedServiceList;
46+
47+
/**
48+
* 构造方法
49+
*/
50+
public ServiceGroup(String code) {
51+
if (StringUtils.isBlank(code)) {
52+
throw new IllegalArgumentException("code 不能为空");
53+
}
54+
55+
this.code = code;
56+
this.sortedServiceList = new ArrayList<>();
57+
}
58+
59+
60+
// get --------------------------------------------------------------------------
61+
62+
@Nullable
63+
public ServiceInfo<S> getFirstInfo() {
64+
if (CollectionUtils.isNotEmpty(this.sortedServiceList)) {
65+
return this.sortedServiceList.get(0);
66+
} else {
67+
return null;
68+
}
69+
}
70+
71+
@Nullable
72+
public S getFirst() {
73+
if (CollectionUtils.isNotEmpty(this.sortedServiceList)) {
74+
return this.sortedServiceList.get(0).getService();
75+
} else {
76+
return null;
77+
}
78+
}
79+
80+
public List<ServiceInfo<S>> getAllInfo() {
81+
return new ArrayList<>(this.sortedServiceList);
82+
}
83+
84+
public List<S> getAllService() {
85+
List<S> serviceList = new ArrayList<>();
86+
for (ServiceInfo<S> serviceInfo : this.sortedServiceList) {
87+
serviceList.add(serviceInfo.getService());
88+
}
89+
return serviceList;
90+
}
91+
92+
93+
// add --------------------------------------------------------------------------
94+
95+
public void add(ServiceInfo<S> serviceInfo) {
96+
this.checkServiceInfo(serviceInfo);
97+
98+
this.sortedServiceList.add(serviceInfo);
99+
this.sort();
100+
}
101+
102+
public void addAll(List<ServiceInfo<S>> serviceInfoList) {
103+
if (CollectionUtils.isEmpty(serviceInfoList)) {
104+
return;
105+
}
106+
107+
serviceInfoList.forEach(this::checkServiceInfo);
108+
109+
this.sortedServiceList.addAll(serviceInfoList);
110+
this.sort();
111+
}
112+
113+
114+
// Getter ---------------------------------------------------------------
115+
116+
public String getCode() {
117+
return code;
118+
}
119+
120+
public boolean isDefaultGroup() {
121+
return ServiceFactory.DEFAULT_SERVICE_CODE.equals(this.code);
122+
}
123+
124+
public List<ServiceInfo<S>> getOriginalList() {
125+
return this.sortedServiceList;
126+
}
127+
128+
129+
// Private ---------------------------------------------------------------
130+
131+
private void checkServiceInfo(ServiceInfo<S> serviceInfo) {
132+
if (ServiceFactory.DEFAULT_SERVICE_CODE.equals(this.code) && serviceInfo.isDefault()) {
133+
return;
134+
}
135+
136+
if (!Objects.equals(this.code, serviceInfo.getCode())) {
137+
throw new IllegalArgumentException("serviceInfo 的 code 与 ServiceGroup 的 code 不一致");
138+
}
139+
}
140+
141+
/**
142+
* 按 order、code、className 排序
143+
*/
144+
private void sort() {
145+
this.sortedServiceList.sort((a, b) -> {
146+
int ret = a.getOrder() - b.getOrder();
147+
if (ret == 0) {
148+
ret = a.getCode().compareTo(b.getCode());
149+
if (ret == 0) {
150+
ret = a.getService().getClass().getName().compareTo(b.getService().getClass().getName());
151+
}
152+
} else {
153+
if (ret < 0) {
154+
ret = -1;
155+
} else {
156+
ret = 1;
157+
}
158+
}
159+
return ret;
160+
});
161+
}
162+
163+
164+
@Override
165+
public String toString() {
166+
if (this.sortedServiceList.isEmpty()) {
167+
return this.code + "[]";
168+
}
169+
170+
StringBuilder sb = new StringBuilder();
171+
172+
sb.append(this.code).append('[');
173+
174+
for (ServiceInfo<S> serviceInfo : this.sortedServiceList) {
175+
if (sb.length() > 0) {
176+
sb.append(',');
177+
}
178+
sb.append(serviceInfo);
179+
}
180+
181+
sb.append(']');
182+
183+
return sb.toString();
184+
}
185+
186+
}

0 commit comments

Comments
 (0)